Running an ant task only when a file has changed - ant

In Ant, I am trying to achieve a simple task: If couple of files are modified a compiler should run. I have seen many solutions using OutOfDate, UpToDate and Modified. I dont want to use OutOfDate and UpToDate because I wont be able to use the task if the files have been modified on the same day. I can use modified but there is no way to call another task - my compiler task, from the modifier task. Is there any other solution apart from these?

Using <uptodate> with a following <antcall> to a conditional <target> will give you what you're looking for:
<project name="ant-uptodate" default="run-tests">
<tstamp>
<format property="ten.seconds.ago" offset="-10" unit="second"
pattern="MM/dd/yyyy hh:mm aa"/>
</tstamp>
<target name="uptodate-test">
<uptodate property="build.notRequired" targetfile="target-file.txt">
<srcfiles dir= "." includes="source-file.txt"/>
</uptodate>
<antcall target="do-compiler-conditionally"/>
</target>
<target name="do-compiler-conditionally" unless="build.notRequired">
<echo>Call compiler here.</echo>
</target>
<target name="source-older-than-target-test">
<touch file="source-file.txt" datetime="${ten.seconds.ago}"/>
<touch file="target-file.txt" datetime="now"/>
<antcall target="uptodate-test"/>
</target>
<target name="source-newer-than-target-test">
<touch file="target-file.txt" datetime="${ten.seconds.ago}"/>
<touch file="source-file.txt" datetime="now"/>
<antcall target="uptodate-test"/>
</target>
<target name="run-tests"
depends="source-older-than-target-test,source-newer-than-target-test"/>
</project>

Related

How can I concat files with Ant if source files is changed?

How can I concat files with Ant if source files is changed?
I create such code but it is very complex to do such simple thing - want to do in more simple way - can I do it?
<target name="check.src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js">
<uptodate property="dirty.src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js"
targetfile="src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js">
<srcfiles file="src/web_site/seo/frontend/base/crud/yaml/base/list/static/list.js"></srcfiles>
<srcfiles file="src/web_site/seo/frontend/base/crud/yaml/keyword/list/js/list.js"></srcfiles>
</uptodate>
<echo message="result: ${dirty.src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js}"/>
</target>
<target name="src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js"
depends="check.src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js"
unless="dirty.src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js">
<echo message="concat"></echo>
<concat destfile="src/web_site/seo/frontend/base/crud/yaml/keyword/list/static/list.js"
fixlastline="true" >
<fileset file="src/web_site/seo/frontend/base/crud/yaml/base/list/static/list.js"></fileset>
<fileset file="src/web_site/seo/frontend/base/crud/yaml/keyword/list/js/list.js"></fileset>
</concat>
</target>
Since ant 1.8.2, you can use overwrite property of the concat task for that purpose.
Before version 1.8.2, you may use the force property instead.

Running an Ant target under certain conditions

I have the following Ant targets in my project.xml:
<target name="to.run.under.conditions">
</target>
<target name="deploy1">
<antcall target="deploy2"/>
</target>
<target name="deploy2">
<antcall target="to.run.under.conditions"/>
</target>
<target name="another.target">
<antcall target="deploy1"/>
</target>
My intent is to be able to exclude the target to.run.under.conditions when running another.target. I am not very familiar with ANT and I am struggling to understand how to approach the issue. I have tried to employ unless="${target.running}" in and set the property as true in the condition task inside the target name ="target.running"
Can you help with this?
Thank you for your help,
I.
----EDIT UPDATED SOLUTION----
This is my current attempt (I am using ANT 1.8.2):
<target name="to.run.under.conditions" if="${target.running}">
</target>
<target name="another.target">
<property name="target.running" value="false"/>
</target>
If I am not mistaken, since the property is set to false inside another.target, then to.run.under.conditions should not be run (I might be wrong, though). Does it make sense? Any comment is much appreciated!
Try this:
<target name="build-module-A" if="module-A-present"/>
<target name="build-own-fake-module-A" unless="module-A-present"/>
In the first example, if the module-A-present property is set (to any value, e.g. false), the target will be run. In the second example, if the module-A-present property is set (again, to any value), the target will not be run.
Please see Any Targets for more information.
I have ended up with this solution that seems to be work as expected:
<target name="deploy2">
<if>
<equals arg1="${target.running}" arg2="true" />
<then>
<echo message="the target will not run" />
</then>
<else>
<echo message="the target will run" />
<antcall target="to.run.under.conditions"/>
</else>
</if>
</target>
<target name="to.run.under.conditions">
</target>
<target name="another.target">
<property name="target.running" value="true"/>
</target>
Hope this helps,
I.

Loop through directory structure in ant

