parameterized depends targets in ant

169 Views Asked by At

I am working on a ant build script to deploy jars. i.e. to just update the final/beta application jar in specified folder. Its checks if the deployed jar is already up-to-date. If yes, it skips running the target using unless flag.

Below is the snippet of targets

<property name="deploy-dir-final" location="C:\Deploy\final" />
<property name="deploy-dir-beta" location="C:\Deploy\beta" />

<macrodef name="macro-deploy-jar">
   <attribute name="deploydir" default="C:\Deploy\beta" />
   <sequential>
      <echo>Deploying jar</echo>
      <copy overwrite="true" file="C:/project/application.jar" todir="@{deploydir}"/>
      <echo>Deployed</echo>
   </sequential>
</macrodef>


<target name="deploy-jar-final" depends="is-final-jar-up-to-date" unless="jar.isUpToDate">
    <task-deploy-jar deploy-dir-path="${deploy-dir-final}"/>
</target>

<target name="deploy-jar-beta" depends="is-beta-jar-up-to-date" unless="jar.isBetaUpToDate">
    <task-deploy-jar deploy-dir-path="${deploy-dir-beta}"/>
</target>


<target name="is-final-jar-up-to-date">
   <echo message="Checking if deployed final jar is up-to-date"/>
   <uptodate property="jar.isUpToDate" targetfile="${deploy-dir-final}/application.jar" >
      <srcfiles dir= "${output-dir}" includes="application.jar"/>
   </uptodate>
</target>

<target name="is-beta-jar-up-to-date">
   <echo message="Checking if deployed beta jar is up-to-date"/>
   <uptodate property="jar.isBetaUpToDate" targetfile="${deploy-dir-beta}/application.jar" >
      <srcfiles dir= "${output-dir}" includes="application.jar"/>
   </uptodate>
</target>

I have used macrodef for code reuse in case of deploy-jar targets. But before deploying I am checking if the existing jar is already up-to-date. Its done via depends attribute on targets. But I can see scope of code reuse here also as its only differ in paths. I did not get how we can pass parameters to depends targets.

Is there any way we can use something similar to macrodef for code reuse in this case? or can we use if condition on macordef so that it should only run if some property is set.

Or any other way I can achieve the same without having to write two targets to check final and beta jar, just to check if they are up-to-date.

1

There are 1 best solutions below

0
Rebse On

With an ant version > 1.9.1 you may use the if unless feature
Can be used for all tasks and nested elements, also with macrodefs.
Some stupid examples:

<project xmlns:if="ant:if">

 <macrodef name="foobar">
  <attribute name="foo"/>
  <attribute name="verbose"/>
  <sequential>
   <echo>@{foo}</echo>
   <echoproperties prefix="ant" if:true="@{verbose}"/>
  </sequential>
 </macrodef>

 <foobar verbose="yes" foo="1. bla bla bla bla.."/>
 <foobar verbose="no" foo="2. bla.."/>

</project>


<project
  xmlns:if="ant:if"
  xmlns:unless="ant:unless"
>

 <property name="foobar" value=""/>

 <echo if:blank="${foobar}">foobar blank !</echo>
 <echo unless:blank="${foobar}">foobar not blank !</echo>

</project>