3

Is there a way to have maven pad a numeric value (ex: build number) within a POM? I've been googling the topic and haven't come up with anything yet.

My use case is as follows. The maven build process is provided a build number via Jenkins which needs to be included as part of the name of the WAR that is generated. So if I provide it 12 as the build number, then I want the WAR file name to be myWar##000012.war. The ##000012 part of the name is the version identifier used by Tomcat.

jwmajors81
  • 1,430
  • 4
  • 20
  • 36
  • Why do you need such build number? You have a version which is unique. Furthermore i hope you are using a version control system which has tags/labels etc. which should be used as references. – khmarbaise Aug 08 '13 at 17:08
  • We've got all the change control system goodies (maven, CI, continuous deployment, etc) – jwmajors81 Aug 09 '13 at 13:53

5 Answers5

1

The simplest solution may be to embed a scripting language in your build. For example, with Groovy, if you have a buildNumber property:

   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>groovy-maven-plugin</artifactId>
    <version>1.5</version>
    <executions>
     <execution>
     <phase>validate</phase>
     <goals><goal>execute</goal></goals>
     <configuration>
      <source>
       project.properties['nameSuffix'] = "##" + String.format("%06d", project.properties['buildNumber'].toLong());
      </source>
     </configuration>
     </execution>
    </executions>
   </plugin>

Afterwards the nameSuffix property is available to define the final name.

Alternatively, as suggested in In Maven, how can I dynamically build a property value at runtime?, use build-helper:regex-property to transform the string.

Community
  • 1
  • 1
Joe
  • 29,416
  • 12
  • 68
  • 88
0

Have you tried using the maven release plugin?

codethulhu
  • 3,976
  • 2
  • 21
  • 15
  • I just reviewed some of the documentation on the web site for the maven release plugin and I don't see how it solves my problem. Was there a particular feature of the plugin that I should be investigating? – jwmajors81 Aug 07 '13 at 21:20
  • See the portion of the page I linked titled "Overriding the default tag name format". – codethulhu Aug 07 '13 at 21:58
0

Based upon @Joe suggestion I looked into the build-helper-maven-plugin and was able to come up with the following which does what I need. I wasn't able to identify how to do it all in one step, so I'm doing it in 2. The first step pads the value on the left with zeros. The second step trims the numeric value so that it is only 7 digits long. Please note that ${build.env.version} is passed in as a parameter to the maven build process and that I have defaulted it to 0 in the POM file for when it isn't passed. If you don't provide a default value then the build fails even though failOnError on set to false.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
      <execution>
        <id>stage1--padNumber</id>
        <goals>
          <goal>regex-property</goal>
        </goals>
        <configuration>
          <name>build.env.version.padded</name>
          <value>${build.env.version}</value>
          <regex>^([\d]{0,})$</regex>
          <replacement>000000$1</replacement>
          <failIfNoMatch>false</failIfNoMatch>
          <failOnError>false</failOnError>
        </configuration>
      </execution>
      <execution>
        <id>stage2--leftTrimToXcharacters</id>
        <goals>
          <goal>regex-property</goal>
        </goals>
        <configuration>
          <name>build.env.version.padded</name>
          <value>${build.env.version.padded}</value>
          <regex>^([\d]*)([\d]{7})$</regex>
          <replacement>$2</replacement>
          <failIfNoMatch>false</failIfNoMatch>
          <failOnError>false</failOnError>
        </configuration>
      </execution>
    </executions>
  </plugin>
jwmajors81
  • 1,430
  • 4
  • 20
  • 36
0

Based upon @jwmajors81 suggestion, I needed to pad the major version for a specific reason...

As we were already using the build-helper-maven-plugin, it is easy enough to get the major version by using the parse-version goal of the build helper. (we only needed 3 chars):

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>parse-version</id>
                    <goals>
                        <goal>parse-version</goal>
                    </goals>
                </execution>
                <execution>
                    <id>stage1--padNumber</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>build.env.version.padded</name>
                        <value>${parsedVersion.majorVersion}</value>
                        <regex>^([\d]{0,})$</regex>
                        <replacement>00$1</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                        <failOnError>false</failOnError>
                    </configuration>
                </execution>
                <execution>
                    <id>stage2--leftTrimToXcharacters</id>
                    <goals>
                        <goal>regex-property</goal>
                    </goals>
                    <configuration>
                        <name>build.env.version.padded</name>
                        <value>${build.env.version.padded}</value>
                        <regex>^([\d]*)([\d]{3})$</regex>
                        <replacement>$2</replacement>
                        <failIfNoMatch>false</failIfNoMatch>
                        <failOnError>false</failOnError>
                    </configuration>
                </execution>
            </executions>
        </plugin>
theINtoy
  • 3,388
  • 2
  • 37
  • 60
0

Yet, in that particular case, if you also need to generate a buildNumber, buildnumber-maven-plugin may be the most straight solution:

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <format>{0, number,000000}</format>
                <items>
                    <item>buildNumber</item>
                </items>
            </configuration>
            <executions>
                <execution>
                    <phase>validate</phase>
                    <goals>
                        <goal>create</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>myWar##${buildNumber}</finalName>
soysal
  • 325
  • 4
  • 9