使用mvn versions:set更新版本失败,版本继承自父POM
问题:使用Maven versions插件更新版本时报错"Project version is inherited from parent"
在CI/CD脚本中执行mvn versions:set -DnewVersion=<新版本号>时触发错误,错误信息如下:
Failed to execute goal org.codehaus.mojo:versions-maven-plugin:2.3:set (default-cli) on project top: Project version is inherited from parent. -> [Help 1]
父POM文件内容
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com</groupId> <artifactId>parent</artifactId> <version>2023.0.0-SNAPSHOT</version> <relativePath>****/pom.xml</relativePath> </parent> <artifactId>top</artifactId> <packaging>pom</packaging> <name> Suite</name> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <maven.compiler.release>17</maven.compiler.release> <timestamp>${maven.build.timestamp}</timestamp> </properties> <modules> <module>ab_initio</module> <module>ab_initio/module</module> <module>autoactions</module> <module>cluster-access</module> <module>common</module> <module>config</module> </modules> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <id>parse-version</id> <goals> <goal>parse-version</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target</outputDirectory> <resources> <resource> <directory>${basedir}/src/main/other</directory> <includes> <include>build.******.env.sh</include> </includes> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
修复方案
切换到根父POM目录执行命令
你的top项目版本继承自更高层级的com:parent,不能直接在top目录执行versions:set。需要找到com:parent对应的POM文件所在目录,在该目录下执行版本更新命令,这样会自动同步所有继承该父POM的子项目版本。添加参数更新当前项目的父版本引用
如果目标是更新top项目中对父POM的版本引用,同时更新自身及子项目版本,执行以下命令:mvn versions:set -DnewVersion=<新版本号> -DupdateParentVersions=true -DallowSnapshots=true-DupdateParentVersions=true:允许修改项目中<parent>节点的版本号-DallowSnapshots=true:如果新版本是SNAPSHOT类型,必须添加此参数
升级versions插件版本
你使用的2.3版本插件较旧,存在兼容性问题,建议升级到最新稳定版(如2.16.2),执行命令时指定版本:mvn org.codehaus.mojo:versions-maven-plugin:2.16.2:set -DnewVersion=<新版本号>同步子项目版本
确保所有子项目的版本都是继承自top项目,无硬编码版本。如果子项目有独立版本,可执行mvn versions:update-child-modules命令同步子项目版本至父项目版本。
内容的提问来源于stack exchange,提问作者karthik
相关产品推荐
相关产品推荐

