如何在Ant脚本中检查Ant版本

19
我的Ant脚本只适用于版本大于等于1.8。我想在脚本中检查版本,以便在安装更新版本之前显示错误信息。

https://dev59.com/3G435IYBdhLWcg3w7k2b#5126301 - assylias
我想在脚本内部以自动化的方式完成它。 - Sumeet Jindal
6个回答

12
这里有一段代码,可能会有所帮助:
<property name="version.required" value="1.8" />

<target name="version_check">
    <antversion property="version.running" />
    <fail message="FATAL ERROR:  The running Ant version, ${version.running}, is too old.">
        <condition>
            <not>
                <antversion atleast="${version.required}" />
            </not>
        </condition>
    </fail>
</target>

<target name="doit" depends="version_check">
    <echo level="info" message="The running version of ant, ${version.running}, is new enough" />
</target>


9

不需要创建目标,你可以在脚本开头使用failantversion

<fail message="Ant 1.8+ required">
     <condition>
         <not><antversion atleast="1.8" /></not>
     </condition>
</fail>

1
这是这里最好的答案。 - mcsilvio

7

Ant内置属性ant.version

<project default="print-version">
    <target name="print-version">
        <echo>${ant.version}</echo>
    </target>
</project>

1
从1.7版本开始,使用<antversion>任务是检查Ant版本的首选方法。 - Kevin Krouse

5

ANT的版本1.7引入了一个专门的antversion任务。

这个功能是ANT可以检查的几个条件之一。


0
在您的构建脚本开头添加以下内容:
<!-- Check Ant Version -->
<property name="ant.version.required"       value="1.9.8" />

<fail message="Ant version ${ant.version.required} or newer is required 
      (${ant.version} is installed)">
  <condition>
    <not><antversion atleast="${ant.version.required}" /></not>
  </condition>
</fail>

如果Ant版本低于所需版本,将会产生以下错误:

需要Ant版本1.9.8或更高版本(已安装Apache Ant(TM)版本1.9.7,编译日期为2016年4月9日)


0

在终端中输入,只需执行:

ant -version

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接