springboot結(jié)合maven實(shí)現(xiàn)多模塊打包
我們平時(shí)在開發(fā)系統(tǒng)時(shí),一般我們的系統(tǒng)工程會(huì)被分為多個(gè)模塊,一個(gè)原因是方便協(xié)同開發(fā),系統(tǒng)間解耦,另外一個(gè)很重要的原因是:別的系統(tǒng)需要依賴我們系統(tǒng)的部分功能,我們可能將這部分功能劃分到一個(gè)模塊里面,單獨(dú)打包提供給對(duì)方。現(xiàn)在我將通過一個(gè)示例工程來演示如何借助maven完成springboot應(yīng)用的多模塊打包的操作。
要點(diǎn):
1、工程存在多個(gè)模塊,模塊間有依賴關(guān)系
2、父工程維護(hù)工程的主版本號(hào),子模塊直接引用父工程定義的版本號(hào)的變量
3、借助flatten-maven-plugin插件完成子模塊pom文件中引用的父工程變量的替換工作
1、 工程結(jié)構(gòu)
test工程結(jié)構(gòu)
test --test-api --src ? --main --pom.xml --test-core --src ? --main ? ? --java ? ? --resouce ? --test --pom.xml
其中test-api模塊為共用模塊,test-core模塊依賴test-api模塊。后續(xù)也會(huì)有其他系統(tǒng)依賴test-api模塊,因此需要將test-api模塊發(fā)布到maven私服。
2、工程模塊pom文件配置
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">
?
? <parent>
? ? ? <groupId>org.springframework.boot</groupId>
? ? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? ? <version>2.2.6.RELEASE</version>
? ? ? <relativePath/> <!-- lookup parent from repository -->
? </parent>
?
? <modelVersion>4.0.0</modelVersion>
?
? <groupId>org.example</groupId>
? <artifactId>test</artifactId>
? <version>${revision}</version>
? <packaging>pom</packaging>
? <modules>
? ? ? <module>test-api</module>
? ? ? <module>test-core</module>
? </modules>
?
? <properties>
? ? ? <maven.compiler.source>8</maven.compiler.source>
? ? ? <maven.compiler.target>8</maven.compiler.target>
? ? ? <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
? ? ? <revision>1.0.0</revision>
? </properties>
? <!--發(fā)布到遠(yuǎn)程倉庫的配置-->
? <distributionManagement>
? ? ? <repository>
? ? ? ? ? <id>releases</id>
? ? ? ? ? <name>releases</name>
? ? ? ? ? <url>http://192.168.1.1/repository/releases/</url>
? ? ? </repository>
?
? ? ? <snapshotRepository>
? ? ? ? ? <id>snapshots</id>
? ? ? ? ? <url>http://192.168.1.1/repository/snapshots/</url>
? ? ? </snapshotRepository>
?
? </distributionManagement>
?
? <build>
? ? ? <plugins>
? ? ? ? ? <plugin>
? ? ? ? ? ? ? <groupId>org.codehaus.mojo</groupId>
? ? ? ? ? ? ? <artifactId>flatten-maven-plugin</artifactId>
? ? ? ? ? ? ? <version>1.4.1</version>
? ? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? </configuration>
? ? ? ? ? ? ? <executions>
? ? ? ? ? ? ? ? ? <!-- enable flattening -->
? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? <id>flatten</id>
? ? ? ? ? ? ? ? ? ? ? <phase>process-resources</phase>
? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? <goal>flatten</goal>
? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? ? ? <!-- ensure proper cleanup -->
? ? ? ? ? ? ? ? ? <execution>
? ? ? ? ? ? ? ? ? ? ? <id>flatten.clean</id>
? ? ? ? ? ? ? ? ? ? ? <phase>clean</phase>
? ? ? ? ? ? ? ? ? ? ? <goals>
? ? ? ? ? ? ? ? ? ? ? ? ? <goal>clean</goal>
? ? ? ? ? ? ? ? ? ? ? </goals>
? ? ? ? ? ? ? ? ? </execution>
? ? ? ? ? ? ? </executions>
? ? ? ? ? </plugin>
? ? ? </plugins>
? </build>
?
?
</project>父模塊很重要的一個(gè)配置就是flatten-maven-plugin這個(gè)插件,用于打包時(shí)替換子模塊中pom文件的引用的父工程的變量,比如revision變量。如果不添加此插件,雖然打包時(shí)不會(huì)報(bào)錯(cuò),但是別的系統(tǒng)引用test-api.jar的時(shí)候,會(huì)出現(xiàn)類似Could not find artifact org.example:test:pom:${revision} in nexus-aliyun 的錯(cuò)誤,主要原因就是子模塊中引用的父工程的變量未被替換導(dǎo)致的
2.2、test-api模塊配置
<?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>
<parent>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
?
<artifactId>test-api</artifactId>
?
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
?
<dependencies>
?
</dependencies>
?
</project>test-api模塊的pom文件指定父工程時(shí),version參數(shù)用變量表示,方便對(duì)版本號(hào)的維護(hù)。后續(xù)升級(jí)系統(tǒng)的版本號(hào),只需要修改父工程中的revision變量即可。打包時(shí),子模塊pom文件中的revision會(huì)被替換成revision的真實(shí)值,此處打包后jar包里的pom文件的{revision}會(huì)被替換成revision的真實(shí)值,此處打包后jar包里的pom文件的revision會(huì)被替換成revision的真實(shí)值,此處打包后jar包里的pom文件的{revision}會(huì)被替換成1.0.0
2.3、test-core模塊配置
<?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>
<parent>
<groupId>org.example</groupId>
<artifactId>test</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
?
<artifactId>test-core</artifactId>
?
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--跳過部署,執(zhí)行deploy時(shí)不將本模塊部署到倉庫-->
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
?
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>test-api</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
?
</project>test-core模塊直接依賴test-api模塊,通過revison參數(shù)動(dòng)態(tài)引用父工程中指定的版本號(hào)。將test−core打包后,test−core.jar包中的pom文件中的{revison}參數(shù)動(dòng)態(tài)引用父工程中指定的版本號(hào)。將test-core打包后,test-core.jar包中的pom文件中的revison參數(shù)動(dòng)態(tài)引用父工程中指定的版本號(hào)。將test−core打包后,test−core.jar包中的pom文件中的{revision}會(huì)被替換成revision參數(shù)的實(shí)際值1.0.0
3、工程打包
3.1、執(zhí)行打包
(1)進(jìn)入test工程根目錄,比如我所在工程根目錄路徑是D:\ideaProject\test,
若執(zhí)行下述命令,
mvn clean install
test-api模塊和test-core模塊都會(huì)被打包進(jìn)本地倉庫。
(2)如果執(zhí)行下述命令,test-api模塊會(huì)被部署到遠(yuǎn)程倉庫,而test-core模塊則不會(huì)被部署到遠(yuǎn)程倉庫。
mvn clean deploy
(3)如果只想打包test-api模塊到本地倉庫,或者只想把test-api模塊部署到遠(yuǎn)程倉庫,可以進(jìn)入test-api模塊的主目錄,比如D:\ideaProject\test\test-api,執(zhí)行下述命令
#只安裝到本地倉庫 mvn clean install #部署到遠(yuǎn)程倉庫(該命令會(huì)先把包安裝到本地倉庫) mvn clean deploy
3.2、打包效果
已test-api為例,打包后的test-api-1.00.jar文件中的pom.xml文件內(nèi)容如下所示
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test-api</artifactId>
<version>1.0.0</version>
<licenses>
<license>
<name>Apache License, Version 2.0</name>
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
</license>
</licenses>
</project>可以發(fā)現(xiàn),里面引入的父工程的變量已經(jīng)被成功替換。
參考
1、flatten-maven-plugin官網(wǎng)
到此這篇關(guān)于springboot結(jié)合maven實(shí)現(xiàn)多模塊打包的文章就介紹到這了,更多相關(guān)springboot maven多模塊打包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
迪米特法則_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了迪米特法則,迪米特法則就是一個(gè)在類創(chuàng)建方法和屬性時(shí)需要遵守的法則,有興趣的可以了解一下2017-08-08
使用Aop的方式實(shí)現(xiàn)自動(dòng)日志記錄的方式詳細(xì)介紹
這篇文章主要介紹了使用Aop的方式實(shí)現(xiàn)自動(dòng)日志記錄,通過監(jiān)聽器去監(jiān)聽,當(dāng)訪問到具體的類方法,通過aop切面去獲取訪問的方法,然后將日志記錄下來,就這種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
SpringSecurity中PasswordEncoder的使用
密碼存儲(chǔ)和加密是非常重要的,本文主要介紹了SpringSecurity中PasswordEncoder的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01

