views:

1224

answers:

5

what is the best way of displaying/using the revision number in a java webapp?

we just use ant to build our .war archive, no buildserver or such. i'd hope there was some kind if $ref that i could write in a resource file, but this is only updated when the file in question is committed. i need it globally.

what would you recommend? post-commit triggers that update certain files? custom ant scripts? is there a more non-hacky way of doing this? or it it better to have my own version number independent of svn.

edit: great suggestions! thanks a lot for the answers!

A: 

See this thread.

My favourite from that thread is just dumping $Id:$ in your code where you want the revision ID. SVN will populate that with the real data when you do an export.

Oli
+1  A: 

If you are using windows you could look at SubWCRev.exe which comes with tortoise.

It gives the current repository revision and will replace $WCREV$ with said, you could include this in your web.xml as say a context param and then get it from there.

Paul
+1  A: 

There are a couple of Ant tasks that can do this for you.

SvnAnt task from tigris is the oldest.

Documentation is here - in particular take a look at the info element which exposes the Subversion repository's revision number as an Ant property which it calls rev. You can write this value to your resouces file using the normal Ant substituion mechanisms.

Someone has also put up a simillar (simpler) task on google code hosting - never used it though so can't comment.

Either of these seem like the neatest way to me if you already have Ant in your build.

serg10
A: 

Before the webapp is packaged, run svn info and redirect the output to some file in WEB-INF/classes. When the webapp starts up, parse this file and have it stashed away in the servlet context or some similar place. In the footer of every page, display this version - if you are using something like Tiles or SiteMesh, this change needs to be done only in one file.

Maven users can try the maven-buildnumber plugin.

binil
+3  A: 

We use the following ant task to include the svn version in a attribute in the jar, along with the version of other packages that are being used

<target name="package" depends="compile" description="Package up the project as a jar">
<!-- Create the subversion version string -->
<exec executable="svnversion" failifexecutionfails="no" outputproperty="version">
  <arg value="."/> 
  <arg value="-n"/>
</exec>
<!-- Create the time stamp -->
<tstamp>
  <format property="timeAndDate" pattern="HH:mm d-MMMM-yyyy"/>
</tstamp>

<jar destfile="simfraserv.jar">
  <manifest>
    <attribute name="Built-By"                value="${user.name} on ${time.date}" />
    <attribute name="Implementation-Version"  value="${svn.version}" />
    <attribute name="Implementation-Java"     value="${java.vendor} ${java.version}" />
    <attribute name="Implementation-Build-OS" value="${os.name} ${os.arch} ${os.version}" />
    <attribute name="JVM-Version"             value="${common.sourcelevel}+" />        
  </manifest>
  <fileset dir="bin">
    <include name="**/*.class"/>
  </fileset>
  <fileset dir="src">
    <include name="**"/>
  </fileset>
</jar>
</target>

And then you can access it in your webapp like this

String version = this.getClass().getPackage().getImplementationVersion();
KeithB