tags:

views:

239

answers:

5

I would like to append the output JAR filename of a Netbeans project with some version number: something like 1.0, 2.0b or even a Subversion revision number. I can't seem to find anything on this, though. I am also not sure if this would the responsibility of the build system (Ant) or if the IDE (Netbeans) can delegate the process. Is there a centralised, clean way of doing this?

+4  A: 

I'm not sure if it's the best way, but we put it in MANIFEST.MF file like this:

Implementation-Version: 2.0b

We can get this value programmatically like this:

String version_num =  this.getClass().getPackage().getImplementationVersion();
Thanks, it would be great to access the version number programmatically.
phantom-99w
A: 

Well is done default by Maven. Even if you want name your jar file with more detailed information you can use build number plugin.

EDIT
At begining I misunderstood your question so following part relates to adding this information inside jar files.

You can do it yourself by creating manifest file. In Maven it you can tune proces of creating manifest file by additional configuration. I suppouse (I'm sure) that in Ant should be similar functionality.

cetnar
A: 

If you feel like using a tool to handle your builds then there are lots about, such as CruiseControl, which is ANT based and has pretty deep integration with your source code control.

I use it to automatically increment a build number and use that as the last digit in my version number for the jar, e.g. 1.4.168, where 168 is the build number. I am just about to get it to put a label into CVS just before the fetch with the build number so I know exactly what code is in the jar.

Simon
+4  A: 

IMO, this is the responsibility of the build system, not of the IDE. Let me say it in other way: don't rely on your IDE to build your project, use a build tool. Using an IDE is fine during development but being IDE dependent to build a project is not a good thing (what if you change your IDE tomorrow, what if you want to build your project on another machine/OS without that IDE, what if you want to build your project on a headless machine, what if you want to automate your build, what if someone wants to build that project and doesn't have that IDE, etc, etc). Really, this is what build systems are for.

Now, regarding your initial request, there are plenty ways to add a version number. One of them is to use the Ant's BuildNumber task:

This is a basic task that can be used to track build numbers.

It will first attempt to read a build number from a file (by default, build.number in the current directory), then set the property build.number to the value that was read in (or to 0, if no such value). It will then increment the number by one and write it back out to the file. (See the PropertyFile task if you need finer control over things such as the property name or the number format.)

Use it for example like this:

  <target name="jar" depends="compile">

     <property name="version.num" value="1.00"/>
     <buildnumber file="build.num"/>

      <jar destfile="foo-${version.num}-b${build.number}.jar"
           basedir="."
           includes="**/*.class"
      />
  </target>

Or you could indeed add subversion revision number. An easy way to do this seems to install the SVNAnt task and use the status task:

  <target name="revisionnumber">

     <!-- get the svn revision number -->
     <svn>
        <status path="application.cfm" revisionProperty="svn.revision" />
     </svn>

     <echo>Sandbox Revision: ${svn.revision}</echo>

  </target>

Finally, another option would be to use Maven instead of Ant which has a built-in version management feature as pointed out by cetnar.

Pascal Thivent
Excellent, thank you very much!
phantom-99w
A: 

you can use maven for vesion and read it from pom.

read this article: Embedding the maven version number at http://happygiraffe.net/blog/2008/10/01/embedding-the-maven-version-number/

davideconsonni