springboot怎么结合maven实现多模块打包


今天主机评测网小编给大家分享一下springboot怎么结合maven实现多模块打包的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

    我们平时在开发系统时,一般我们的系统工程会被分为多个模块,一个原因是方便协同开发,系统间解耦,另外一个很重要的原因是:别的系统需要依赖我们系统的部分功能,我们可能将这部分功能划分到一个模块里面,单独打包提供给对方。现在我将通过一个示例工程来演示如何借助maven完成springboot应用的多模块打包的操作。

    要点:

    1、工程存在多个模块,模块间有依赖关系

    2、父工程维护工程的主版本号,子模块直接引用父工程定义的版本号的变量

    3、借助flatten-maven-plugin插件完成子模块pom文件中引用的父工程变量的替换工作

    1、 工程结构

    test工程结构

    test--test-api--src--main--pom.xml--test-core--src--main--java--resouce--test--pom.xml

    其中test-api模块为共用模块,test-core模块依赖test-api模块。后续也会有其他系统依赖test-api模块,因此需要将test-api模块发布到maven私服。

    2、工程模块pom文件配置

    2.1、父模块pom配置

    <?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="maven.apache.org/POM/4.0.0"xmlns:xsi="w3.org/2001/XMLSchema-instance"xsi:schemaLocation="maven.apache.org/POM/4.0.0maven.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/><!--lookupparentfromrepository--></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><mavenpiler.source>8</mavenpiler.source><mavenpiler.target>8</mavenpiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><revision>1.0.0</revision></properties><!--发布到远程仓库的配置--><distributionManagement><repository><id>releases</id><name>releases</name><url>192.168.1.1/repository/releases/</url></repository><snapshotRepository><id>snapshots</id><url>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><!--enableflattening--><execution><id>flatten</id><phase>process-resources</phase><goals><goal>flatten</goal></goals></execution><!--ensurepropercleanup--><execution><id>flatten.clean</id><phase>clean</phase><goals><goal>clean</goal></goals></execution></executions></plugin></plugins></build></project>

    父模块很重要的一个配置就是flatten-maven-plugin这个插件,用于打包时替换子模块中pom文件的引用的父工程的变量,比如revision变量。如果不添加此插件,虽然打包时不会报错,但是别的系统引用test-api.jar的时候,会出现类似Could not find artifact org.example:test:pom:${revision} in nexus-aliyun 的错误,主要原因就是子模块中引用的父工程的变量未被替换导致的

    2.2、test-api模块配置

    <?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="maven.apache.org/POM/4.0.0"xmlns:xsi="w3.org/2001/XMLSchema-instance"xsi:schemaLocation="maven.apache.org/POM/4.0.0maven.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><mavenpiler.source>8</mavenpiler.source><mavenpiler.target>8</mavenpiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies></dependencies></project>

    test-api模块的pom文件指定父工程时,version参数用变量表示,方便对版本号的维护。后续升级系统的版本号,只需要修改父工程中的revision变量即可。打包时,子模块pom文件中的revision会被替换成revision的真实值,此处打包后jar包里的pom文件的{revision}会被替换成revision的真实值,此处打包后jar包里的pom文件的revision会被替换成revision的真实值,此处打包后jar包里的pom文件的{revision}会被替换成1.0.0

    2.3、test-core模块配置

    <?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="maven.apache.org/POM/4.0.0"xmlns:xsi="w3.org/2001/XMLSchema-instance"xsi:schemaLocation="maven.apache.org/POM/4.0.0maven.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><mavenpiler.source>8</mavenpiler.source><mavenpiler.target>8</mavenpiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--跳过部署,执行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参数动态引用父工程中指定的版本号。将test&minus;core打包后,test&minus;core.jar包中的pom文件中的{revison}参数动态引用父工程中指定的版本号。将test-core打包后,test-core.jar包中的pom文件中的revison参数动态引用父工程中指定的版本号。将test&minus;core打包后,test&minus;core.jar包中的pom文件中的{revision}会被替换成revision参数的实际值1.0.0

    3、工程打包

    3.1、执行打包

    (1)进入test工程根目录,比如我所在工程根目录路径是D:\ideaProject\test,

    若执行下述命令,

    mvncleaninstall

    test-api模块和test-core模块都会被打包进本地仓库。

    (2)如果执行下述命令,test-api模块会被部署到远程仓库,而test-core模块则不会被部署到远程仓库。

    mvncleandeploy

    (3)如果只想打包test-api模块到本地仓库,或者只想把test-api模块部署到远程仓库,可以进入test-api模块的主目录,比如D:\ideaProject\test\test-api,执行下述命令

    #只安装到本地仓库mvncleaninstall#部署到远程仓库(该命令会先把包安装到本地仓库)mvncleandeploy

    3.2、打包效果

    已test-api为例,打包后的test-api-1.00.jar文件中的pom.xml文件内容如下所示

    <?xmlversion="1.0"encoding="UTF-8"?><projectxsi:schemaLocation="maven.apache.org/POM/4.0.0maven.apache.org/xsd/maven-4.0.0.xsd"xmlns="maven.apache.org/POM/4.0.0"xmlns:xsi="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>ApacheLicense,Version2.0</name><url>apache.org/licenses/LICENSE-2.0</url></license></licenses></project>

    可以发现,里面引入的父工程的变量已经被成功替换。

    以上就是“springboot怎么结合maven实现多模块打包”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注主机评测网行业资讯频道。


    上一篇:怎么用Ant?Design编写登录和注册页面

    下一篇:Android升级gradle后引入aar包报错怎么解决


    Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
    温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
    热门搜索