2

How can I do this? I want to get the build number from the checked out branch and build it with Maven. Later in my java application I want to use the buildNumber variable as a Revision ID so i'll have more specific details on each project build. I am currently using this code for getting the buildNumber and use resource filtering on it in a build.properties file but the ${buildNumber} from the file doesn't get updated with the revision number.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.company</groupId>
    <artifactId>myProject</artifactId>
    <version>2.5</version>
    <packaging>pom</packaging>
    <name>myProject</name>
    <description>myProject Modules</description>
    <!-- Module list must match directory name (Camel Case) -->
    <modules>
        ....
        <!-- Contains modules -->
    </modules>
    <properties>
        <myProject.version>${project.version}</myProject.version>
        <java-version>1.7</java-version>
        <svn.root>http://SVNmyCompanyURL/Project/myProject/trunk</svn.root>
        <apsf.groupId>com.myCompany.aps</apsf.groupId>
        <aji.version>13.22</aji.version>
        <pim.version>1.8</pim.version>
        <apsf.version>1.5.0.0</apsf.version>
        <springframework.version>4.2.3.RELEASE</springframework.version>
        <apsf.service.groupId>com.myCompany.aps.adf</apsf.service.groupId>
        <apsf.service.version>1.5.0.0</apsf.service.version>
        <apsf.codegen.groupId>com.myCompany.aps.adf</apsf.codegen.groupId>
        <apsf.codegen.version>1.5.0.0</apsf.codegen.version>
        <apsf.module.scope>compile</apsf.module.scope>
        <myProject.buildNumber>${project.version}.{buildNumber}</myProject.buildNumber>
    </properties>
    <scm>
        <url>${svn.root}</url>
        <connection>scm:svn:${svn.root}</connection>
        <developerConnection>scm:svn:${svn.root}</developerConnection>
        <tag>HEAD</tag>
    </scm>
    <issueManagement>
        <!-- Jira stuff -->
    </issueManagement>
    <organization>
        <!-- Company stuff -->
    </organization>
    <dependencyManagement>
        <dependencies>
            <!--Lots of dependencies-->
        </dependencies>
    </dependencyManagement>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <skip.integration.tests>true</skip.integration.tests>
                <skip.unit.tests>false</skip.unit.tests>
            </properties>
        </profile>
        <profile>
            <id>skipTests</id>
            <properties>
                <skip.integration.tests>true</skip.integration.tests>
                <skip.unit.tests>true</skip.unit.tests>
            </properties>
        </profile>
        <profile>
            <id>integration-test</id>
            <properties>
                <build.profile.id>integration-test</build.profile.id>
                <skip.integration.tests>false</skip.integration.tests>
                <skip.unit.tests>true</skip.unit.tests>
            </properties>
        </profile>
    </profiles>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources/</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>buildnumber-maven-plugin</artifactId>
                    <version>1.3</version>
                    <executions>
                        <execution>
                            <phase>validate</phase>
                            <goals>
                                <goal>create</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <timestampFormat>{0,date,dd-MM-yyyy HH:mm:ss}</timestampFormat>
                        <doCheck>false</doCheck>
                        <doUpdate>false</doUpdate>
                        <providerImplementations>
                            <svn>javasvn</svn>
                        </providerImplementations>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>com.google.code.maven-scm-provider-svnjava</groupId>
                            <artifactId>maven-scm-provider-svnjava</artifactId>
                            <version>2.1.1</version>
                        </dependency>
                        <dependency>
                            <groupId>org.tmatesoft.svnkit</groupId>
                            <artifactId>svnkit</artifactId>
                            <version>1.8.5</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <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>
                        <compilerArgument>-Xlint:none</compilerArgument>
                        <showWarnings>true</showWarnings>
                        <showDeprecation>true</showDeprecation>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.15</version>
                    <configuration>
                        <skipTests>${skip.unit.tests}</skipTests>
                        <argLine>-XX:-UseSplitVerifier</argLine>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>2.4</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jarsigner-plugin</artifactId>
                    <version>1.2</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                            </manifest>
                            <manifestEntries>
                                <Implementation-Build>${buildNumber}</Implementation-Build>
                                <Implementation-Title>${project.name}</Implementation-Title>
                                <Implementation-Vendor>ENTERPRISE</Implementation-Vendor>
                                <Implementation-Version>${project.version}</Implementation-Version>
                                <Built-By>${user.name}</Built-By>
                                <Built-OS>${os.name}</Built-OS>
                                <Build-Date>${timestamp}</Build-Date>
                                <SCM-Revision>${buildNumber}</SCM-Revision>
                            </manifestEntries>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ear-plugin</artifactId>
                    <version>2.8</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>cobertura-maven-plugin</artifactId>
                    <version>2.6</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>webstart-maven-plugin</artifactId>
                    <version>1.0-beta-6</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.3.1</version>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                    <version>2.4</version>
                </plugin>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-dependency-plugin</artifactId>
                                        <versionRange>[2.1,)</versionRange>
                                        <goals>
                                            <goal>copy</goal>
                                            <goal>unpack</goal>
                                            <goal>copy-dependencies</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>build-helper-maven-plugin</artifactId>
                                        <versionRange>[1.7,)</versionRange>
                                        <goals>
                                            <goal>add-test-source</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-antrun-plugin</artifactId>
                                        <versionRange>[1.3,)</versionRange>
                                        <goals>
                                            <goal>run</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.jibx</groupId>
                                        <artifactId>jibx-maven-plugin</artifactId>
                                        <versionRange>[1.2.3,)</versionRange>
                                        <goals>
                                            <goal>bind</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
