Maven模块使用spring-boot

hel*_*ldt 33 java spring maven spring-boot

我喜欢通过创建模块来配置maven中的应用程序;

<groupId>com.app</groupId>
<artifactId>example-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>app-api</module>
    <module>app-impl</module>
    <module>app-web</module>
</modules>
Run Code Online (Sandbox Code Playgroud)

然后,模块使用'example-app'作为父级.

现在我想在我的Web应用程序中使用'spring-boot'.

有没有办法配置maven,以便我的'app-web'是一个spring-boot应用程序?

我面临的问题是你必须使用spring-boot作为父级.

Dav*_*yer 55

您不必使用spring-boot-starter-parent,它只是一种快速入门的方法.它提供的只是依赖管理和插件管理.您可以自己完成,如果您想要中途步骤,可以使用spring-boot-dependencies(或等效的父项)来管理依赖项.要做到这一点,请使用scope=import这样的

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <type>pom</type>
            <version>1.0.2.RELEASE</version>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
Run Code Online (Sandbox Code Playgroud)

  • 轻微编辑 - 你需要在<dependencyManagement>下面添加一个<dependencies>元素,否则它似乎也适合我. (2认同)
  • 当我用"spring-boot-dependencies"工件的depdendency替换spring-boot-starter-parent的声明时,我得到以下错误:没有主要的manifest属性,在target/my-app-0.1.0.jar中是否意味着我必须从spring-boot-starter-parent复制所有插件? (2认同)

sal*_*ama 9

另一种替代方法,是在父POM,为弹簧引导父声明包括,如图此

example-app pom.xml:

<project>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    // rest of the example-app pom declarations
</project>
Run Code Online (Sandbox Code Playgroud)

之后,在模块poms(app-web,app-impl等)中,您将example-app声明为parent,但现在您可以像通常在常规项目中那样包含入门依赖项.

app-web pom.xml:

<project>
    <parent>
        <groupId>org.demo</groupId>
        <artifactId>example-app</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <name>app-web</name>
    <artifactId>app-web</artifactId>
    <version>1.0-SNAPSHOT</version> 
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.demo</groupId>
            <artifactId>app-api</artifactId>
            <version>1.0-SNAPSHOT</version> 
        </dependency>
        <dependency>
            <groupId>org.demo</groupId>
            <artifactId>app-impl</artifactId>
            <version>1.0-SNAPSHOT</version> 
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    // rest of the app-web pom declarations
</project>
Run Code Online (Sandbox Code Playgroud)

关于版本管理,我在这些示例中使用的并不是最佳实践,但由于超出了问题的范围,我跳过了dependencyManagement和父属性使用.

此外,如果在每个模块中都使用了启动器,则可以在父pom中声明依赖关系,然后所有模块都将继承它(例如spring-boot-starter-test)