前言
Apache Maven 3.5.0org.codehaus.mojo:versions-maven-plugin 2.5/versions-maven-plugin/index.html创建项目
mvn -B archetype:generate \-DarchetypeGroupId=org.apache.maven.archetypes \-DgroupId=com.mycompany.app \-DartifactId=my-app
windows 用这个:
mvn -B archetype:generate ^-DarchetypeGroupId=org.apache.maven.archetypes ^-DgroupId=com.mycompany.app ^-DartifactId=my-app
查看版本号
cmd> mvn help:evaluate -Dexpression=project.version -q -DforceStdout1.0-SNAPSHOT
添加插件versions-maven-plugin
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mycompany.app</groupId><artifactId>my-app</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><name>my-app</name><url></url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>versions-maven-plugin</artifactId><version>2.5</version><configuration><generateBackupPoms>false</generateBackupPoms></configuration></plugin></plugins></build></project>
修改版本号
cmd> mvn versions:set -DnewVersion=1.1[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building my-app 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- versions-maven-plugin:2.5:set (default-cli) @ my-app ---[INFO] Searching for local aggregator root...[INFO] Local aggregation root: D:\test\my-app[INFO] Processing change of com.mycompany.app:my-app:1.0-SNAPSHOT -> 1.1[INFO] Processing com.mycompany.app:my-app[INFO]Updating project com.mycompany.app:my-app[INFO] from version 1.0-SNAPSHOT to 1.1[INFO][INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 1.917 s[INFO] Finished at: -01-15T17:05:54+08:00[INFO] Final Memory: 11M/155M[INFO] ------------------------------------------------------------------------
再查看版本号
cmd> mvn help:evaluate -Dexpression=project.version -q -DforceStdout1.1
嗯,版本号已经修改完了。
单模块项目用这个修改版本号没啥优势。当项目为多模块的项目时,用这个修改版本号较为方便。
多模块项目修改版本号
比如,有下面这样结构的继承关系多模块项目:
my-apps│ └─pom.xml(1)├─my-app-child1│ └─pom.xml (2)└─my-app-child2└─pom.xml (3)
在(1)pom.xml中添加versions-maven-plugin:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.mycompany.app</groupId><artifactId>my-apps</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><name>my-apps</name><url></url><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>versions-maven-plugin</artifactId><version>2.5</version><configuration><generateBackupPoms>false</generateBackupPoms></configuration></plugin></plugins></build><modules> <module>my-app-child1</module><module>my-app-child2</module></modules></project>
(2)pom.xml、(3)pom.xml不需要添加该插件。
使用mvn versions:set -DnewVersion=1.1
命令可以将3个项目(1个父项目和2个子项目)的版本号一次性修改为1.1。
cmd> mvn versions:set -DnewVersion=1.1[INFO] Scanning for projects...[INFO] ------------------------------------------------------------------------[INFO] Reactor Build Order:[INFO][INFO] my-apps[INFO] my-app-child1[INFO] my-app-child2[INFO][INFO] ------------------------------------------------------------------------[INFO] Building my-apps 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- versions-maven-plugin:2.5:set (default-cli) @ my-apps ---[INFO] Searching for local aggregator root...[INFO] Local aggregation root: D:\test\my-apps[INFO] Processing change of com.mycompany.app:my-apps:1.0-SNAPSHOT -> 1.1[INFO] Processing com.mycompany.app:my-apps[INFO]Updating project com.mycompany.app:my-apps[INFO] from version 1.0-SNAPSHOT to 1.1[INFO][INFO] Processing com.mycompany.app:my-app-child1[INFO]Updating parent com.mycompany.app:my-apps[INFO] from version 1.0-SNAPSHOT to 1.1[INFO]Updating project com.mycompany.app:my-app-child1[INFO] from version 1.0-SNAPSHOT to 1.1[INFO][INFO] Processing com.mycompany.app:my-app-child2[INFO]Updating parent com.mycompany.app:my-apps[INFO] from version 1.0-SNAPSHOT to 1.1[INFO]Updating project com.mycompany.app:my-app-child2[INFO] from version 1.0-SNAPSHOT to 1.1[INFO][INFO] ------------------------------------------------------------------------[INFO] Reactor Summary:[INFO][INFO] my-apps ............................................ SUCCESS [ 1.760 s][INFO] my-app-child1 ...................................... SKIPPED[INFO] my-app-child2 ...................................... SKIPPED[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------[INFO] Total time: 2.059 s[INFO] Finished at: -01-15T18:27:09+08:00[INFO] Final Memory: 13M/220M[INFO] ------------------------------------------------------------------------
另,如果没有继承关系时(多模块的聚合项目),需要注意一下2点:
版本 >= 2.5添加参数 processAllModules=true (默认值是false)
命令如下:
mvn versions:set -DprocessAllModules=true -DnewVersion=1.1