利用maven怎么动态修改版本号

方案一:mvn -Denv.project.version=1.0-env

注:env.project.version为自定参数变量,env为dev或者test

1、在maven项目的父级pom设置变量,形如下

 <groupId>org.example</groupId>
  <artifactId>demo-parent</artifactId>
  <packaging>pom</packaging>
  <version>${env.project.version}</version>

  <properties>
    <env.project.version>1.0-SNAPSHOT</env.project.version>
  </properties>

 

2、修改其子模块pom

 <parent>
    <artifactId>demo-parent</artifactId>
    <groupId>org.example</groupId>
    <version>${env.project.version}</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>module-biz</artifactId>
 <parent>
    <artifactId>demo-parent</artifactId>
    <groupId>org.example</groupId>
    <version>${env.project.version}</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>module-api</artifactId>

 

3、执行如下命令

mvn clean package -Denv.project.version=1.0-dev -DskipTests

如果要打包发布到私有仓库,则执行

mvn clean deploy-Denv.project.version=1.0-dev -DskipTests

4、验证

利用maven怎么动态修改版本号

利用maven怎么动态修改版本号

从截图,可以看出达到预期的效果

方案二 maven的profile+自定参数变量

1、在maven项目的父级pom设置变量,并添加profile,形如下

  <properties>
        <env.project.version>1.0-SNAPSHOT</env.project.version>
      </properties>
    <profiles>
    <profile>
      <id>dev</id>
      <properties>
        <env.project.version>1.0-dev</env.project.version>
      </properties>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
    </profile>
    <profile>
      <id>test</id>
      <properties>
        <env.project.version>1.0-test</env.project.version>
      </properties>
    </profile>
  </profiles>

2、修改其子模块pom

 
<parent>
    <artifactId>demo-parent</artifactId>
    <groupId>org.example</groupId>
    <version>${env.project.version}</version>
  </parent>
  <modelVersion>4.0.0</modelVersion>

  <artifactId>module-biz</artifactId>

 

3、执行如下命令

mvn clean package -Ptest -DskipTests

如果要打包发布到私有仓库,则执行

mvn clean deploy -Ptest -DskipTests

注:不指定-P默认为dev

4、验证

利用maven怎么动态修改版本号

利用maven怎么动态修改版本号

从截图,可以看出达到预期的效果

方案三(推荐) mvn versions:set -DnewVersion=1.0-dev

注:使用该命令,项目无需做任何变动。

直接执行命令

mvn versions:set -DnewVersion=1.0-dev

 

如果要发布到私仓,此时要分两次命令执行,命令如下

mvn versions:set -DnewVersion=1.0-dev
mvn clean deploy -DskipTests

 

利用maven怎么动态修改版本号

此时查看idea,会发现

利用maven怎么动态修改版本号

项目版本号已经发生改变,且产生一个pom.xml.versionsBackup文件,这个文件是用来回退版本用的,其内容如下

利用maven怎么动态修改版本号

如果确认没问题,则可以执行你本来要操作的步骤,比如打包或者发布,形如下命令

mvn clean package/deploy -DskipTests

 

利用maven怎么动态修改版本号

从截图,可以看出达到预期的效果

如果有问题想回退版本,则执行

mvn versions:revert

进行回退。不过能成功执行该步骤的前提是没有执行

mvn versions:commit

且存在pom.xml.versionsBackup文件文件

如果不想产生pom.xml.versionsBackup文件,则可以在父pom配置如下插件

 <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>versions-maven-plugin</artifactId>
        <version>2.8.1</version>
        <configuration>
          <generateBackupPoms>false</generateBackupPoms>
        </configuration>
      </plugin>
    </plugins>
  </build>

 

指定generateBackupPoms为false

参数介绍

利用maven怎么动态修改版本号

 

转自 https://www.yisu.com/zixun/370659.html

 

posted @ 2023-07-18 10:23  君子笑而不语  阅读(1469)  评论(0编辑  收藏  举报