22

I've a Java project using Spring Framework and Git and I wanted to display a build number. I found the Build Number Maven plugin. With Git the build number is a Git hash. I dislike that and I thought a date was much more expressive.

I found this excellent blog article explaining how to use build number plugin with a different profile for SVN and Git. Since I just use Git, instead of creating a new profile, I just copied the plugin part in my build tag.

When I run "mvn package" it tells me:

[INFO] --- buildnumber-maven-plugin:1.0:create (default) @ sherd ---
[INFO] Storing buildNumber: 2011-08-04_21-48_stivlo at timestamp: 1312487296631

Which looks ok, but I wonder, where is it stored? "git status" doesn't detect any new file and it seems it's not in target/ too (target/ is in my .gitignore).

Maybe I've to change the configuration to store the build number in a file? How can I use the build number value?


Thanks to the hint of Michael-O, I read the chapter about how to filter resource files in Maven Getting Started Guide. I've created a file application.properties in src/main/resources/properties/application.properties with the following contents:

# application properties
application.name=${pom.name}
application.version=${pom.version}
application.build=${buildNumber}

I've added the following XML snippet within my build section:

<resources>
    <resource>
        <directory>src/main/resources/properties</directory>
        <filtering>true</filtering>
    </resource>
</resources>

Now when I call from command line "mvn package", this property file gets saved in target/classes/properties/application.properties, for instance with the following contents:

# application properties
application.name=Sherd Control Panel
application.version=1.0.1-SNAPSHOT
application.build=2011-08-05_05-55_stivlo

Everything works fine from command line, but, sigh, m2eclipse gives Build errors:

05/08/11 6.05.03 CEST: Build errors for obliquid-cp; 
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal
org.codehaus.mojo:buildnumber-maven-plugin:1.0:create (default) on project 
sherd: Cannot get the branch information from the scm repository : 
Exception while executing SCM command.

For some reason m2eclipse tries to connect to my repository, but it can't because it's a Git repository accessed with SSH and a private key. I wonder if I can tell m2eclipse to not connect to Git.


After more digging I found about revisionOnScmFailure option, set it to true and now also m2eclipse works. For reference, here is the full configuration of buildnumber maven plugin that I used (it goes in pom.xml in the build / plugins section).

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>create</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <doCheck>false</doCheck>
        <doUpdate>false</doUpdate>
         <revisionOnScmFailure>true</revisionOnScmFailure>
        <format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
        <items>
            <item>timestamp</item>
            <item>${user.name}</item>
        </items>
    </configuration>
</plugin>
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
stivlo
  • 83,644
  • 31
  • 142
  • 199

4 Answers4

10

You shouldn't set revisionOnScmFailure option to true, it doesn't expect a boolean. Set it to revision string you want to use when SCM is unavailable, like na or like that. It doesn't matter for your case since you override the build number format but it would be more correct.

See buildnumber-maven-plugin docs.

Tombart
  • 30,520
  • 16
  • 123
  • 136
Tvaroh
  • 6,645
  • 4
  • 51
  • 55
  • Thanks, i could not get the plugin to work when building through Netbeans on Mac OS X 10.10, but through the command line it builds fine. Setting this property let me continue working through the IDE and the Jenkins server obviously works too. – Hiro2k Jun 03 '16 at 17:35
4

Store it in a filtered properties file. See Using maven to output the version number to a text file

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
Michael-O
  • 18,123
  • 6
  • 55
  • 121
3

I could not reproduce the problem reported by the OP. In my case both command line and m2eclipse work fine and the file is generated correctly in the target/classes folder. The answer provided by @KasunBG is incorrect. The buildNumber.properties is generated only if you use the following:

        <format>{0,number}</format>
        <items>
            <item>buildNumber</item>
        </items>

buildNumber.properties is used to store a number which can be incremented. For this reason ( I think ) the plugin doesn't generate this file if you use timestamp/scmVersion etc.

Prashant Saraswat
  • 838
  • 1
  • 8
  • 20
0

The documentation page says that the properties files is stored at ${basedir}/buildNumber.properties, which is created when the buildnumber:create phase is ran.

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
Kasun Gajasinghe
  • 2,735
  • 1
  • 21
  • 30
  • however it doesn't create that file, at least with my configuration. I had a partial success by filtering properties files, but I ran into another problem as in my updated question. – stivlo Aug 05 '11 at 04:31
  • have you tried manually creating the file, and adding a starting buildNumber there first? Do that and, do your thing and then check whether the number is incremented. Make sure you fulfilled the requirements it asked for. – Kasun Gajasinghe Aug 05 '11 at 05:03
  • Thanks for the suggestion. I tried to create an empty buildNumber.properties file, and execute buildnumber:create and nothing happens, then I tried to put inside a 1, execute a buildnumber:create and nothing happened. In the meantime, I just found the solution to the last problem using filtered properties. I am going to update my question with the full solution. – stivlo Aug 05 '11 at 05:11
  • 1
    @stivlo When you use filtered properties your properties file contains the string `foo=${buildNumber}` and changes the variable only for the built artifact. What if I am in development mode and want to get the build number from the properties file but not the `${buildNumber}` string? – Fagner Brack Aug 25 '14 at 13:50