#maven 的版本升级,切换

[toc]

  1. 首先描述一下项目的pom的版本 父pom
<groupId>com.dxhy.jdService</groupId>
<artifactId>insert-card</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>

某子pom

<parent>
    <artifactId>insert-card</artifactId>
    <groupId>com.dxhy.jdService</groupId>
    <version>1.0.0-SNAPSHOT</version>
</parent>

使用案例

案例描述

使用场景:

在版本控制中准备将测试成功的分支合并到master分支中,将快照版本替换为正式版本。发布正式版本。 然后将版本升级为下一个小版本。提交到开发者分支。发布新版本的开发版本。

使用需求点:
  1. 父子pom中版本的修改
  2. 配置文件中对子版本的互相调用的版本修改。

一、发布正式版本

1. 将原来 1.0.0-SNAPSHOT 升级为 1.0.0正式版本
mvn versions:set -DnewVersion=1.0.0 -DallowSnopShot=true

-DnewVersion=1.0.0 为设定新版本 -DallowSnopShot=true 为运行快照版本执行

2. 执行提交命令
mvn versions:commit
3. 执行修改pom文件中某properties 的版本号
mvn versions:update-properties -DallowSnatshots=true -Dincludes=com.dxhy.jdService:*

-DallowSnopShot=true 为运行快照版本执行

-Dincludes=com.dxhy.jdService:* 为只允许groupId为com.dxhy.jdService的jar使用的配置文件更改。

4. 执行提交命令
mvn versions:commit

二、升级为高版本快照版本

1. 将原来 1.0.0-SNAPSHOT 升级为 1.0.1-SNAPSHOT
mvn versions:set -DnewVersion=1.0.1-SNAPSHOT

因为当前版本为正式版本,所以不用写运行快照版本也行;

2. 执行提交命令
mvn versions:commit
3. 执行修改pom文件中某properties 的版本号
mvn versions:update-properties -DallowSnatshots=true -Dincludes=com.dxhy.jdService:*
4. 执行提交命令
mvn versions:commit

备份:

1. 执行提交命令过程
赵睿@zhaorui MINGW64 /d/workspace/com/dxhy/jdService/insert-card (master)
$ mvn versions:commit
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] insert-card
[INFO] insert-card-core
[INFO] insert-card-accepter
[INFO] insert-card-processer
[INFO] insert-card-sender
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:commit (default-cli) @ insert-card ---
[INFO] Accepting all changes to D:\workspace\com\dxhy\jdService\insert-card\pom.xml
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-core 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:commit (default-cli) @ insert-card-core ---
[INFO] Accepting all changes to D:\workspace\com\dxhy\jdService\insert-card\insert-card-core\pom.xml
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-accepter 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:commit (default-cli) @ insert-card-accepter ---
[INFO] Accepting all changes to D:\workspace\com\dxhy\jdService\insert-card\insert-card-accepter\pom.xml
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-processer 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:commit (default-cli) @ insert-card-processer ---
[INFO] Accepting all changes to D:\workspace\com\dxhy\jdService\insert-card\insert-card-processer\pom.xml
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-sender 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:commit (default-cli) @ insert-card-sender ---
[INFO] Accepting all changes to D:\workspace\com\dxhy\jdService\insert-card\insert-card-sender\pom.xml
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] insert-card ........................................ SUCCESS [  1.728 s]
[INFO] insert-card-core ................................... SUCCESS [  0.004 s]
[INFO] insert-card-accepter ............................... SUCCESS [  0.003 s]
[INFO] insert-card-processer .............................. SUCCESS [  0.004 s]
[INFO] insert-card-sender ................................. SUCCESS [  0.012 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.194 s
[INFO] Finished at: 2017-05-10T15:28:56+08:00
[INFO] Final Memory: 19M/227M
[INFO] ------------------------------------------------------------------------
2. 执行修改版本的命令过程
赵睿@zhaorui MINGW64 /d/workspace/com/dxhy/jdService/insert-card (master)
$ mvn versions:set -DnewVersion=1.0.0 -DallowSnopShot=true
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] insert-card
[INFO] insert-card-core
[INFO] insert-card-accepter
[INFO] insert-card-processer
[INFO] insert-card-sender
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:set (default-cli) @ insert-card ---
[INFO] Searching for local aggregator root...
[INFO] Local aggregation root: D:\workspace\com\dxhy\jdService\insert-card
[INFO] Processing change of com.dxhy.jdService:insert-card:1.0.0-SNAPSHOT -> 1.0.0
[INFO] Processing com.dxhy.jdService:insert-card
[INFO]     Updating project com.dxhy.jdService:insert-card
[INFO]         from version 1.0.0-SNAPSHOT to 1.0.0
[INFO]
[INFO] Processing com.dxhy.jdService:insert-card-accepter
[INFO]     Updating parent com.dxhy.jdService:insert-card
[INFO]         from version 1.0.0-SNAPSHOT to 1.0.0
[INFO]
[INFO] Processing com.dxhy.jdService:insert-card-core
[INFO]     Updating parent com.dxhy.jdService:insert-card
[INFO]         from version 1.0.0-SNAPSHOT to 1.0.0
[INFO]
[INFO] Processing com.dxhy.jdService:insert-card-processer
[INFO]     Updating parent com.dxhy.jdService:insert-card
[INFO]         from version 1.0.0-SNAPSHOT to 1.0.0
[INFO]
[INFO] Processing com.dxhy.jdService:insert-card-sender
[INFO]     Updating parent com.dxhy.jdService:insert-card
[INFO]         from version 1.0.0-SNAPSHOT to 1.0.0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] insert-card ........................................ SUCCESS [  1.302 s]
[INFO] insert-card-core ................................... SKIPPED
[INFO] insert-card-accepter ............................... SKIPPED
[INFO] insert-card-processer .............................. SKIPPED
[INFO] insert-card-sender ................................. SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.115 s
[INFO] Finished at: 2017-05-10T15:26:37+08:00
[INFO] Final Memory: 20M/227M
[INFO] -----------------------------------------------
3. 执行更改配置文件的命令过程
赵睿@zhaorui MINGW64 /d/workspace/com/dxhy/jdService/insert-card (master)
$ mvn versions:update-properties -DallowSnatshots=true -Dincludes=com.dxhy.jdService:*
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] insert-card
[INFO] insert-card-core
[INFO] insert-card-accepter
[INFO] insert-card-processer
[INFO] insert-card-sender
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:update-properties (default-cli) @ insert-card ---
[INFO] Not updating the property ${cxf.version} because it is used by artifact org.apache.cxf:cxf-rt-frontend-jaxws:jar:3.0.2 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO] Not updating the property ${mybatis.spring.boot.vesion} because it is used by artifact org.mybatis.spring.boot:mybatis-spring-boot-starter:jar:1.1.1 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO] Not updating the property ${commons.lang.version} because it is used by artifact commons-lang:commons-lang:jar:2.6 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO] Not updating the property ${commons.lang3.version} because it is used by artifact org.apache.commons:commons-lang3:jar:3.3.2 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO] Not updating the property ${druid.version} because it is used by artifact com.alibaba:druid:jar:1.0.29 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO] Not updating the property ${commons.io.version} because it is used by artifact commons-io:commons-io:jar:2.5 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO] Not updating the property ${tomcat.version} because it is used by artifact org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:7.0.77 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO] Updated ${project.version} from 1.0.0-SNAPSHOT to 1.0.0
[INFO] Not updating the property ${mysql.version} because it is used by artifact mysql:mysql-connector-java:jar:5.1.39 and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-core 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:update-properties (default-cli) @ insert-card-core ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-accepter 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:update-properties (default-cli) @ insert-card-accepter ---
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-processer 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:update-properties (default-cli) @ insert-card-processer ---
[INFO] Not updating the property ${openApi.sdk.version} because it is used by artifact com.dxhy.openApi:openApi-sdk:jar:0.0.2-SNAPSHOT and that artifact is not included in the list of  allowed artifacts to be updated.
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card-sender 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:update-properties (default-cli) @ insert-card-sender ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]
[INFO] insert-card ........................................ SUCCESS [  1.310 s]
[INFO] insert-card-core ................................... SUCCESS [  0.005 s]
[INFO] insert-card-accepter ............................... SUCCESS [  0.008 s]
[INFO] insert-card-processer .............................. SUCCESS [  0.016 s]
[INFO] insert-card-sender ................................. SUCCESS [  0.004 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.132 s
[INFO] Finished at: 2017-05-10T16:04:09+08:00
[INFO] Final Memory: 20M/227M
[INFO] ------------------------------------------------------------------------

Versions Maven Plugin 官方的一些插件介绍

Note: The first time any of the goals that modify the pom.xml file run, they will create a local backup copy pom.xml.versionsBackup. Subsequent modifications will leave this backup unchanged. The versions:commit goal will remove the backup copy, while the versions:revert goal will restore the backup copy. It is best practice to use a Source Code Management system and not rely on the pom.xml.versionsBackup files created by the versions-maven-plugin. The versions:commit and versions:revert goals are only a "Poor Man's SCM".

注意:首先任何修改pom.xml的指令执行,都创建一个本地备份的pom.xml.versionsBackup文件。随后的修改不会让这个备份被修改。

versions:commit 命令 会删除备份副本文件。

versions:revert 命令 会回复副本备份文件。

然后就是说最好是直接用scm直接源代码管理系统进行操作,而不是依靠通过versions-maven-plugin这个插件操作pom.xml.versionsBackup文件;versions:commitversions:revert恢复目标只是一个“匮乏的SCM”

versions:update-parent 升级父pom的包

执行命令之后,结果把我导入的spring-boot-starter-parent的版本升级了。

其他子命令:

mvn versions:update-parent "-DparentVersion=[14,16) 限定升级版本为14-16:

mvn versions:update-parent -DallowSnapshots=true

强制快照版本也可以升级

versions:revert 恢复上一个版本

versions:update-child-modules

mvn -N versions:update-child-modules

如果是快照,改不动,需要设定允许快照

mvn versions:update-child-modules  -DallowSnapshots=true

子版本会跟随父版本全部变化。

赵睿@zhaorui MINGW64 /d/workspace/com/dxhy/jdService/insert-card (release)
$ mvn -N versions:update-child-modules -DallowSnapshots=true
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building insert-card 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- versions-maven-plugin:2.2:update-child-modules (default-cli) @ insert-card ---
[INFO] Module: insert-card-accepter
[INFO]     parent was com.dxhy.jdService:insert-card:1.0.0
[INFO]     updated to com.dxhy.jdService:insert-card:1.0.0-SNAPSHOT
[INFO] Module: insert-card-core
[INFO]     parent was com.dxhy.jdService:insert-card:1.0.0
[INFO]     updated to com.dxhy.jdService:insert-card:1.0.0-SNAPSHOT
[INFO] Module: insert-card-processer
[INFO]     parent was com.dxhy.jdService:insert-card:1.0.0
[INFO]     updated to com.dxhy.jdService:insert-card:1.0.0-SNAPSHOT
[INFO] Module: insert-card-sender
[INFO]     parent was com.dxhy.jdService:insert-card:1.0.0
[INFO]     updated to com.dxhy.jdService:insert-card:1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.100 s
[INFO] Finished at: 2017-05-10T14:51:12+08:00
[INFO] Final Memory: 15M/155M
[INFO] ------------------------------------------------------------------------

versions:display-dependency-updates

可以查看jar包版本是不是最新的,是不是很牛逼啊

部分信息为:

[INFO]                                           1.1.2.RELEASE -> 1.2.0.RELEASE
[INFO]   org.springframework.restdocs:spring-restdocs-restassured ...
[INFO]                                           1.1.2.RELEASE -> 1.2.0.RELEASE
[INFO]   org.springframework.security.oauth:spring-security-oauth ...
[INFO]                                          2.0.13.RELEASE -> 2.1.0.RELEASE
[INFO]   org.springframework.security.oauth:spring-security-oauth2 ...
[INFO]                                          2.0.13.RELEASE -> 2.1.0.RELEASE
[INFO]   org.springframework.session:spring-session ...
[INFO]                                           1.3.0.RELEASE -> 1.3.1.RELEASE
[INFO]   org.springframework.session:spring-session-data-gemfire ...
[INFO]                                           1.3.0.RELEASE -> 1.3.1.RELEASE
[INFO]   org.springframework.session:spring-session-data-mongo ...
[INFO]                                           1.3.0.RELEASE -> 1.3.1.RELEASE
[INFO]   org.springframework.session:spring-session-data-redis ...
[INFO]                                           1.3.0.RELEASE -> 1.3.1.RELEASE
[INFO]   org.springframework.session:spring-session-hazelcast ...
[INFO]                                           1.3.0.RELEASE -> 1.3.1.RELEASE
[INFO]   org.springframework.session:spring-session-jdbc ...
[INFO]                                           1.3.0.RELEASE -> 1.3.1.RELEASE
[INFO]   org.springframework.social:spring-social-facebook-web ...
[INFO]                                                2.0.3.RELEASE -> 3.0.0.M1
[INFO]   org.thymeleaf:thymeleaf ............... 2.1.5.RELEASE -> 3.0.6.RELEASE
[INFO]   org.thymeleaf:thymeleaf-spring4 ....... 2.1.5.RELEASE -> 3.0.6.RELEASE
[INFO]   org.thymeleaf.extras:thymeleaf-extras-java8time ...
[INFO]                                           2.1.0.RELEASE -> 3.0.0.RELEASE
[INFO]   org.thymeleaf.extras:thymeleaf-extras-springsecurity4 ...
[INFO]                                           2.1.3.RELEASE -> 3.0.2.RELEASE
[INFO]   org.webjars:hal-browser ........................... 9f96c74 -> 3325375
[INFO]   org.webjars:webjars-locator ........................... 0.32 -> 0.32-1
[INFO]   org.xerial:sqlite-jdbc .............................. 3.15.1 -> 3.16.1
[INFO]   org.yaml:snakeyaml ...................................... 1.17 -> 1.18
[INFO]   xml-apis:xml-apis .................................... 1.4.01 -> 2.0.2
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO]

versions:display-plugin-updates

和上面那个相似,不过是检测maven 插件是不是最新的

versions:display-property-updates

查看属性有无最新版本?

versions:update-properties 和versions:update-property

versions:lock-snapshots 锁定快照

searches the pom for all -SNAPSHOT versions and replaces them with the current timestamp version of that -SNAPSHOT, e.g. -20090327.172306-4

versions:unlock-snapshots 解锁快照

versions:use-releases 将快照替换为正式版本

资料来源:

  1. 官网--Updating POM Versions
  2. 官网--Versions Maven Plugin