8
  • What do you get for the property ${buildNumber}? (because according to the doc, it should be the revision number) Could you show the part of the pom where you use it? Commented Mar 2, 2017 at 13:50
  • I don't get anything, that's the point. Its just the text and doesn't get replaced with numbers Commented Mar 2, 2017 at 14:02
  • Ok, I don't see you using ${buildNumber} anywhere in your pom, could you add that section? Commented Mar 2, 2017 at 14:04
  • Sure, just a sec to get to the office Commented Mar 2, 2017 at 14:04
  • @asettouf updated now Commented Mar 2, 2017 at 14:12

2 Answers 2

1

You should use the following to configure it correctly as aalready mentioned not defining executions in pluginManagement.

<project
  xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company</groupId>
  <artifactId>myProject</artifactId>
  <version>2.5</version>
  <packaging>pom</packaging>
  <name>myProject</name>
  <description>myProject Modules</description>


  <!-- Module list must match directory name (Camel Case) -->
  <modules>
    <!-- Contains modules -->
  </modules>


  <properties>
    <maven.compiler.target>1.7</maven.compiler.target>
    <maven.compiler.source>1.7</maven.compiler.source>

    <svn.root>http://SVNmyCompanyURL/Project/myProject/trunk</svn.root>
    <apsf.groupId>com.myCompany.aps</apsf.groupId>
    <aji.version>13.22</aji.version>
    <pim.version>1.8</pim.version>
    <apsf.version>1.5.0.0</apsf.version>
    <springframework.version>4.2.3.RELEASE</springframework.version>
    <apsf.service.groupId>com.myCompany.aps.adf</apsf.service.groupId>
    <apsf.service.version>1.5.0.0</apsf.service.version>
    <apsf.codegen.groupId>com.myCompany.aps.adf</apsf.codegen.groupId>
    <apsf.codegen.version>1.5.0.0</apsf.codegen.version>
    <apsf.module.scope>compile</apsf.module.scope>
  </properties>


  <scm>
    <url>${svn.root}</url>
    <connection>scm:svn:${svn.root}</connection>
    <developerConnection>scm:svn:${svn.root}</developerConnection>
    <tag>HEAD</tag>
  </scm>


  <issueManagement>
    <!-- Jira stuff -->
  </issueManagement>

  <organization>
    <!-- Company stuff -->
  </organization>


  <dependencyManagement>
    <dependencies>

      <!--Lots of dependencies -->

    </dependencies>
  </dependencyManagement>


  <profiles>
    <profile>
      <id>dev</id>
      <properties>
        <skip.integration.tests>true</skip.integration.tests>
        <skip.unit.tests>false</skip.unit.tests>
      </properties>
    </profile>

    <profile>
      <id>skipTests</id>
      <properties>
        <skip.integration.tests>true</skip.integration.tests>
        <skip.unit.tests>true</skip.unit.tests>
      </properties>
    </profile>

    <profile>
      <id>integration-test</id>
      <properties>
        <build.profile.id>integration-test</build.profile.id>
        <skip.integration.tests>false</skip.integration.tests>
        <skip.unit.tests>true</skip.unit.tests>
      </properties>
    </profile>
  </profiles>


  <build>
    <resources>
      <resource>
        <directory>src/main/resources/</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>buildnumber-maven-plugin</artifactId>
          <version>1.4</version>
          <executions>
            <execution>
              <phase>validate</phase>
              <goals>
                <goal>create</goal>
              </goals>
            </execution>
          </executions>
          <configuration>
            <timestampFormat>{0,date,dd-MM-yyyy HH:mm:ss}</timestampFormat>
            <doCheck>false</doCheck>
            <doUpdate>false</doUpdate>
            <providerImplementations>
              <svn>javasvn</svn>
            </providerImplementations>
          </configuration>
          <dependencies>
            <dependency>
              <groupId>com.google.code.maven-scm-provider-svnjava</groupId>
              <artifactId>maven-scm-provider-svnjava</artifactId>
              <version>2.1.1</version>
            </dependency>
            <dependency>
              <groupId>org.tmatesoft.svnkit</groupId>
              <artifactId>svnkit</artifactId>
              <version>1.8.5</version>
            </dependency>
          </dependencies>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.6.1</version>
          <configuration>
            <compilerArgument>-Xlint:none</compilerArgument>
            <showWarnings>true</showWarnings>
            <showDeprecation>true</showDeprecation>
          </configuration>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.15</version>
          <configuration>
            <skipTests>${skip.unit.tests}</skipTests>
            <argLine>-XX:-UseSplitVerifier</argLine>
          </configuration>
        </plugin>

        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-assembly-plugin</artifactId>
          <version>2.4</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jarsigner-plugin</artifactId>
          <version>1.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <version>2.4</version>
          <configuration>
            <archive>
              <manifest>
                <addClasspath>true</addClasspath>
              </manifest>
              <manifestEntries>
                <Implementation-Build>${buildNumber}</Implementation-Build>
                <Implementation-Title>${project.name}</Implementation-Title>
                <Implementation-Vendor>ENTERPRISE</Implementation-Vendor>
                <Implementation-Version>${project.version}</Implementation-Version>
                <Built-By>${user.name}</Built-By>
                <Built-OS>${os.name}</Built-OS>
                <Build-Date>${timestamp}</Build-Date>
                <SCM-Revision>${buildNumber}</SCM-Revision>
              </manifestEntries>
            </archive>
          </configuration>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-war-plugin</artifactId>
          <version>2.3</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-ear-plugin</artifactId>
          <version>2.8</version>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>cobertura-maven-plugin</artifactId>
          <version>2.6</version>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>webstart-maven-plugin</artifactId>
          <version>1.0-beta-6</version>
        </plugin>
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>
          <version>1.3.1</version>
        </plugin>

        <!--
          ! Wrong plugin.
          !
        -->
        <plugin>
          <groupId>org.codehaus.mojo</groupId>
          <artifactId>sonar-maven-plugin</artifactId>
          <version>2.4</version>
        </plugin>
        <!--
          ! This is the correct one! 
         -->
        <plugin>
          <groupId>org.sonarsource.scanner.maven</groupId>
          <artifactId>sonar-maven-plugin</artifactId>
          <version>3.2</version>
        </plugin>

        <!--This plugin's configuration is used to store Eclipse m2e settings
          only. It has no influence on the Maven build itself. -->
        <plugin>
          <groupId>org.eclipse.m2e</groupId>
          <artifactId>lifecycle-mapping</artifactId>
          <version>1.0.0</version>
          <configuration>
            <lifecycleMappingMetadata>
              <pluginExecutions>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>
                      org.apache.maven.plugins
                    </groupId>
                    <artifactId>
                      maven-dependency-plugin
                    </artifactId>
                    <versionRange>
                      [2.1,)
                    </versionRange>
                    <goals>
                      <goal>copy</goal>
                      <goal>unpack</goal>
                      <goal>
                        copy-dependencies
                      </goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>
                      org.codehaus.mojo
                    </groupId>
                    <artifactId>
                      build-helper-maven-plugin
                    </artifactId>
                    <versionRange>
                      [1.7,)
                    </versionRange>
                    <goals>
                      <goal>add-test-source</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>
                      org.apache.maven.plugins
                    </groupId>
                    <artifactId>
                      maven-antrun-plugin
                    </artifactId>
                    <versionRange>
                      [1.3,)
                    </versionRange>
                    <goals>
                      <goal>run</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
                <pluginExecution>
                  <pluginExecutionFilter>
                    <groupId>org.jibx</groupId>
                    <artifactId>
                      jibx-maven-plugin
                    </artifactId>
                    <versionRange>
                      [1.2.3,)
                    </versionRange>
                    <goals>
                      <goal>bind</goal>
                    </goals>
                  </pluginExecutionFilter>
                  <action>
                    <ignore />
                  </action>
                </pluginExecution>
              </pluginExecutions>
            </lifecycleMappingMetadata>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <executions>
          <execution>
            <phase>validate</phase>
            <goals>
              <goal>create</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>

  </build>

</project>
Sign up to request clarification or add additional context in comments.

1 Comment

I still don't get the revision number when i run this build file.
0

I had to create a separate <plugins> tag </plugins> outside the <pluginManagement> tag and insert the plugin there and now it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.