1

I want to have the buildNumber from buildnumber-maven-plugin in an XML file.

I configured the build number plugin. I can see it works because the finalName is being set with a build number. Then I configured resources filtering. I can see the resource deployment.xml is filtered, because it replaces the ${project.version}. However it does not replace the ${buildNumber}. What do I have to do differently to make it work? I looked at Maven build number plugin, how to save the build number in a file?, but it doesn't work like there, although there pretty much the same is done.

pom.xml:

    <!-- build number -->
        <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>
             <format>{0,number}</format>
             <items>
                <item>buildNumber</item>
             </items>
             <doCheck>false</doCheck>
             <doUpdate>false</doUpdate>
           </configuration>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}-${project.version}-r${buildNumber}</finalName>
    <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
          <includes>
            <include>**/deployment.xml</include>
          </includes>
        </resource>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>false</filtering>
          <excludes>
            <exclude>**/deployment.xml</exclude>
          </excludes>
        </resource>
    </resources>
</build>
<!-- dummy scm for build number plugin -->
<scm>
   <connection>scm:git:http://127.0.0.1/dummy</connection>
   <developerConnection>scm:git:https://127.0.0.1/dummy</developerConnection>
   <tag>HEAD</tag>
   <url>http://127.0.0.1/dummy</url>
</scm>

src/main/resources/deployment.xml

<root>
    <build>
        <buildNumber>${buildNumber}</buildNumber>
        <buildNumber>${project.version}</buildNumber>
        <buildNumber>${project.finalName}</buildNumber>
    </build>
</root>

target/classes/deployment.xml

<root>
    <build>
        <buildNumber>${buildNumber}</buildNumber>
        <buildNumber>1.0.0-BUILD-SNAPSHOT</buildNumber>
        <buildNumber>${project.finalName}</buildNumber>
    </build>
</root>

I now noticed something weird: During maven install, the build number is first replaced into the XML file, and then the XML file gets replaced again with the XML file above which has ${buildNumber} not replaced.

I tried switching off the additional copying steps, but it doesn't seem to have the effect I am expecting (I still have the same issue):

[INFO] --- maven-resources-plugin:3.1.0:copy-resources (copy-resources) @ rator ---
[INFO] Using 'Cp1252' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 83 resources
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ rator ---
[INFO] Skipping the execution.
[INFO] 
.. compiling ..
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ rator ---
[INFO] Not copying test resources
[INFO] 

I was looking at target/classes. It appears the build number is correctly packaged into the war file, when I am looking at target/xxx-1.0.0-BUILD-SNAPSHOT-r36/WEB-INF/classes/deployment.xml. I guess I can live with this, although I still don't understand why the file target/classes/deployment.xml gets recreated after the replacement of the buildNumber.

More weirdness: When I edit src/main/resources/deployment.xml then the file target/classes/deployment.xml gets updated as well. It must be something done by the eclipse IDE?

I moved deployment.xml to resources-filtered/deployment.xml but the overwriting behavior persists.

Adder
  • 5,708
  • 1
  • 28
  • 56

1 Answers1

1

Have a properties section in your xml. This way you tell Maven about your custom properties, which will be replaced on build time.

<project>
  <properties>
      <buildNumber>YOUR_BUILD_NUMBER</buildNumber>
      <anotherProperty>foo</anotherProperty>
  </properties>
</project>

See https://maven.apache.org/pom.html#Properties

Here an unofficial list of predefined Maven properties: https://github.com/cko/predefined_maven_properties/blob/master/README.md

Steve Benett
  • 12,843
  • 7
  • 59
  • 79