How to Use Macrodef?

Lets assume this is my code. buildJar is my macrodef.

    <target name="build"> 
     <buildJar build.dir="module1"/>
     <buildJar build.dir="module2"/>
     </target>

How to invoke macrodef "buildJar" based on some condition? For example, above code can be:

    <target name="build">
        <if module="module1" >
            <buildJar build.dir="module1"/>
        </if>
        <if module="module2" >
           <buildJar build.dir="module2"/>
        </if>
    </target>
1

1 Answer

Ant supports if and unless attributes. These can be combined with a directory check using the available task:

<project name="demo" default="build" xmlns:if="ant:if">

  <macrodef name="greeting">
    <attribute name="name"/>
    <sequential>
      <echo message="found @{name}"/>
    </sequential>
  </macrodef>

  <available property="found.module1" file="module1"/>
  <available property="found.module2" file="module2"/>

  <target name="build">
    <greeting name="module1" if:true="${found.module1}"/>
    <greeting name="module2" if:true="${found.module2}"/>
  </target>

</project>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest