springboot結(jié)合maven實現(xiàn)多模塊打包
我們平時在開發(fā)系統(tǒng)時,一般我們的系統(tǒng)工程會被分為多個模塊,一個原因是方便協(xié)同開發(fā),系統(tǒng)間解耦,另外一個很重要的原因是:別的系統(tǒng)需要依賴我們系統(tǒng)的部分功能,我們可能將這部分功能劃分到一個模塊里面,單獨打包提供給對方?,F(xiàn)在我將通過一個示例工程來演示如何借助maven完成springboot應(yīng)用的多模塊打包的操作。
要點:
1、工程存在多個模塊,模塊間有依賴關(guān)系
2、父工程維護工程的主版本號,子模塊直接引用父工程定義的版本號的變量
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ù)也會有其他系統(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ā)布到遠程倉庫的配置--> ? <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>
父模塊很重要的一個配置就是flatten-maven-plugin這個插件,用于打包時替換子模塊中pom文件的引用的父工程的變量,比如revision變量。如果不添加此插件,雖然打包時不會報錯,但是別的系統(tǒng)引用test-api.jar的時候,會出現(xiàn)類似Could not find artifact org.example:test:pom:${revision} in nexus-aliyun 的錯誤,主要原因就是子模塊中引用的父工程的變量未被替換導致的
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文件指定父工程時,version參數(shù)用變量表示,方便對版本號的維護。后續(xù)升級系統(tǒng)的版本號,只需要修改父工程中的revision變量即可。打包時,子模塊pom文件中的revision會被替換成revision的真實值,此處打包后jar包里的pom文件的{revision}會被替換成revision的真實值,此處打包后jar包里的pom文件的revision會被替換成revision的真實值,此處打包后jar包里的pom文件的{revision}會被替換成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時不將本模塊部署到倉庫--> <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ù)動態(tài)引用父工程中指定的版本號。將test−core打包后,test−core.jar包中的pom文件中的{revison}參數(shù)動態(tài)引用父工程中指定的版本號。將test-core打包后,test-core.jar包中的pom文件中的revison參數(shù)動態(tài)引用父工程中指定的版本號。將test−core打包后,test−core.jar包中的pom文件中的{revision}會被替換成revision參數(shù)的實際值1.0.0
3、工程打包
3.1、執(zhí)行打包
(1)進入test工程根目錄,比如我所在工程根目錄路徑是D:\ideaProject\test,
若執(zhí)行下述命令,
mvn clean install
test-api模塊和test-core模塊都會被打包進本地倉庫。
(2)如果執(zhí)行下述命令,test-api模塊會被部署到遠程倉庫,而test-core模塊則不會被部署到遠程倉庫。
mvn clean deploy
(3)如果只想打包test-api模塊到本地倉庫,或者只想把test-api模塊部署到遠程倉庫,可以進入test-api模塊的主目錄,比如D:\ideaProject\test\test-api,執(zhí)行下述命令
#只安裝到本地倉庫 mvn clean install #部署到遠程倉庫(該命令會先把包安裝到本地倉庫) 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實現(xiàn)多模塊打包的文章就介紹到這了,更多相關(guān)springboot maven多模塊打包內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity中PasswordEncoder的使用
密碼存儲和加密是非常重要的,本文主要介紹了SpringSecurity中PasswordEncoder的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-01-01