springboot结合maven实现多模块打包
作者:IT人的天地 发布时间:2022-01-16 07:13:51
我们平时在开发系统时,一般我们的系统工程会被分为多个模块,一个原因是方便协同开发,系统间解耦,另外一个很重要的原因是:别的系统需要依赖我们系统的部分功能,我们可能将这部分功能划分到一个模块里面,单独打包提供给对方。现在我将通过一个示例工程来演示如何借助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配置
<?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>
<!--发布到远程仓库的配置-->
<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变量。如果不添加此插件,虽然打包时不会报错,但是别的系统引用test-api.jar的时候,会出现类似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参数用变量表示,方便对版本号的维护。后续升级系统的版本号,只需要修改父工程中的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>
<!--跳过部署,执行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−core打包后,test−core.jar包中的pom文件中的{revison}参数动态引用父工程中指定的版本号。将test-core打包后,test-core.jar包中的pom文件中的revison参数动态引用父工程中指定的版本号。将test−core打包后,test−core.jar包中的pom文件中的{revision}会被替换成revision参数的实际值1.0.0
3、工程打包
3.1、执行打包
(1)进入test工程根目录,比如我所在工程根目录路径是D:\ideaProject\test,
若执行下述命令,
mvn clean install
test-api模块和test-core模块都会被打包进本地仓库。
(2)如果执行下述命令,test-api模块会被部署到远程仓库,而test-core模块则不会被部署到远程仓库。
mvn clean deploy
(3)如果只想打包test-api模块到本地仓库,或者只想把test-api模块部署到远程仓库,可以进入test-api模块的主目录,比如D:\ideaProject\test\test-api,执行下述命令
#只安装到本地仓库
mvn clean install
#部署到远程仓库(该命令会先把包安装到本地仓库)
mvn clean deploy
3.2、打包效果
已test-api为例,打包后的test-api-1.00.jar文件中的pom.xml文件内容如下所示
<?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>
可以发现,里面引入的父工程的变量已经被成功替换。
参考
1、flatten-maven-plugin官网
来源:https://juejin.cn/post/7222931700439498809


猜你喜欢
- 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1)
- 1.1 解决方案名和项目名上右键——重命名如下图:改完后效果如下:1.2 接下来要改名 代码中的名称
- 开篇Mybatis有个实用的功能就是逆向工程,能根据表结构反向生成实体类,这样能避免手工生成出错。市面上的教程大多都很老了,大部分都是针对m
- 这篇文章主要介绍了Maven打包jar生成javadoc文件和source文件代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作
- 需求分析文档可以和项目一起进行版本管理文档可以在线访问文档可以与springboot项目集成,不需要分开部署MarkDown支持文档跟随,打
- 输出为MP4需要用到ffmpeg相关的文件,我打包的库已经带了,去官网找的库可以在这个目录找到:2:添加这些引用:3:两个全局变量://用来
- using System.Drawing;using System.Drawing.Drawing2D;using System.Drawi
- 前言:在日常开发中经常会遇到字符串匹配问题,我们就来学习使用Java中的一些方便快捷的方法来解决这个问题吧使用String类Java自带的字
- Android中的Adapter在自定义显示列表时非常有用,比如SimpleAdapter,它的构造函数是:public SimpleAda
- 前言我们都知道memberwiseclone 会将浅克隆。什么是浅克隆?如何深克隆呢?正文public class good{
- Java 定时器在JAVA中实现定时器功能要用的二个类是Timer,TimerTaskTimer类是用来执行任务的类,它接受一个
- 前言最近做了个滑动选择的小控件,拿出来给大家分享一下,先上图运行效果实现步骤这里分解为3个动作:Down、Move、Up来进行分析,博主文采
- 本文实例讲述了android编程实现图片库的封装方法。分享给大家供大家参考,具体如下:大家在做安卓应用的时候 经常要从网络中获取图片 都是通
- HTTP 头处理HTTP 头是 HTTP 请求和响应中的重要组成部分。在创建 HTTP 请求时需要设置一些 HTTP 头。在得到 HTTP
- Android 拦截返回键事件的实例详解KeyEvent类Android.View.KeyEvent类中定义了一系列的常量和方法,用来描述A
- 前言在RequestMappingHandlerAdapter对request进行了适配,并且调用了目标handler之后,其会返回一个Mo
- 本文实例讲述了Java实现的并发任务处理方法。分享给大家供大家参考,具体如下:public void init() { super.init
- 一、思路将分页所需的内容都放到一个实体类中分页数据所需要的实体类!内包含页码,页大小,总条数,总页数,起始行pagehelpr提供了这个类
- 1、用字符串分隔: using System.Text.RegularExpressions; string str="aaajs
- 本文实例讲述了Android实现模仿UCweb菜单效果的方法。分享给大家供大家参考。具体如下:UCWeb的菜单看起来不错,自己模仿做一个,思