如何在Maven构建中通过versions插件批量设置/更新多属性版本
解决方法
针对你的需求,有两种可行的方式来一次性更新多个版本属性:
方式一:在单个命令中多次执行versions:set-property目标
Maven 3.3.1及以上版本支持给同一个插件的多个目标执行分配唯一ID,通过@标识区分,这样可以在一条命令里完成多个属性的设置。命令格式如下:
mvn versions:set-property@set-core versions:set-property@set-extra \ -Dproperty@set-core=myartifact.version -DnewVersion@set-core=10.1.1 \ -Dproperty@set-extra=mysecondArtifact.version -DnewVersion@set-extra=10.5.1
这里用@set-core和@set-extra作为两次执行的ID,分别为对应的属性指定新版本值。
方式二:使用versions:update-properties批量更新
如果不想每次写长命令,可以在POM中配置versions-maven-plugin的update-properties目标,预先定义要更新的属性和版本:
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>versions-maven-plugin</artifactId> <version>2.16.0</version> <executions> <execution> <id>batch-update-properties</id> <goals> <goal>update-properties</goal> </goals> <configuration> <properties> <property> <name>myartifact.version</name> <value>10.1.1</value> </property> <property> <name>mysecondArtifact.version</name> <value>10.5.1</value> </property> </properties> <!-- 如果需要强制覆盖已有的属性值,添加下面的配置 --> <forceUpdate>true</forceUpdate> </configuration> </execution> </executions> </plugin> </plugins> </build>
配置完成后,执行以下命令即可完成批量更新:
mvn versions:update-properties@batch-update-properties
如果需要临时修改版本,也可以通过命令行覆盖配置里的版本值:
mvn versions:update-properties@batch-update-properties \ -Dproperties[0].name=myartifact.version -Dproperties[0].value=10.2.0 \ -Dproperties[1].name=mysecondArtifact.version -Dproperties[1].value=10.6.0
注意事项
你之前的命令里存在拼写错误:-Dpropery应该是-Dproperty,这也可能导致之前的执行出现异常。
内容的提问来源于stack exchange,提问作者Indra Yadav
相关产品推荐
相关产品推荐

