如何使用ANT将版本号添加到WAR文件

时间:2014-11-04 07:05:11

标签: java ant

我有一个ANT构建文件,我在其中创建一个WAR文件。在此ANT文件中,我已将版本信息添加到WAR文件名。但正在发生的是,版本号的主要和次要版本正在同时更新。我的意思是说主要和次要的增量应该发生 1.1.0 1.2.0 : : 1.9.0 2.0.0

但正在发生的事情是

1.1.0 2.2.0 : : 9.9.0 10.10.0

我的Ant代码是

    <target name="war" depends="compile, sethostname, setcurrenttime, setdistdir_withversion">
    <echo>Load properties file</echo>
    <loadproperties srcFile="build_info.properties" prefix="current"/>

    <echo>Dist conf path : ${main.dist.dir}</echo>
    <copy todir="${main.dist.dir}/prop/conf" preservelastmodified="true">
        <fileset dir="${conf.dir}">
            <!-- <include name="**/*_server.xml"/> -->
            <include name="*_catalina_logging.properties"/>
        </fileset>

    <condition property="warfilename" value="${name}">
        <not>
            <isset property="warfilename"/>
        </not>
    </condition>

    <echo>Main Dist directory:${main.dist.dir}</echo>
    <echo>Dist directory:${dist.dir}</echo>
    <echo>WAR filename:${warfilename}</echo>
    <war destfile="${main.dist.dir}/${warfilename}.war"
          webxml="WebContent/WEB-INF/web.xml">
        <fileset dir="conf"/>
        <fileset dir="WebContent"/>
        <classes dir="${build.dir}"/>
    </war>
    <copy todir="${war.archive.dir}" preservelastmodified="true">
        <fileset dir="${main.dist.dir}">
            <include name="*.war"/>
        </fileset>
    </copy>
</target>

<target name="setwarfilename_withversion" depends="revision-deploywar">
    <loadproperties srcFile="build_info.properties" prefix="current"/>
    <echo>Current build number:${current.build.major.number}.${current.build.minor.number}.${current.build.revision.number}</echo>
    <property name="warfilename" value="${name}-${hostname}-${current.build.major.number}.${current.build.minor.number}.${current.build.revision.number}" />
    <echo>WAR filename set : ${warfilename}</echo>
</target>

<target name="sethostname" >
  <property environment="env"/>
  <condition property="hostname" value="${env.HOSTNAME}">
    <os family="unix"/>
  </condition>
  <condition property="hostname" value="${env.COMPUTERNAME}">
    <os family="windows"/>
  </condition>   
  <echo message="host = ${hostname}"/>
</target>

<target name="setcurrenttime" >
    <tstamp>
        <format property="build.time" pattern="MM/dd/yyyy hh:mm:ss aa zzzz" timezone="America/Los_Angeles"/>
    </tstamp>    
  <echo message="host = ${build.time}"/>
</target>

<target name="setbuildinfo">
    <propertyfile  file="build_info.properties">
        <entry key="build.user" value="${user.name}"/>
        <entry key="build.system" value="${hostname}"/>
        <entry key="build.time" value="${build.time}"/>
    </propertyfile>
</target>

<target name="revision">
    <echo>Revision</echo>
    <propertyfile  file="build_info.properties">
        <entry key="build.revision.number" type="int" operation="+" value="1" pattern="00"/>
    </propertyfile>
    <antcall target="setbuildinfo"/>
</target>

<target name="minor" >
    <echo>Minor</echo>
    <propertyfile  file="build_info.properties">
        <entry key="build.minor.number" type="int" operation="+" value="1" pattern="00"/>
    </propertyfile>
   <antcall target="setbuildinfo"/>
</target>

<target name="major" >
    <echo>Major</echo>
    <propertyfile  file="build_info.properties">
        <entry key="build.major.number" type="int" operation="+" value="1" pattern="00"/>
    </propertyfile>
    <antcall target="setbuildinfo"/>
</target>

<target name="revision-deploywar" depends="revision, minor, major" description="Deploy application as a WAR file"/>

1 个答案:

答案 0 :(得分:0)

问题是以下目标取决于revisionminormajor目标,这将导致所有3个数字递增。

<target name="revision-deploywar" depends="revision, minor, major" description="Deploy application as a WAR file"/>

通常主要编号是由开发人员手动递增的,而次编号在编译之后会在dist和修订版本之后递增。

有关详细信息,请查看以下答案:https://stackoverflow.com/a/1452157/3041189