简体   繁体   中英

How to check Ant version inside Ant script

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.

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>

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

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

Ant has built-in property ant.version :

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

Version 1.7 of ANT introduced a dedicated antversion task.

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

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 version 1.9.8 or newer is required (Apache Ant(TM) version 1.9.7 compiled on April 9 2016 is installed)

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

ant -version

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM