Som Sarkar
Som Sarkar

Reputation: 289

Failing a ant build if the Java version is 1.5 or 1.6

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?

Upvotes: 0

Views: 643

Answers (4)

user4524982
user4524982

Reputation:

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>

Upvotes: 0

Ed Randall
Ed Randall

Reputation: 7600

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>

Upvotes: 0

user432
user432

Reputation: 3534

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

Upvotes: 1

nils
nils

Reputation: 1382

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.

Upvotes: 0

Related Questions