简体   繁体   中英

Maven module using spring-boot

I like to configure my applications in maven by creating modules like;

<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>

The modules then use the 'example-app' as the parent.

Now I want use 'spring-boot' for my web application.

Is there a way to configure maven so that my 'app-web' is a spring-boot application?

The problem I'm facing is that you have to use spring-boot as a parent.

You don't have to use the spring-boot-starter-parent, it's just a way to get started quickly. All it provides are dependency management and plugin management. You can do both yourself, and you can use the spring-boot-dependencies (or equivalently the parent) to manage dependencies if you want a halfway step. To do that, use scope=import like this

<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>

Another alternative, is to include in the parent pom, the parent declaration for spring boot, as shown in this post

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>

After that, in the modules poms (app-web, app-impl, etc.), you declare example-app as parent, but now you can include the starter dependencies as you would normally do in a regular project.

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>

Regarding version management, what i used in these examples aren't exactly the best practices, but since is out of the scope of the question i skipped dependencyManagement and parent properties usage.

Also, if there is a starter that is used in every module, you can declare the dependency in the parent pom and then all the modules will inherit it (for example spring-boot-starter-test )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM