需求描述

在项目迭代过程中,需要为不同的版本号打上 Tag,对于发行的稳定版本则需要上传到指定的仓库。手动操作则过于繁琐,且无法统一 Tag 的样式,因此需要一款实现自动的可配置的发行工具。

解决方案

使用 maven-release-plugin 插件,其提供了自动追赠版本号、自动切换 snapshots 和 releases 版本、以及自动打 Tag 与版本发行等功能。

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!-- 父模块 pom.xml -->
<!-- git 地址 -->
<scm>
<connection>scm:git:url</connection>
<developerConnection>scm:git:url</developerConnection>
<tag>HEAD</tag>
</scm>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<username>scm 用户名</username>
<!-- 对于 github 用户,(当密码无效时)可能需要 repo token 权限(代替密码) -->
<password>scm 密码</password>
<scmCommentPrefix>[提交信息的前缀]</scmCommentPrefix>
<!-- 子模块的版本号与父模块是否一致 -->
<autoVersionSubmodules>true</autoVersionSubmodules>
<!-- tag 的格式,如 v1.0.0 -->
<tagNameFormat>v@{project.version}</tagNameFormat>
<!-- 跳过测试以及生成 javadoc 文档(注释不规范时会报错)-->
<arguments>-DskipTests -DuseReleaseProfile=false</arguments>
<!-- tag 的路径,gitee 路径,主要是快 -->
<tagBase>https://gitee.com/{用户名}/{项目名}/tags</tagBase>
</configuration>
</plugin>

<!-- 配置发行路径,这里配置的是阿里云效的私有仓库 -->
<!-- 需要在 settings.xml 中配置 server 的用户名和密码,且指定的 id 需要和下面的一致 -->
<distributionManagement>
<repository>
<id>rdc-releases</id>
<url>https://packages.aliyun.com/maven/repository/2046382-release-Kot3ui/</url>
</repository>
<snapshotRepository>
<id>rdc-snapshots</id>
<url>https://packages.aliyun.com/maven/repository/2046382-snapshot-sZVDXF/</url>
</snapshotRepository>
</distributionManagement>

<!-- settings.xml -->
<servers>
<server>
<!-- id 需要和上面一致 -->
<id>rdc-releases</id>
<username>用户名</username>
<password>密码</password>
</server>
<server>
<id>rdc-snapshots</id>
<username>用户名</username>
<password>密码</password>
</server>
</servers>

正式发行一定要配置 distributionManagement 且要注意授权信息(账号、密码)是否填写且正确,否则授权无法通过产生 401 异常。

使用

1
2
3
4
5
6
# 预备发行:打 Tag,自动切换快照版本
mvn release:prepare
# 将已有的 Tag 发行到指定仓库
mvn release:perform
# 异常时回滚
mvn release:rollback

在 idea 可以通过可视化插件使用,需要注意的时,正式发行前一定要先进行预备发行。且每次打 Tag 时必须要求 git 是最新提交,正式发行使用的是上次 Tag(历史提交) 中的 pom,因此每次修改需要先提交到 git。
image.png

注意事项

  1. 提交时出现 remote: Invalid username or password,确保自己的登录信息没有错,则尝试使用 repo token 来替换密码;
  2. 发布 prepare 之后,会产生各个模块的 pom 备份文件,最好在提交之前加入 .gitignore;

评论