The method of modifying the version number of multiple modules in Maven project at the same time with one line command

Time:2020-3-8

Maven, a common project management tool for java development, can build and manage Java projects.

For many Java programmers, layered architecture is not new, at least the three-tier architecture of MVC is not new, and even someone said, “any problem in computer science can be solved by other layer of indirection.”

To layer in the code, it is better to create multiple module projects


maven-parent (Maven Project)
   |- maven-dao (Maven Module)
     |- pom.xml
   |- maven-service (Maven Module)
     |- pom.xml
   |- maven-view (Maven Module)
     |- pom.xml
   |- pom.xml

There are three modules in the above projects. Generally, Maven is used to manage the relationship between modules. Such as:

In the outermost pom.xml, define the following:


<modelVersion>4.0.0</modelVersion>

<artifactId>hollis-test</artifactId>
<groupId>com.hollis.lab</groupId>
<name>test</name>
<packaging>pom</packaging>
<version>1.0.0</version>
<modules>
    <module>maven-dao</module>
    <module>maven-service</module>
    <module>maven-view</module>
</modules>

Then define the following in each sub module:


<modelVersion>4.0.0</modelVersion>
<parent>
  <artifactId>hollis-test</artifactId>
  <groupId>com.hollis.lab</groupId>
  <version>1.0.0</version>
</parent>

<artifactId>maven-service</artifactId>
<name>com.hollis.lab</name>
<packaging>jar</packaging>

In this way, a parent-child relationship is formed.

However, such a project will be more troublesome when the version is upgraded, because the version number in all POMS to be traversed will be modified. For example, to upgrade 1.0.0 to 1.0.1, you need to change the versions in all POMs.

The process of human flesh modification is tedious and error prone, so is there any way to replace human flesh modification?

The answer is yes.

One line command to modify all version numbers

Maven is powerful because it has a plug-in mechanism for ox. We can do this with a plug-in.

This plug-in is versions Maven plugin. It is also easy to use. In the outermost POM file, add the following plug-in configuration:


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

Generatebackuppoms is used to configure whether to generate a backup POM for version rollback.

After the plug-in is configured, execute the command


mvn versions:set -DnewVersion=1.0.1

All version numbers in the above example can be reduced to 1.0.1.

For convenience, you can also set aliases on Linux, such as:


alias mvs='mvs() { mvn versions:set -DnewVersion=$1 }; mvs'

You can use the MVS command to change the version number.

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.