SoFunction
Updated on 2025-04-06

Detailed steps to using Maven for version management

Step 1: Define the project version

existIn the file, you need to define the version number of the project. Version numbers usually follow<major>.<minor>.<incremental>-<qualifier>format, where:

  • major: Major version number, usually added when there are major changes.
  • minor: Minor version number, usually added when new features are added.
  • incremental: Incremental version number, usually increased when fixing bugs.
  • qualifier: Qualifier, usually used to identify development versions (e.g.SNAPSHOT) or pre-release version (e.g.RC1)。

A basic version definition example:

<project>
  ...
  <modelVersion>4.0.0</modelVersion>
  <groupId></groupId>
  <artifactId>my-project</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  ...
</project>

In this example, <version>1.0.0-SNAPSHOT</version> means that the project is currently in development and is a snapshot version.

Step 2: Automatically update the version number

Maven provides versions-maven-plugin plugin, which can help you automatically update project version numbers. To use this plugin, you first need to add the plugin configuration in:

<build>
  <plugins>
    <plugin>
      <groupId></groupId>
      <artifactId>versions-maven-plugin</artifactId>
      <version>2.12.0</version>
    </plugin>
  </plugins>
</build>

Then, run the following command on the command line to update the version number:

mvn versions:set -DnewVersion=1.0.1-SNAPSHOT

This command will transfer the project from1.0.0-SNAPSHOTUpdate to1.0.1-SNAPSHOT

Step 3: Submit the updated version number

After updating the version number, you need to submit the change to the version control system:

git commit -am "Bump version to 1.0.1-SNAPSHOT"

Step 4: Publish the version

When you are ready to release a version, you need to update the snapshot version to the official version. For example,1.0.1-SNAPSHOTUpdated to1.0.1

mvn versions:set -DnewVersion=1.0.1

Then submit the change again:

git commit -am "Release version 1.0.1"

Step 5: Rollback version update

If you find any problem after updating the version number, you can useversions-maven-pluginPlugin rollback version update:

mvn versions:revert

This command will undo the last version update operation.

Step 6: Use the Version Management Command

The versions-maven-plugin plugin also provides other useful commands, such as versions:display-dependent-updates and versions:display-plugin-updates, which can help you view dependencies and plug-ins' version updates.

mvn versions:display-dependency-updates

This command lists all dependencies that can be updated.

Through these steps, you can effectively use Maven for version management, ensuring that the version number of the project is always up to date, while conveniently publishing and rolling back. Maven's version management feature is one of its core features, helping to maintain project stability and maintainability.

This is the end of this article about the detailed steps of using Maven for version management. For more related Maven version management content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!