Can I set the project version with a buildnumber-maven-plugin?

Peter Kahn picture Peter Kahn · Aug 4, 2009 · Viewed 26.4k times · Source

I'm trying to add the svn.revision to project version as a build number and can't seem to do so. My jar has the correct name durin packaging, but its installed in the my local repository it is as if ${buildNumber} is/was undefined when the version was set.

I get foo-1.0.0-SNAPSHOT-${buildNumber} instead of foo-1.0.0-SNAPSHOT-304

Any idea what I'm doing wrong or is adding a revision to the project version a bad idea? Thanks for the help.

<project>
  ...
  <version>1.0.0-${release.identifier}-${buildNumber}</version>
  <properties>
    <release.identifier>SNAPSHOT</release.identifier>
  </properties>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <executions>
          <execution>
            <id>useLastCommittedRevision</id>
            <goals>
              <goal>create</goal>
            </goals>
            <configuration>
              <useLastCommittedRevision>true</useLastCommittedRevision>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  ...
</project>

Answer

Rich Seller picture Rich Seller · Aug 8, 2009

The problem has two parts:

  1. You're trying to set the buildNumber into the version before it is resolved so it will always be ${buildNumber} rather than the resolved value.

    Instead of trying to dynamically change the version, you should set the buildNumber into the finalName element in the build. This will create the artifacts with the intended name in the local repository.

  2. The install plugin will ignore the finalName and deploy it as 1.0.0-SNAPSHOT regardless, I don't know of a way to address that. The buildNumber is added to the Manifest if you configure the plugin as below.

    So your configuration would be something like:

    <version>1.0.0-${release.identifier}</version>
    ...
    <build>
      <finalName>${project.artifactId}-${project.version}-${buildNumber}</finalName>
      ...
    </build>
    

I would avoid using build numbers on SNAPSHOT projects.

Maven provides the SNAPSHOT keyword to signify a volatile project in active development. So if you reference a project with a SNAPSHOT dependency version, Maven will automatically check for updates and keep your dependencies in sync.

If you then add a build number to the end of that version, you will have to manually update the dependencies, so you lose any benefit of having the SNAPSHOT suffix.

I personally avoid using build numbers where possible anyway. If I have to update a project, I just bump the version number, or use a suffix like beta-2 or RC2. If you need to track the revision in the SNAPSHOT, I'd recommend adding it to the Manifest so you can check where the build originated, but use the standard SNAPSHOT suffix to allow Maven to resolve the versions normally. The configuration below shows how to add the revision to the Manifest.

As far as your configuration is concerned, it looks OK to me assuming your SCM url is set up correctly. If you have no SCM configuration in your POM that may be the problem.

Can you run with -X and check for any output from the plugin indicating why it isn't setting the property?

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>buildnumber-maven-plugin</artifactId>
  <version>0.9.4</version>
  <executions>
    <execution>
      <id>useLastCommittedRevision</id>
      <phase>validate</phase>
      <goals>
        <goal>create</goal>
      </goals>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.1</version>
  <configuration>
    <archive>
      <manifest>
        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
      </manifest>
      <manifestEntries>
        <Implementation-Build>${buildNumber}</Implementation-Build>
      </manifestEntries>
    </archive>
  </configuration>
</plugin>