Maven多模块版本规范

背景

无论是支付端还是金融端的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:

  1. 交互式,输入命令后会弹出提醒要求设置新的版本号。

    mvn -DgenerateBackupPoms= false  versions:set
    ...
    ...
    Enter the  new  version to set  1.0 . 0 -SNAPSHOT: :  1.0 . 0
  2. 非交互式,直接在命令行参数执行新的版本号

    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配置

parent_pom.xml
< 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配置

submodule_pom.xml
< 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 >
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Maven模块脚手架搭建的步骤如下: 1. 创建一个 Maven 父级项目作为容器,使用 Maven 命令创建: ```bash mvn archetype:generate -DgroupId=com.example -DartifactId=parent -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ``` 2. 在父级项目下创建多个子模块,每个子模块负责不同的功能模块,使用 Maven 命令创建: ```bash mvn archetype:generate -DgroupId=com.example -DartifactId=module1 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false mvn archetype:generate -DgroupId=com.example -DartifactId=module2 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false ... ``` 3. 在每个子模块的 pom.xml 文件中配置依赖和插件,如数据库驱动、Spring 框架等,示例: ```xml <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.3.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.2.2.RELEASE</version> </plugin> </plugins> </build> ``` 4. 在父级项目的 pom.xml 文件中使用 <modules> 标签将所有子模块引入,形成一个完整的项目,示例: ```xml <modules> <module>module1</module> <module>module2</module> ... </modules> ``` 5. 使用 Maven 命令进行编译、打包、部署等操作,如 mvn compile、mvn package、mvn deploy 等。 需要注意的是,Maven模块脚手架搭建需要考虑模块之间的依赖关系和代码复用,同时也需要遵循良好的设计原则和规范,以提高项目的可维护性和可扩展性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值