Maven – Normalize release using maven-release-plugin

Article directory

  • Maven Release plugin – Introduction
  • Maven Release plugin – Plugin Documentation
  • Maven Release plugin – Usage
  • Practical cases


Maven Release plugin – Introduction

Maven Release Plugin (Maven Release Plugin) is a plug-in used to help perform the version release process in Maven projects. Its main function is tosimplify the release and management of project versions, ensure the correctness of version numbers, and automatically handle tasks related to version releases.

Typically, version management of software projects is a complex process that requires consideration of a series of tasks such as the increment of version numbers, branching of the code base, creation of tags, and updating of documents. The Maven Release Plugin is designed to reduce human error and provide a consistent release process by automating these tasks.

The Maven Release Plugin provides the following main steps and functionality:

  1. Prepare Phase:

    • Check the status of the code base to make sure there are no uncommitted changes.
    • Automatically increase the project version number, such as from 1.0.0-SNAPSHOT to 1.0.0.
    • Create a release tag for subsequent version control and tracking.
  2. Perform Phase:

    • Submit updated version numbers and tags to a version control system (e.g. Git, Subversion).
    • Build, test, package the project, and generate release packages.
    • Deploy the generated release package to the Maven repository to make it available to other projects.
  3. End phase:

    • Revert development version numbers (e.g. from 1.0.0 to 1.0.1-SNAPSHOT).
    • Submit the restored development version number to the version control system.

The benefits of using the Maven Release Plugin include:

  • Simplify the process: Automate tedious version management tasks and reduce the possibility of human error.
  • Consistency: Ensure the publishing process is consistent and all releases are performed according to the same rules.
  • Version Control: Create version number labels to make it easier to find and trace back specific versions of code in the future.
  • Build stability: Perform release tasks in an independent build environment to reduce problems caused by inconsistencies with the development environment.

To use the Maven Release Plugin, you need to configure the plug-in in the project’s pom.xml file, and then trigger the plug-in operation through the command line or the plug-in integration of the integrated development environment. It should be noted that the Maven Release Plugin needs to be configured and used with caution, especially in a team collaboration environment, to ensure that all developers understand and follow the corresponding release process.

When using the Maven Release Plugin, the following steps and configuration are typically involved:

Step 1: Configure Maven Release Plugin

In the project’s pom.xml file, the Maven Release Plugin needs to be configured. Here is an example plugin configuration:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <tagNameFormat>@{project.version}</tagNameFormat>
            </configuration>
        </plugin>
    </plugins>
</build>

In the above configuration, we specified the Maven Release Plugin version as 3.0.0 and set a tag name format, where @{project.version} will be replaced with the project version number .

Step 2: Execute the publishing process

  1. Prepare Phase:

    Execute the following commands to prepare for release, which will include incrementing the version number and creating tags:

    mvn release:prepare
    

    The plugin will prompt you for a version number, the next development version number, and whether to commit and push these changes.

  2. Perform Phase:

    After the preparation phase is complete, execute the following command to perform the publish:

    mvn release:perform
    

    This will build, test, package your project, and deploy the resulting release to a Maven repository.

  3. End phase:

    After completing the release, you can execute the following command to end the release process and restore the development version number:

    mvn release:clean
    

    This will undo previous version number and label changes.

Note:

  • Ensure that all uncommitted changes are committed or saved before executing the publish process to avoid data loss.
  • The behavior of the plug-in is affected by the structure and build process of the project, and the specific configuration and steps may vary from project to project.
  • If you are using a version control system (such as Git), make sure you have the appropriate permissions to create tags and push changes.

Please note that this is just a simple example, and actual use may require more detailed configuration based on project needs. It is recommended to read the relevant documents before using the Maven Release Plugin to understand the meaning of each configuration parameter and possible problems and solutions.

This plug-in is used to publish projects using Maven, saving a lot of repetitive manual work. Project launch is a two-step process: preparation and execution.

https://maven.apache.org/maven-release/maven-release-plugin/

  • release:clean Clean up after a release preparation.
  • release:prepare Prepare for a release in SCM.
  • release:prepare-with-pom Prepare for a release in SCM, and generate release POMs that record the fully resolved projects used.
  • release:rollback Rollback a previous release.
  • release:perform Perform a release from SCM.
  • release:stage Perform a release from SCM into a staging folder/repository.
  • release:branch Create a branch of the current project with all versions updated.
  • release:update-versions Update the versions in the POM(s).

Maven Release plugin – Plugin Documentation

https://maven.apache.org/maven-release/maven-release-plugin/plugin-info.html

You should specify the version in your project’s plugin configuration:

<project>
  ...
  <build>
    <!-- To define the plugin version in your parent POM -->
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-release-plugin</artifactId>
          <version>3.0.1</version>
        </plugin>
        ...
      </plugins>
    </pluginManagement>
    <!-- To use the plugin goals in your POM or parent POM -->
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-release-plugin</artifactId>
      </plugin>
      ...
    </plugins>
  </build>
  ...
</project>

Maven Release plugin – Usage



https://gitee.com/-/ide/project/FutaoSmile/starter-fustack/edit/master/-/pom.xml

 ?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <packaging>pom</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.futao</groupId>
    <artifactId>starter-fustack</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    <name>starter-fustack</name>
    <description>framework base springboot</description>

    <modules>
        <!--WeChat Mini Program-->
        <module>fustack-wx-mini-program</module>
     
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.3.0</spring-boot-admin.version>
        <maven.test.skip>true</maven.test.skip>
        <maven.javadoc.skip>true</maven.javadoc.skip>
    </properties>


    <developers>
        <developer>
            <id>futao</id>
            <email>[email protected]</email>
            <name>A maverick pig</name>
            <roles>
                <role>CEO</role>
                <role>developer-be</role>
            </roles>
            <timezone>GMT + 8</timezone>
            <url>https://gitee.com/FutaoSmile</url>
        </developer>
    </developers>


    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
       .......
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!--jwt start-->
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt-api</artifactId>
                <version>0.11.2</version>
            </dependency>
         
         ...
        </dependencies>
    </dependencyManagement>

    <!-- <build>-->
    <!-- <plugins>-->
    <!-- <plugin>-->
    <!-- <groupId>org.springframework.boot</groupId>-->
    <!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
    <!-- </plugin>-->
    <!-- </plugins>-->
    <!-- </build>-->


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <skip>true</skip>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <configuration>
                    <!--<tagBase>https://github.com/FutaoSmile/starter-fustack</tagBase>-->
                    <!--Whether to automatically assign parent versions to submodules. If set to false, the user will be prompted for the version of each submodule. -->
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                    <!--tag tag name-->
                    <tagNameFormat>release-by-maven-${version}</tagNameFormat>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>ishanggang-releases</id>
            <url>https://maven.ishanggang.com/repository/maven-releases/</url>
        </repository>
    </distributionManagement>


    <scm>
        <!--Readable SCM address-->
        <!--<connection>scm:git:https://github.com/FutaoSmile/starter-fustack</connection>-->
        <!--Writable SCM address-->
        <developerConnection>scm:git:https://github.com/FutaoSmile/starter-fustack</developerConnection>
        <!--The scm address that can be accessed in the browser-->
        <!--<url>https://github.com/FutaoSmile/starter-fustack</url>-->
        <!--Set tagName-->
        <tag>V-release-0.0.1</tag>
    </scm>
</project>

Practical cases

Use the automated plug-in release to manage the version release of maven projects