在webapp中获取当前svn修订版

问题描述:

在java webapp中显示/使用版本号的最佳方式是什么?在webapp中获取当前svn修订版

我们只是用ant来构建我们的.war档案,没有buildserver等。我希望如果$ ref可以在资源文件中写入某种类型的文件,但是只有在有问题的文件被提交时才会更新。我需要全球。

你会推荐什么?提交后触发器更新某些文件? 自定义蚂蚁脚本?有没有更不危险的做法呢? 或者它有我自己的版本号独立于svn更好。

编辑:伟大的建议!非常感谢答案!

有几个Ant任务可以为你做到这一点。

SvnAnt task来自底格里斯河是最古老的。

文档是here - 尤其要看看info元素,它将Subversion存储库的修订版号作为Ant属性公开,它称为rev。您可以使用普通的Ant替代机制将此值写入您的资源文件。

有人也提出了谷歌代码托管simillar (simpler) task - 从来没有使用过它,所以不能评论。

如果你已经在你的版本中安装了Ant,这两种方式对我来说都是最好的方式。

请参阅this thread

我从该主题中收到的最爱只是将$Id:$转储到您想要修订ID的代码中。当您执行导出时,SVN将用真实数据填充它。

如果你使用的是windows,你可以看看乌龟自带的SubWCRev.exe。

它给出了当前的存储库修订版,并将用上述代替$ WCREV $,你可以在你的web.xml中包含这个作为上下文参数,然后从那里获取它。

打包web应用之前,运行svn info并将输出重定向到WEB-INF/classes中的某个文件。当webapp启动时,解析这个文件,并将其保存在servlet上下文或类似的地方。在每个页面的页脚中,显示此版本 - 如果您正在使用类似Tiles或SiteMesh的内容,则此更改只需在一个文件中完成。

Maven用户可以尝试maven-buildnumber插件。

我们用下面的Ant任务包括在罐子属性SVN版本,与正在使用的

<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> 

其他包的版本一起然后你就可以在你的web应用访问它像这

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

如果您使用的蚂蚁,你可以定义这个任务:

<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> 

和您所需的修订值。

+0

我喜欢这个解决方案,因为它很简单并且开箱即用。 – amotzg 2012-10-22 14:16:55