编程知识 cdmana.com

How to dynamically and uniformly modify the version number of maven

Preface

Recently, business development departments share the same development environment and test environment maven Private warehouse , What led to their development environment API Package and test environment API The package is covered . So they put forward a demand to our department , I hope we can help them achieve or provide such a solution , When the project is built automatically , The version number of the project can change with the environment . For example, the development environment , Then the API The package version looks like 1.0-dev, If it's a test environment , Then the API The version looks like 1.0-test

Demonstration of examples

The project hierarchy is as follows

image.png

Scheme 1 :mvn -Denv.project.version=1.0-env

notes : env.project.version It is a self-defined parameter variable ,env by dev perhaps test

1、 stay maven The parent of the project pom Set a variable , The shape is as follows

  <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、 Modify its submodules 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、 Execute the following command

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

If you want to package and publish to a private Repository , execute

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

4、 verification

image.png image.png

From the screenshot , It can be seen that the expected effect is achieved

Option two maven Of profile+ Self defined parameter variable

1、 stay maven The parent of the project pom Set a variable , And add profile, The shape is as follows

		<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、 Modify its submodules 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、 Execute the following command

mvn clean package -Ptest -DskipTests

If you want to package and publish to a private Repository , execute

mvn clean deploy -Ptest -DskipTests

** notes :** Don't specify -P The default is dev

4、 verification

image.png  Insert picture description here

From the screenshot , It can be seen that the expected effect is achieved

Option three ( recommend ) mvn versions:set -DnewVersion=1.0-dev

notes : Use this command , There is no need to make any changes to the project .

Direct command execution

mvn versions:set -DnewVersion=1.0-dev

If you want to publish to private warehouse , In this case, the command should be executed twice , The order is as follows

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

image.png

At this point to see idea, Will find

image.png

The project number has changed , And produce a pom.xml.versionsBackup file , This file is used to rollback the version , It reads as follows

image.png

If it's ok , Then you can perform the steps you were supposed to do , Like packaging or publishing , The following is the command

mvn clean package/deploy -DskipTests

image.png

From the screenshot , It can be seen that the expected effect is achieved

If you have a problem and want to go back to the version , execute

mvn versions:revert

Back off . However, the premise of successful implementation of this step is that it has not been implemented

mvn versions:commit

And there are pom.xml.versionsBackup File file

If you don't want to produce pom.xml.versionsBackup file , In the father pom Configure the following plug-ins

 <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>

Appoint generateBackupPoms by false

Parameter Introduction

image.png

More details can be found on the official website

http://www.mojohaus.org/versions-maven-plugin/index.html

summary

From the business scenario of this example , Although the effect can also be achieved through user-defined variables , But if there are more than one sub module , It may be easy to make mistakes . So I recommend mvn versions:set -DnewVersion=1.0-dev This is the way to modify

版权声明
本文为[Linyb road to Geek]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225095555722f.html

Scroll to Top