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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10514222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:23:00  来源:igfitidea点击:

How to check Ant version inside Ant script

javaant

提问by Sumeet Jindal

My ant script only works with version >=1.8. I want to check the version in the script, so that it displays error if a lesser version installed.

我的 ant 脚本只适用于 >=1.8 的版本。我想检查脚本中的版本,以便在安装较低版本时显示错误。

采纳答案by Victor Sorokin

Ant has built-in property ant.version:

Ant 具有内置属性ant.version

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

回答by Cyril Sagan

Here's a code snip that may help:

这是一个可能有帮助的代码片段:

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

回答by user1075613

No need to create a target, you can use fail+antversionat the beginning of your script :

无需创建目标,您可以在脚本开头使用fail+ antversion

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

回答by Mark O'Connor

Version 1.7 of ANT introduced a dedicated antversiontask.

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

This functionality is part of several conditionsthat can be checked by ANT.

此功能是ANT 可以检查的几个条件的一部分。

回答by isapir

Add the following at the beginning of your build script:

在构建脚本的开头添加以下内容:

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

If the Ant version is lower than required, it will produce an error like this:

如果 Ant 版本低于要求,则会产生如下错误:

Ant version 1.9.8 or newer is required (Apache Ant(TM) version 1.9.7 compiled on April 9 2016 is installed)

需要 Ant 1.9.8 或更新版本(安装了 2016 年 4 月 9 日编译的 Apache Ant(TM) 1.9.7 版)

回答by rastaman

In terminal type, just execute:

在终端类型中,只需执行:

ant -version