java 带弹簧靴的多模块项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30207589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:39:43  来源:igfitidea点击:

Multi module project with spring boot

javaspringmaven

提问by alok ailawadi

I am creating a project with spring-boot.

我正在创建一个项目spring-boot

Spring boot maven structure mandates that it has a parent defined to spring-boot-starter-parent.

Spring boot maven 结构要求它有一个定义为spring-boot-starter-parent.

I have a situation where I would like to package my application as a multi-module structure where I define modules which contain functionality related to a geography.

我有一种情况,我想将我的应用程序打包为一个多模块结构,在该结构中我定义包含与地理相关的功能的模块。

Something like this, each module has a jar packaging,

像这样,每个模块都有一个jar包,

parent Pom
|
|-----------Core module
|
|-----------India Module
|
|----------Africa Module
|
|--------- Europe Module

Now, I can package my application depending on geography using maven profiles where in India profile only core module and India module are included and packaged.

现在,我可以使用 maven 配置文件根据地理位置打包我的应用程序,其中在印度配置文件中仅包含和打包核心模块和印度模块。

How can I achieve it using spring boot where my parent is already defined to spring-boot-starter-parent?

我如何使用 spring boot 来实现它,我的父级已经定义为spring-boot-starter-parent

回答by alok ailawadi

There are two ways one can do it,

有两种方法可以做到,

  1. use dependencyManagement in your parent pom as in

     <dependencyManagement>
        <dependencies>
           <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <type>pom</type>
            <version>1.2.3.RELEASE</version>
            <scope>import</scope>
        </dependency>
    </dependencies>
    

  2. This is the way I am doing it. The root POM (with pom packaging) has spring boot as parent

    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.2.3.RELEASE</version>
    </parent> 
    
  1. 在你的父 pom 中使用依赖管理

     <dependencyManagement>
        <dependencies>
           <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <type>pom</type>
            <version>1.2.3.RELEASE</version>
            <scope>import</scope>
        </dependency>
    </dependencies>
    

  2. 这就是我这样做的方式。根 POM(带 pom 包装)以 spring boot 作为父级

    <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.2.3.RELEASE</version>
    </parent> 
    

the module poms have root pom as parent. The core module POM adds has dependency on other modules. It also has 'spring-boot-maven-plugin' to create the fat jar.

模块 poms 将根 pom 作为父项。POM 添加的核心模块依赖于其他模块。它还具有“spring-boot-maven-plugin”来创建胖罐。

The spring context is started with java -jar Core_xxx.release.jar

spring 上下文以 java -jar Core_xxx.release.jar 启动

Off course if you have organization wise parent POM then first option would be more appropriate.

当然,如果您有组织明智的父 POM,那么第一个选项会更合适。

回答by BSeitkazin

Right now you can solve that problem using official Spring guidline and create Spring Boot Multi-Module project. Just follow the instruction https://spring.io/guides/gs/multi-module/

现在您可以使用官方 Spring 指南解决该问题并创建 Spring Boot Multi-Module 项目。只需按照说明https://spring.io/guides/gs/multi-module/

回答by cbeutenmueller

I don't know specifically about Spring Boot.

我不特别了解 Spring Boot。

But in general using Spring this can be done by common configuration packages. So in your main App/Configuration you do a:

但是一般使用 Spring 这可以通过常见的配置包来完成。因此,在您的主应用程序/配置中,您执行以下操作:

@Configuration
@ComponentScan("com.example.common.bootstrap")

And then you can just put module specific config in the bootstrap package for each module. So just create a com.example.common.boostrap.AsiaModule and com.example.common.bootstrap.EuropeModule as Spring Configurations and you are good.

然后你可以将模块特定的配置放在每个模块的引导程序包中。所以只需创建一个 com.example.common.boostrap.AsiaModule 和 com.example.common.bootstrap.EuropeModule 作为 Spring 配置,你就很好。