Run Ant with IF ELSE from Maven

You already know how to run Ant from Maven (if not, read Run Ant from Maven) , and you need to use IF ELSE, but you don’t know how to do that.

This page is for you.

Full Code:

3_maven_with_ant_with_if.zip

What you need to run this:

  • Maven 3

Explain:

Your goal is that you want to use IF ELSE in build.xml, like this:

<target name="install">
   <if>
      <equals arg1="${prop1}" arg2="YES" />
      <then>
         <echo message="The value of prop1 is YES" />
      </then>
      <else>
         <echo message="The value of prop1 is not YES" />
      </else>
   </if>
</target>

You need to use Ant-Contrib .  You have to download from ant-contrib-1.0b3.jar from https://sourceforge.net/projects/ant-contrib/files/

Assume that you have your project in:

c:/mavn_with_ant

Then keep the jar file in

c:/mavn_with_ant/ant_lib

Then add these line in your build.xml

<property name="runningLocation" location="" />
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
    <classpath>
        <pathelement location="${runningLocation}/ant_lib/ant-contrib-1.0b3.jar" />
    </classpath>
</taskdef>

The full code of build.xml looks like this:

<project name="mavn_with_ant_example" basedir=".">
    <property name="prop1" value="YES" />
    <property name="runningLocation" location="" />
    <taskdef resource="net/sf/antcontrib/antcontrib.properties">
        <classpath>
            <pathelement location="${runningLocation}/ant_lib/ant-contrib-1.0b3.jar" />
        </classpath>
    </taskdef>

    <target name="install">
        <if>
            <equals arg1="${prop1}" arg2="YES" />
            <then>
                <echo message="The value of prop1 is YES" />
            </then>
            <else>
                <echo message="The value of prop1 is not YES" />
            </else>
        </if>
    </target>

</project>

Then try to this command in console:

mvn install

Then you will see this in your console:

install:
      [echo] YES
      [echo] The value of prop1 is YES
 [INFO] Executed tasks
 [INFO] ------------------------------------------------------------------------
 [INFO] BUILD SUCCESS
 [INFO] ------------------------------------------------------------------------
 [INFO] Total time: 1.625 s
 [INFO] Finished at: 2017-12-09T15:20:57+01:00
 [INFO] Final Memory: 10M/162M
 [INFO] ------------------------------------------------------------------------