0

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.

Kuldeep Singh
  • 1,214
  • 4
  • 18
  • 45
  • Below link might be useful to pass param https://stackoverflow.com/questions/25448523/call-ant-target-multiple-times-with-different-parameters – Snehal Patel Aug 30 '18 at 11:02
  • Thanks.That is similar to what I am doing for deploy-jar. I was more interested in how to call depends targets with parameters. – Kuldeep Singh Aug 30 '18 at 11:06
  • You can't pass specific parameters to depends targets. As posted above, the generally agreed upon way to do this would be to make a top level target that uses `antcall` to call generic targets. – CAustin Aug 30 '18 at 16:34

1 Answers1

0

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>
Rebse
  • 10,307
  • 2
  • 38
  • 66