Maven module using spring-boot

前端 未结 2 1647
無奈伤痛
無奈伤痛 2021-02-01 02:39

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

com.app
example-app
&l         


        
相关标签:
2条回答
  • 2021-02-01 03:00

    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>
    
    0 讨论(0)
  • 2021-02-01 03:07

    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)

    0 讨论(0)
提交回复
热议问题