maven-idea構(gòu)建多模塊項目-maven的聚合和繼承

多模塊效果圖

1. 操作步驟

將多個子項目聚合在一個父工程中,子項目中,可引入parent標簽,即:繼承父工程。

第一步:選擇maven
第二步:填寫信息
第三步:點擊完成
第四步:刪除src目錄
第五步:選擇module
第六步:選擇maven工程
第七步:配置module
第八步:配置完成
父pom:maven的聚合
子pom:maven的繼承

創(chuàng)建其他的module模塊。

2. 依賴關(guān)系

1. 父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>com.tellme</groupId>
    <artifactId>springboot-client</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--自動生成的子模塊依賴-->
    <modules>
        <module>mm-web</module>
        <module>mm-service</module>
        <module>mm-repo</module>
        <module>mm-entity</module>
    </modules>
    <packaging>pom</packaging>

    <!--properties總體配置文件-->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <commons.lang3.version>3.5</commons.lang3.version>
    </properties>

    <!--繼承springboot提供的父工程-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!--版本統(tǒng)一聲明,聲明子模塊的版本號-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-web</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>  <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-service</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>  <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-repo</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.tellme</groupId>
                <artifactId>mm-entity</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--公共jar包聲明-->
    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons.lang3.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--插件管理-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

需要注意的點:

  1. pom.xml的打包方式:<packaging>pom</packaging>;
  2. modules標簽包含的是子模塊;
  3. properties標簽里面是總體參數(shù)配置;
  4. dependencyManagement標簽并不引入依賴,是總體的依賴聲明;
  5. dependencies標簽引入依賴,子類共享;
  6. build插件管理,需要注意的是dao、service、entity這三個module并不需要build標簽,父pom設(shè)置的子類共享。

2. entity模塊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>springboot-client</artifactId>
        <groupId>com.tellme</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>mm-entity</artifactId>
</project>

需要注意的點:

  1. parent標簽聲明父標簽;
  2. artifactId聲明子標簽,因為groupIdversion都是繼承了父pom,故可以省略;
  3. 因為是最底層依賴,故可以不依賴其他子模塊;

3. service模塊的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>springboot-client</artifactId>
        <groupId>com.tellme</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>mm-service</artifactId>
    <dependencies>
        <dependency>
            <groupId>com.tellme</groupId>
            <artifactId>mm-entity</artifactId>
        </dependency>
    </dependencies>
</project>

需要注意的點:

  1. parent標簽聲明父pom;
  2. service模塊需要使用entity模塊的數(shù)據(jù),故需要dependencies標簽引入entity模塊;
  3. 注意循環(huán)引用問題,即service模塊依賴entity模塊;entity模塊也依賴service模塊

4. web模塊的pom.xml內(nèi)容

<?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>springboot-client</artifactId>
        <groupId>com.tellme</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>mm-web</artifactId>

    <dependencies>
        <!--依賴service層-->
        <dependency>
            <groupId>com.tellme</groupId>
            <artifactId>mm-service</artifactId>
        </dependency>
        <!--依賴entity層-->
        <dependency>
            <groupId>com.tellme</groupId>
            <artifactId>mm-entity</artifactId>
        </dependency>
    </dependencies>

    <!--多模塊打包-->
    <build>
        <defaultGoal>package</defaultGoal>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 多模塊項目僅僅需要在啟動類所在的模塊添加打包插件? -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

需要注意的點:

  1. web層需要依賴service層和entity層,故需將其dependency引入;
  2. 在多模塊項目中僅僅需要在啟動類所在的模塊添加打包插件,將其打成可運行的jar。

3. 打包可執(zhí)行jar

打包執(zhí)行步驟
  1. 在每次打包前,需要clean。
  2. 雙擊運行package,看到BUILD SUCCESS 就證明打包成功了。
clean成功
package成功

打包的重點是每一個模塊下的pom要配置好,誰需要打包,誰不需要打包,誰依賴誰,父工程是否聲明了子模塊,子模塊是否聲明了父工程。

執(zhí)行jar包

運行jar包

測試成功

執(zhí)行java -jar xxx.jar命令來測試運行打包是否成功。

聚合工程舉一個簡單的例子:
整個工程就好像一個公司,父工程(退休了什么都不干)只需要聲明有幾個兒子(子模塊)就完事了。
子模塊web聲明父工程是誰,就當他是大兒子,公司歸他管,pom.xml需要打包,需要build配置,需要其他子模塊的幫助。
其他模塊聲明父工程是誰,之間關(guān)系都是兄弟,不需要打包,哪里需要就去哪里。

反編譯mm-web.jar

我們可以看到mm-web.jar實際上在lib文件下包含了所有的jar包。

4. 需要注意的點

  1. 父pom.xml打包方式,jar要更改為pom,build需要更改;
  2. 不需要打包的模塊pom.xml文件不要寫<build>,全刪掉,例如有些工程的common模塊,utils模塊都不需要打包;
  3. 聲明父工程時,填寫父類工程位置<relativePath>../pom.xml</relativePath>
  4. 關(guān)于application.properties配置文件,只需要在啟動的模塊中配置就可以了。
  5. 關(guān)于打包為什么打包jar包,不打war包,打war包目的是war包可以運行在tomcat下,但是SpringBoot是內(nèi)置tomcat,如果你打war包,前提是干掉內(nèi)置的tomcat,然后才能打包,各種麻煩,直接打包可執(zhí)行jar包,使用java -jar 命令就可以完美的運行起來很方便!
  6. 真實開發(fā)中使用@Autowired 注解 來實現(xiàn)注入,而不是new對象這種方式,所以可能會產(chǎn)生注入以后報錯,是因為你的啟動類上沒有配置掃描,使用

@ComponentScan(basePackages = "你的路徑")注解來解決,如果你使用的持久層是Mybatis,那么你的mapper也需要掃描,在啟動類上使用

@MapperScan("你的mapper文件地址")注解來解決,算了還是貼個圖片吧
真實開發(fā)中使用@Autowired 注解 來實現(xiàn)注入,而不是new對象這種方式,所以可能會產(chǎn)生注入以后報錯,是因為你的啟動類上沒有配置掃描,使用

@ComponentScan(basePackages = "你的路徑")注解來解決,如果你使用的持久層是Mybatis,那么你的mapper也需要掃描,在啟動類上使用

@MapperScan("你的mapper文件地址")注解來解決,算了還是貼個圖片吧

多模塊使用注解開啟全局掃描

4. 聚合和繼承的關(guān)系

區(qū)別:

  • 對于聚合模塊來說,他知道哪些被聚合的模塊,但那些被聚合的模塊不知道這個模塊的存在;

  • 對于繼承關(guān)系的父POM來說,它不知道哪些子模塊繼承了它,但是子模塊都必須知道自己的父POM是什么。

共同點:

  • 聚合POM與繼承關(guān)系的父POM的packaging都是POM。
  • 聚合模塊與繼承關(guān)系的父模塊除了POM之外都沒有實際的內(nèi)容。
聚合和繼承

詳細參看:
https://blog.csdn.net/baidu_41885330/article/details/81875395

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容