问题
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!
回答1:
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.
回答2:
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();
回答3:
If you are using ant you can define this task:
<target name="version">
<exec executable="svn" output="svninfo.xml" failonerror="true">
<arg line="info --xml" />
</exec>
<xmlproperty file="svninfo.xml" collapseattributes="true" />
<echo message="SVN Revision: ${info.entry.commit.revision}"/>
<property name="revision" value="${info.entry.commit.revision}" />
</target>
and you the revision value where you want.
回答4:
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.
回答5:
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.
回答6:
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.
来源:https://stackoverflow.com/questions/166322/obtain-current-svn-revision-in-webapp