2008-10-03 85 views
9

在java webapp中顯示/使用版本號的最佳方式是什麼?在webapp中獲取當前svn修訂版

我們只是用ant來構建我們的.war檔案,沒有buildserver等。我希望如果$ ref可以在資源文件中寫入某種類型的文件,但是隻有在有問題的文件被提交時纔會更新。我需要全球。

你會推薦什麼?提交後觸發器更新某些文件? 自定義螞蟻腳本?有沒有更不危險的做法呢? 或者它有我自己的版本號獨立於svn更好。

編輯:偉大的建議!非常感謝答案!

回答

3

有幾個Ant任務可以爲你做到這一點。

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

文檔是here - 尤其要看看info元素,它將Subversion存儲庫的修訂版號作爲Ant屬性公開,它稱爲rev。您可以使用普通的Ant替代機制將此值寫入您的資源文件。

有人也提出了谷歌代碼託管simillar (simpler) task - 從來沒有使用過它,所以不能評論。

如果你已經在你的版本中安裝了Ant,這兩種方式對我來說都是最好的方式。

1

請參閱this thread

我從該主題中收到的最愛只是將$Id:$轉儲到您想要修訂ID的代碼中。當您執行導出時,SVN將用真實數據填充它。

1

如果你使用的是windows,你可以看看烏龜自帶的SubWCRev.exe。

它給出了當前的存儲庫修訂版,並將用上述代替$ WCREV $,你可以在你的web.xml中包含這個作爲上下文參數,然後從那裏獲取它。

0

打包web應用之前,運行svn info並將輸出重定向到WEB-INF/classes中的某個文件。當webapp啓動時,解析這個文件,並將其保存在servlet上下文或類似的地方。在每個頁面的頁腳中,顯示此版本 - 如果您正在使用類似Tiles或SiteMesh的內容,則此更改只需在一個文件中完成。

Maven用戶可以嘗試maven-buildnumber插件。

6

我們用下面的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(); 
4

如果您使用的螞蟻,你可以定義這個任務:

<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

相關問題