Question

I have an ant build file, which first checks what is the version of JAVA in my system. the code is like this-

<target name="-private.check.java">
  <echo message="Ant java version: ${ant.java.version}" />
</target>

The output is -

-private.check.java:
 [echo] Ant java version: 1.7

Now I want to make Build Fail if the java version is less than 1.7, how can I do that?

Was it helpful?

Solution

<target name="checkJava">
    <fail message="Unsupported Java version: ${java.version}. Make sure that the Java version is 1.7.">
        <condition>
            <not>
                <or>
                    <contains string="${java.version}" substring="1.7" casesensitive="false" /> 
                    <contains string="${java.version}" substring="1.8" casesensitive="false" /> 
                </or>
            </not>
        </condition>
    </fail>
</target>

OTHER TIPS

The approach I know would be to use the following:

<condition property="version1.6">  
  <or>     
    <equals arg1="1.5" arg2="${ant.java.version}"/>  
    <equals arg1="1.6" arg2="${ant.java.version}"/>  
  </or>     
 </condition>

Plus you have to negate it probably.

I use the test below; There's a subtle different between ${ant.java.version} (the Java version which is being used to run Ant) and ${java.version} (The Java which will be found on the PATH by forked tasks) so you may need to modify the property used according to your needs.

<fail message="ERROR: This project requires Java 7 or above; you are using ${java.version}.">
    <condition>
        <matches string="${java.version}" pattern="^1\.[0-6]"/>
    </condition>
</fail>

The approach taken by the Ant team itself is to verify that something that has been added in Java7 is actually present. This way you don't need to change your build file when Java9 comes out - or get surprised if people use Java 1.4.

<fail message="Java7 is required to build">
  <condition>
    <not>
      <available classname="java.nio.file.FileSystem"/>
    </not>
  </condition>
</fail>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top