Ant Target Call

I would like to call target backup.yes only if the condition is true.

<condition property="directory.found.yes"> <equals arg1="${directory.found}" arg2="true"/> </condition> <antcall target="update.backup"/> 

Is there any way to do this.

+7
source share
2 answers

Instead of <antcall/> do the following:

Imagine that you call the target foo , and you want to make a backup earlier, but only if this condition exists:

 <target name="foo" depends="update.backup"> <..../> </target> <target name="update.backup.test"> <condition property="directory.found.yes"> <equals arg1="${directory.found}" arg2="true"/> </condition> </target> <target name="update.backup" depends="update.backup.test" if="directory.found.yes"> <.../> </target> 

The problem with <antcall/> is that it is used when the Ant dependency matrix is ​​used, and it is used to force the task to complete until another task completes. When you really abuse, you end up invoking the same task several times. I had a project that literally called each goal between 10 and 14 times, and there were more than two dozen goals. I rewrote the entire assembly without <antcall/> and, using the true dependency setting, reduced the build time by 75%.

In my experience, 90% of <antcall/> is due to poor control of goal dependence.

Suppose you want to fulfill the goal of foo . (The goal that the user really wants to fulfill), and before foo is called, you want to make a backup, but only if the directory really exists.

In the above expression, foo is called. It depends on update.backaup . The target update.backup , but it depends on update.backup.test , which will check if the directory really exists.

If the directory exists, the if clause in the update.backup task is true, and the task will be completed. Otherwise, if the directory does not exist, it will not be executed.

Note that update.backup first calls up any dependencies before checks to see if the property is checked in the if or unless parameter of the target object. This allows the target group to invoke the test before attempting to run it.

This is not just a side effect, but a built-in Ant design. In fact, Ant Manual on Targets] ( http://ant.apache.org/manual/targets.html ) gives a very similar example:

 <target name="myTarget" depends="myTarget.check" if="myTarget.run"> <echo>Files foo.txt and bar.txt are present.</echo> </target> <target name="myTarget.check"> <condition property="myTarget.run"> <and> <available file="foo.txt"/> <available file="bar.txt"/> </and> </condition> </target> 

And indicates:

Important : if and if attributes only activate or deactivate the target to which they are attached. They do not control whether the goals, which the conditional goal depends on, are fulfilled. In fact, they are not even evaluated until the goal is completed and all its predecessors are already running.

+10
source

You can do the following

For another purpose:

 <antcall target="update.back"> <param name="ok" value="${directory.found.yes}"/> </antcall> 

And in target.backup:

 <target name="update.backup" if="ok"> 

But I think you can also do the following with the if statement from ant-contrib :

 <if> <equals arg1="${directory.found.yes}" arg2="true" /> <then> <antcall target="update.back" /> </then> </if> 
+6
source

All Articles