Search code examples
mavenbuild-automationrelease-management

Maven, automating the version change


Let's say I have a pom file,

<version>14.4.1-SNAPSHOT</version>

which defines a version of the project to build. This value is updated automatically by our build system (jenkins).

Later on, in one of the plugins, I need to have a property, which incorporates the first two figures from the version, so that for 14.4.1-SNAPSHOT value, it would be "14.4", and for 13.12.39-SNAPSHOT value it would be "13.12".

Currently we update this value manually each month:

<plugins>
        <plugin>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-maven-plugin</artifactId>
            <version>2.0.1</version>
            <executions>
                <execution>
                    <id>default-cli</id>
                    <phase>process-resources</phase>
                    <configuration>
                        <changeLogFile>src/main/resources/14.4/changeLog.xml</changeLogFile>

Ideally, I would love to have instead something like

<changeLogFile>src/main/resources/${releaseVersion}/changeLog.xml</changeLogFile>

But how would I get this ${releaseVersion} (=14.4) calculated automatically from the <version>14.4.1-SNAPSHOT</version> ?

In that case it is absolutely automated, and we do not have any manual process in place. Is there any expressions-kind-of-language I can use in pom files, which could parse the string 14.4.1-SNAPSHOT and produce from it an 14.4 ?


Solution

  • You can try the mojo build-helper with parse-version for this.

    Stack Overflow Question

    Original Documentation

    [Edit] Here's my example pom.xml:

    <build>
    
     <plugins>
      <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-war-plugin</artifactId>
      <version>2.2</version>
      <configuration>
      <webResources>
        <resource>
                <filtering>true</filtering>
         <targetPath>WEB-INF</targetPath>
        <directory>src/main/resources/${parsedVersion.majorVersion}.${parsedVersion.minorVersion}</directory>
        </resource>
    </webResources>
    </configuration>
    </plugin>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
          <phase>validate</phase>
            <id>parse-version</id>
            <goals>
              <goal>parse-version</goal>
            </goals>
             <configuration>
            <propertyPrefix>parsedVersion</propertyPrefix>
        </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.1</version>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <tasks>
                            <echo>Major: ${parsedVersion.majorVersion}</echo>
                            <echo>Minor: ${parsedVersion.minorVersion}</echo>
                            <echo>Incremental: ${parsedVersion.incrementalVersion}</echo>
                            <echo>Qualifier: ${parsedVersion.qualifier}</echo>
                            <echo>BuildNumber: ${parsedVersion.buildNumber}</echo>
                        </tasks>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        </plugins>
    </build>