1. 操作步驟
將多個子項目聚合在一個父工程中靴庆,子項目中册养,可引入parent標簽构拳,即:繼承父工程蕊梧。
創(chuàng)建其他的module模塊霞赫。
2. 依賴關系
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>
需要注意的點:
- pom.xml的打包方式:
<packaging>pom</packaging>
肥矢; - modules標簽包含的是子模塊绩脆;
- properties標簽里面是總體參數(shù)配置;
- dependencyManagement標簽并不引入依賴橄抹,是總體的依賴聲明靴迫;
- dependencies標簽引入依賴,子類共享楼誓;
- build插件管理玉锌,需要注意的是
dao
、service
疟羹、entity
這三個module并不需要build標簽主守,父pom設置的子類共享。
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>
需要注意的點:
- parent標簽聲明父標簽榄融;
- artifactId聲明子標簽参淫,因為
groupId
和version
都是繼承了父pom,故可以省略愧杯; - 因為是最底層依賴涎才,故可以不依賴其他子模塊;
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>
需要注意的點:
- parent標簽聲明父pom力九;
- service模塊需要使用entity模塊的數(shù)據(jù)耍铜,故需要dependencies標簽引入entity模塊;
- 注意循環(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>
需要注意的點:
- web層需要依賴service層和entity層抵乓,故需將其dependency引入伴挚;
- 在多模塊項目中僅僅需要在啟動類所在的模塊添加打包插件靶衍,將其打成可運行的jar。
3. 打包可執(zhí)行jar
- 在每次打包前茎芋,需要clean颅眶。
- 雙擊運行package,看到BUILD SUCCESS 就證明打包成功了败徊。
打包的重點是每一個模塊下的pom要配置好,誰需要打包掏缎,誰不需要打包皱蹦,誰依賴誰,父工程是否聲明了子模塊眷蜈,子模塊是否聲明了父工程沪哺。
執(zhí)行jar包
執(zhí)行java -jar xxx.jar
命令來測試運行打包是否成功。
聚合工程舉一個簡單的例子:
整個工程就好像一個公司酌儒,父工程(退休了什么都不干)只需要聲明有幾個兒子(子模塊)就完事了辜妓。
子模塊web聲明父工程是誰,就當他是大兒子忌怎,公司歸他管籍滴,pom.xml需要打包,需要build配置榴啸,需要其他子模塊的幫助孽惰。
其他模塊聲明父工程是誰,之間關系都是兄弟鸥印,不需要打包勋功,哪里需要就去哪里。
我們可以看到mm-web.jar實際上在lib文件下包含了所有的jar包库说。
4. 需要注意的點
- 父pom.xml打包方式狂鞋,jar要更改為pom,build需要更改潜的;
- 不需要打包的模塊pom.xml文件不要寫<build>骚揍,全刪掉,例如有些工程的common模塊啰挪,utils模塊都不需要打包疏咐;
- 聲明父工程時,填寫父類工程位置<relativePath>../pom.xml</relativePath>
- 關于application.properties配置文件脐供,只需要在啟動的模塊中配置就可以了浑塞。
- 關于打包為什么打包jar包,不打war包政己,打war包目的是war包可以運行在tomcat下酌壕,但是SpringBoot是內(nèi)置tomcat掏愁,如果你打war包,前提是干掉內(nèi)置的tomcat卵牍,然后才能打包果港,各種麻煩,直接打包可執(zhí)行jar包糊昙,使用java -jar 命令就可以完美的運行起來很方便辛掠!
- 真實開發(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. 聚合和繼承的關系
區(qū)別:
對于聚合模塊來說锯梁,他知道哪些被聚合的模塊即碗,但那些被聚合的模塊不知道這個模塊的存在;
對于繼承關系的父POM來說陌凳,它不知道哪些子模塊繼承了它剥懒,但是子模塊都必須知道自己的父POM是什么。
共同點:
- 聚合POM與繼承關系的父POM的packaging都是POM合敦。
- 聚合模塊與繼承關系的父模塊除了POM之外都沒有實際的內(nèi)容初橘。
詳細參看:
https://blog.csdn.net/baidu_41885330/article/details/81875395