一句话概括,这个插件是用来修改pom的版本的,几乎是所有的版本号,比如工程,依赖和插件等等。
那么这个插件有什么用?
之前介绍过maven的版本管理经验,所有依赖都有一个root-pom来统一管理。
这样的话,每发一个release的包,其实就应该同时也修改了root-pom中这个包的版本号,这样,其他工程才能依赖到最新的代码。那么修改root-pom版本号的任务就可以交给versions插件。
这个可以放在jenkins中作为一个post-action来处理。每构建一个release包就触发一次root-pom修改。
下面介绍下常用命令:
1. 修改本工程的版本号:
mvn versions:set -DnewVersion=xxx
2. 回滚:
versions的任何目标,最终都会先记录一个pom.xml.versionsBackup文件暂存改动,可以使用
mvn versions:revert来回滚,这样就会恢复成pom.xml.versionsBackup文件的样子同时删除pom.xml.versionsBackup。
当然也可以mvn versions:commit来提交修改,删除pom.xml.versionsBackup文件。
3. 修改依赖的版本:
- versions:use-next-releases searches the pom for all non-SNAPSHOT versions which have been a newer release and replaces them with the next release version.
- versions:use-latest-releases searches the pom for all non-SNAPSHOT versions which have been a newer release and replaces them with the latest release version.
- versions:use-next-snapshots searches the pom for all non-SNAPSHOT versions which have been a newer -SNAPSHOT version and replaces them with the next -SNAPSHOT version.
- versions:use-latest-snapshots searches the pom for all non-SNAPSHOT versions which have been a newer -SNAPSHOT version and replaces them with the latest -SNAPSHOT version.
- versions:use-next-versions searches the pom for all versions which have been a newer version and replaces them with the next version.
- versions:use-latest-versions searches the pom for all versions which have been a newer version and replaces them with the latest version.
其实主要就这六个。前四个只会修改pom中的release依赖版本号,修改为下一个release或者snapshot。后两个会修改所有的依赖为下一个或者最新的版本号。
注意2.0版本的下一个snapshot为2.1-SNAPSHOT而非3.0-SNAPSHOT。
当然可以指定修改特定的依赖的版本号,通过添加-Dincludes=group:artifact即可,并且支持通配符。
另外,默认会修改dependency和dependencyMangement的版本号,可以加参数限定:
mvn versions:use-next-releases -DprocessDependencies=false
mvn versions:use-next-releases -DprocessDependencyManagement=false
参考至:http://www.mojohaus.org/versions-maven-plugin/examples/advancing-dependency-versions.html
--------------------------------------------------------------
补充:
还有一个很有用的点,可以使用set-property目标来修改pom中定义的property。这个在修改root-pom中的依赖版本会很有用。因为一般root-pom中的依赖的版本都是通过property来定义的,这样通过修改property就能修改依赖版本了,更新一个工程之后修改root-pom会很有用。
例子:
<dependency>
<groupId>com.liyao</groupId>
<artifactId>libJar</artifactId>
<version>${lib.version}</version>
</dependency>
<lib.version>1.0-SNAPSHOT</lib.version>
mvn versions:set-property -Dproperty=lib.version -DnewVersion=1.1-SNAPSHOT