背景
无论是支付端还是金融端的Java项目,大部分都是多模块的Maven项目,同一项目下的不同模块版本比较混乱,发布到Maven私服里的Jar包版本也很混乱,本规范目的是要统一Jar包版本规范。
规范
1、同一项目中所有模块版本保持一致
2、子模块统一继承父模块的版本
3、统一在顶层模块Pom的<dependencyManagement/>节中定义所有子模块的依赖版本号,子模块中添加依赖时不要添加版本号
4、开发测试阶段使用SNAPSHOT
5、生产发布使用RELEASE
6、新版本迭代只修改顶层POM中的版本
流程
1、研发开发新功能,假设当前开发的POM版本都是 1.0.0-SNAPSHOT
2、封版发布到生产之前,将版本号修改为RELEASE版本。在Maven根模块下执行以下命令,使用versions-maven-plugin:
-
交互式,输入命令后会弹出提醒要求设置新的版本号。
mvn
-DgenerateBackupPoms= false versions:set
...
...
Enter
the new version
to set 1.0 . 0 -SNAPSHOT:
: 1.0 . 0
|
-
非交互式,直接在命令行参数执行新的版本号
mvn
-DnewVersion= 1.0 . 0 -DgenerateBackupPoms= false versions:set
|
该命令在项目根目录执行,成功执行后会修改根模块以及所有子模块里的parent的版本。
3、提交修改版本后的源码,按要求打Tag,并提交Tag给配管发布
4、Jenkins上在构建服务实现模块的时候,通过versions:set设置服务实现模块的版本号为SCM的Tag号,命令如下:(假设子模块submodule为服务模块,一般是WEB服务或者WS服务)
mvn
-f submodule2/pom.xml -DnewVersion=${SCM_TAG} clean versions:set
|
依赖
versions-maven-plugin:2.1,新版本会报错,请使用2.1版本。
< plugin >
< groupId >org.codehaus.mojo</ groupId >
< artifactId >versions-maven-plugin</ artifactId >
< version >2.1</ version >
</ plugin >
|
示例
父模块的POM配置
< groupId >com.haier.hairy</ groupId >
< artifactId >maven-release-test</ artifactId >
< packaging >pom</ packaging >
< version >3.2.3-SNAPSHOT</ version >
< modules >
< module >submodule1</ module >
< module >submodule2</ module >
</ modules >
< properties >
< hry-cxf-common.version >0.0.2</ hry-cxf-common.version >
</ properties >
< dependencyManagement >
< dependencies >
< dependency >
< groupId >com.haier.hairy</ groupId >
< artifactId >hry-cxf-common</ artifactId >
< version >${hry-cxf-common.version}</ version >
</ dependency >
< dependency >
< groupId >com.haier.hairy</ groupId >
< artifactId >maven-release-test.submodule1</ artifactId >
< version >${project.parent.version}</ version >
</ dependency >
< dependency >
< groupId >com.haier.hairy</ groupId >
< artifactId >maven-release-test.submodule2</ artifactId >
< version >${project.parent.version}</ version >
</ dependency >
</ dependencies >
</ dependencyManagement >
< build >
< pluginManagement >
< plugins >
< plugin >
< groupId >org.apache.maven.plugins</ groupId >
< artifactId >maven-release-plugin</ artifactId >
< version >2.5.3</ version >
< configuration >
< preparationGoals >clean
install</ preparationGoals >
</ configuration >
</ plugin >
< plugin >
< groupId >org.codehaus.mojo</ groupId >
< artifactId >versions-maven-plugin</ artifactId >
< version >2.1</ version >
</ plugin >
</ plugins >
</ pluginManagement >
</ build >
|
子模块的POM配置
< parent >
< artifactId >maven-release-test</ artifactId >
< groupId >com.haier.hairy</ groupId >
< version >3.2.3-SNAPSHOT</ version >
</ parent >
< modelVersion >4.0.0</ modelVersion >
< packaging >jar</ packaging >
< artifactId >maven-release-test.submodule1</ artifactId >
< dependencies >
< dependency >
< groupId >com.haier.hairy</ groupId >
< artifactId >hry-cxf-common</ artifactId >
</ dependency >
< dependency >
< groupId >com.haier.hairy</ groupId >
< artifactId >maven-release-test.submodule2</ artifactId >
</ dependency >
</ dependencies >
< build >
< plugins >
< plugin >
< groupId >org.codehaus.mojo</ groupId >
< artifactId >versions-maven-plugin</ artifactId >
</ plugin >
</ plugins >
</ build >
|