add spring-boot modules to maven project

spring boot 項目中 多modules parent 衝突

若是一個maven project 創建多modules ,pom文件應該是這樣:java

<groupId>cn.ifengkou</groupId>
<artifactId>spring-cloud-test</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>

<modules>
    <module>spring-boot-test</module>
    <module>spring-cloud-config</module>
</modules>

module 的pom文件應該是這樣:git

<parent>
    <artifactId>spring-cloud-test</artifactId>
    <groupId>cn.ifengkou</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-test</artifactId>

你們在一開始學spring-boot時,都應該看過 官網的quick start項目,裏面pom文件也有一個parentgithub

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

這樣,modules 項目中的 要繼承父項目,而spring boot 又要繼承spring-boot-starter-parent,一個pom文件不能存在兩個parent,這個問題怎麼解決呢?web

網上提供了一種方式,經過在子module 的parent 中,加 ../pom.xml 節點,實現父項目依賴管理的繼承。但這種方式違背了按需加載依賴的理念,並不可取spring

#project(parent) pom
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>
<modules>
    <module>dao</module>
</modules>

# module pom
<parent>
    <artifactId>parent</artifactId>
    <groupId>com.luyh.projectv1</groupId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
</parent>

其實spring-boot-starter-parent 只是一種快速開始spring boot 的方式,不必非要用它,它所作的只是提供依賴管理和plugin管理(dependency management and plugin management),咱們也能夠本身作這些,固然若是想省事,spring 也提供了 spring-boot-dependencies 這個依賴(等價於 parent) 去管理依賴項,要實現一樣的效果,你只須要用 scope=import, 像下面例子:apache

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring.boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

完整pom.xml,或者查看githubspring-cloud-testmaven

#project(parent) pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.ifengkou</groupId>
    <artifactId>spring-cloud-test</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>spring-boot-test</module>
        <module>spring-cloud-config</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

        <spring.boot.version>1.5.2.RELEASE</spring.boot.version>
    </properties>


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>


# module pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>spring-cloud-test</artifactId>
        <groupId>cn.ifengkou</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-test</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
相關文章
相關標籤/搜索