We want to loop through directory structure in ant without using foreach .
Is there any elegant way to do the same ?
The apply task can iterate over a set of directories or files
<target name="run-apply">
<apply executable="echo">
<dirset dir="src"/>
</apply>
</target>
I personally like the groovy ANT task
<target name="run-groovy">
<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>
<dirset id="dirs" dir="src"/>
<groovy>
project.references.dirs.each {
ant.echo it
}
</groovy>
</target>
The installation of the task jar is easily automated:
<target name="install-groovy">
<mkdir dir="${user.home}/.ant/lib"/>
<get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.1.1/groovy-all-2.1.1.jar"/>
</target>
Finally if you're iterating thru other build files, the subant task is very useful:
<target name="run-subant">
<subant>
<fileset dir="src" includes="**/build.xml"/>
</subant>
</target>
Short answer: Not really. There are ways around this, but I prefer the ant-contrib <for/> task for clarity and simplicity. With the <local/> task, you can now localize values of variables. Before, you sometimes had to use ant-contrib's <var/> task to reset the values, so you could loop through them over and over.
<for param="directory">
<fileset dir="${some.dir}"/>
<sequential>
<local name="foo"/>
<local name="bar"/> <!-- Properties that may change with each iteration -->
<!-- Here be dragons -->
</sequential>
</for>
It's clean, simple, and easy to understand. The big issue many people have with Ant Contrib is that not everyone may have it installed in their $ANT_HOME/lib directory. Far enough. So, if you use ant-contrib, put it as part of your project.
I'll put the ant-contrib jar in ${basedir}/antlib/antcontrib and then put this in my program:
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<fileset dir="${basedir}/antlib/antcontrib"/>
</classpath>
</taskdef>
Now, when someone checks out my project, they have ant-contrib already installed (since it's inside my project) and accessible (since I point my <taskdef> task at the location of ant-contrib.jar in my project).

Ant antcall a target that defines a property

In Ant I want to define a target (called A) that define a property and antcall it from another target (called B). I want that the target B, after antcalling the target A, can access the property defined in the target A.
For example:
<target name="B">
<antcall target="A" inheritAll="true" inheritRefs="true" />
<echo>${myprop}</echo>
</target>
<target name="A">
<property name="myprop" value="myvalue" />
</target>
However it doesn't work and <echo>${myprop}</echo> doesn't print myvalue (I think because the property myprop isn't defined in B).
Is there any way to do that?
According to the Apache Ant FAQ:
<target name="cond" depends="cond-if"/>
<target name="cond-if" if="prop1">
<antcall target="cond-if-2"/>
</target>
<target name="cond-if-2" if="prop2">
<antcall target="cond-if-3"/>
</target>
<target name="cond-if-3" unless="prop3">
<echo message="yes"/>
</target>
Note: <antcall> tasks do not pass property changes back up to the environment they were called from, so you wouldn't be able to, for example, set a result property in the cond-if-3 target, then do <echo message="result is ${result}"/> in the cond target.
In this respect, it is impossible to do what you want using antcall.
========== edit ===========
Try antcallback: AntCallBack is identical to the standard 'antcall' task, except that it allows properties set in the called target to be available in the calling target.
http://antelope.tigris.org/nonav/docs/manual/bk03ch20.html
Sample code pasted from the above page:
<target name="testCallback" description="Test CallBack">
<taskdef name="antcallback" classname="ise.antelope.tasks.AntCallBack" classpath="${antelope.home}/build" />
<antcallback target="-testcb" return="a, b"/>
<echo>a = ${a}</echo>
<echo>b = ${b}</echo>
</target>
<target name="-testcb">
<property name="a" value="A"/>
<property name="b" value="B"/>
</target>
Another approach is to refactor your targets into macros. You are trying to use targets like functions and they are just not intended to be used that way. I typically write the bulk of my logic as macros, so that I can compose it more easily into more complicated macros. Then I write simple wrapper targets for the command-line entry points that I need.
Rather than using <antcall>, why not just have target B depend on target A?
<target name="B" depends="A">
<echo>${myprop}</echo>
</target>
<target name="A">
<property name="myprop" value="myvalue" />
</target>
I think you want to use a param.
<project default="B">
<target name="B">
<antcall target="A">
<param name="myprop" value="myvalue"/>
</antcall>
</target>
<target name="A">
<echo>${myprop}</echo>
</target>
</project>
I surrounded this with a project tag and moved the echo statement into "A". My output says
B:
A:
[echo] myvalue
BUILD SUCCESSFUL
#alem0lars, since you said you would like to subdivide a target, let me offer a different solution (that unfortunately doesn't answer your original question).
<project default="mytarg">
<target name="mytarg">
<property name="tgt" value="build"/>
<antcall target="deps"/>
</target>
<target name="deps" depends="aTgt,bTgt"/>
<target name="aTgt">
<echo>"In aTgt doing a ${tgt}"</echo>
</target>
<target name="bTgt">
<echo>"In bTgt doing a ${tgt}"</echo>
</target>
</project>
This subdivides the build into aTgt and bTgt.
Output will be
aTgt:
[echo] "In aTgt doing a build"
bTgt:
[echo] "In bTgt doing a build"
deps:
BUILD SUCCESSFUL

In Ant, can I use a property inside a target's "depends"?

I m trying to do this with Ant:
<property name="test" value="123"/>
<target name="helloworld" depends="${test}"/>
But I'm getting error "Target ${test} does not exist in this project."
So I m guessing I can do this?
You can use the AntCall Task to call a Task inside another Task.
<project>
<target name="asdf">
<property name="prop" value="qwer" />
<antcall target="${prop}" />
</target>
<target name="qwer">
<echo message="in qwer" />
</target>
</project>
To make one depend on the other, you can set a parameter in the dependent task and check it in your calling task.
Rather than depends, you can check a property using the if attribute. See the manual for more details.
For example:
<target name="helloworld" if="test"/>
Note this only checks if the property is set (you can use unless to check if it is unset).
An alternative, more complex but powerful, approach is to use a nested condition on a depended target:
<target name="helloworld" depends="myTarget.check" if="myTarget.run">
...
</target>
<target name="myTarget.check">
<condition property="test">
<and>
<available file="foo.txt"/>
<available file="bar.txt"/>
</and>
</condition>

Resources