mvn如何发布SNAPSHOT项目

问题

项目开发过程中,版本号是 x.y.x-SNAPSHOT,如何能够自动以release版本号发布项目,并自动更新成新的SNAPSHOT版本号?

步骤

使用插件:Maven Release Plugin

配置pom.xml

需要同时配置scm(本文用git)和distributionManagement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<scm>
<connection>scm:git:http://git.crazy1984.com/sale_java/sale-boot.git</connection>
<tag>HEAD</tag>
</scm>
<distributionManagement>
<repository>
<id>releases</id>
<url>http://repo.crazy1984.com/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://repo.crazy1984.com/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>

mvn release:prepare

发布前准备:变更pom.xml中的版本为release版本号,并打scm tag。

会顺序执行以下步骤:

  • Check that there are no uncommitted changes in the sources
  • Check that there are no SNAPSHOT dependencies
  • Change the version in the POMs from x-SNAPSHOT to a new version (you will be prompted for the versions to use)
  • Transform the SCM information in the POM to include the final destination of the tag
  • Run the project tests against the modified POMs to confirm everything is in working order
  • Commit the modified POMs (git commit)
  • Tag the code in the SCM with a version name (this will be prompted for)
  • Bump the version in the POMs to a new value y-SNAPSHOT (these values will also be prompted for)
  • Commit the modified POMs (git commit)

mvn release:perform

发布:scm检出最新release版本的tag,执行mvn deploy。

示例

  • 原开发版本:1.1.0-SNAPSHOT
  • 发布版本:1.1.0
  • 新开发版本:1.2.0-SNAPSHOT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ mvn release:prepare
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------< com.crazy1984:xfund-archetype >-------------------
[INFO] Building xfund-archetype 1.1.0-SNAPSHOT
[INFO] --------------------------[ maven-archetype ]---------------------------
[INFO]
[INFO] --- maven-release-plugin:2.5.3:prepare (default-cli) @ xfund-archetype ---
[INFO] Verifying that there are no local modifications...
[INFO] ignoring changes on: **/pom.xml.releaseBackup, **/pom.xml.next, **/pom.xml.tag, **/pom.xml.branch, **/release.properties, **/pom.xml.backup
[INFO] Executing: /bin/sh -c cd /Users/zhangjy/git/git.crazy1984.com/sale_java/sale-boot/archetype && git rev-parse --show-toplevel
[INFO] Working directory: /Users/zhangjy/git/git.crazy1984.com/sale_java/sale-boot/archetype
[INFO] Executing: /bin/sh -c cd /Users/zhangjy/git/git.crazy1984.com/sale_java/sale-boot/archetype && git status --porcelain .
[INFO] Working directory: /Users/zhangjy/git/git.crazy1984.com/sale_java/sale-boot/archetype
[WARNING] Ignoring unrecognized line: ?? archetype/release.properties
[INFO] Checking dependencies and plugins for snapshots ...
What is the release version for "xfund-archetype"? (com.crazy1984.sale:xfund-archetype) 1.1.0: :
What is SCM release tag or label for "xfund-archetype"? (com.crazy1984.sale:xfund-archetype) xfund-archetype-1.1.0: :
What is the new development version for "xfund-archetype"? (com.crazy1984.sale:xfund-archetype) 1.1.1-SNAPSHOT: : 1.2.0-SNAPSHOT
...

$ mvn release:perform
$ mvn release:clean