title: Maven build number plugin, how to save the build number in a file?
body: <p>I've a Java project using Spring Framework and Git and I wanted to display a build number. I found the <a href="http://mojo.codehaus.org/buildnumber-maven-plugin/" rel="noreferrer">Build Number Maven plugin</a>. With Git the build number is a Git hash. I dislike that and I thought a date was much more expressive.</p>
  
  <p>I found this <a href="http://blog.jayway.com/2009/06/04/maven-automatic-build-versioning-and-git-against-subversion/" rel="noreferrer">excellent blog article</a> 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.</p>
  
  <p>When I run "mvn package" it tells me:</p>
  
  <pre><code>[INFO] --- buildnumber-maven-plugin:1.0:create (default) @ sherd ---
  [INFO] Storing buildNumber: 2011-08-04_21-48_stivlo at timestamp: 1312487296631
  </code></pre>
  
  <p>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).</p>
  
  <p>Maybe I've to change the configuration to store the build number in a file? How can I use the build number value? </p>
  
  <hr>
  
  <p>Thanks to the hint of Michael-O, I read the chapter about <a href="http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files" rel="noreferrer">how to filter resource files in Maven Getting Started Guide</a>. I've created a file application.properties in src/main/resources/properties/application.properties with the following contents:</p>
  
  <pre><code># application properties
  application.name=${pom.name}
  application.version=${pom.version}
  application.build=${buildNumber}
  </code></pre>
  
  <p>I've added the following XML snippet within my build section:</p>
  
  
  
  <pre class="lang-xml prettyprint-override"><code>&lt;resources&gt;
      &lt;resource&gt;
          &lt;directory&gt;src/main/resources/properties&lt;/directory&gt;
          &lt;filtering&gt;true&lt;/filtering&gt;
      &lt;/resource&gt;
  &lt;/resources&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre class="lang-xml prettyprint-override"><code># application properties
  application.name=Sherd Control Panel
  application.version=1.0.1-SNAPSHOT
  application.build=2011-08-05_05-55_stivlo
  </code></pre>
  
  <p>Everything works fine from command line, but, sigh, m2eclipse gives Build errors:</p>
  
  <pre class="lang-xml prettyprint-override"><code>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.
  </code></pre>
  
  <p>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.</p>
  
  <hr>
  
  <p>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).</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;plugin&gt;
      &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
      &lt;artifactId&gt;buildnumber-maven-plugin&lt;/artifactId&gt;
      &lt;version&gt;1.0&lt;/version&gt;
      &lt;executions&gt;
          &lt;execution&gt;
              &lt;phase&gt;generate-resources&lt;/phase&gt;
              &lt;goals&gt;
                  &lt;goal&gt;create&lt;/goal&gt;
              &lt;/goals&gt;
          &lt;/execution&gt;
      &lt;/executions&gt;
      &lt;configuration&gt;
          &lt;doCheck&gt;false&lt;/doCheck&gt;
          &lt;doUpdate&gt;false&lt;/doUpdate&gt;
           &lt;revisionOnScmFailure&gt;true&lt;/revisionOnScmFailure&gt;
          &lt;format&gt;{0,date,yyyy-MM-dd_HH-mm}_{1}&lt;/format&gt;
          &lt;items&gt;
              &lt;item&gt;timestamp&lt;/item&gt;
              &lt;item&gt;${user.name}&lt;/item&gt;
          &lt;/items&gt;
      &lt;/configuration&gt;
  &lt;/plugin&gt;
  </code></pre>