Set ant property when comparing directories - ant

I have two directories that I need to compare for having same files. I succesfully do this as follows:
<fileset dir="d:\test" id="onlyinbar">
<not>
<present targetdir="A_DIR"/>
</not>
</fileset>
<echo>these files are only in bar : ${toString:onlyinbar}</echo>
<fileset dir="A_DIR" id="differentbarfoo">
<different targetdir="d:\test" ignoreFileTimes="true"/>
</fileset>
<echo>these files are different in bar compared to foo : ${toString:differentbarfoo}</echo>
However, I need to issue an other task if either of these are true. So long the <condition> tag does seem to support only file to file comparison and I cannot see how to assign a property within the <fileset> tag. Help will be appreciated.

We needed to avoid any third party contribs for this solution. My major problem was the combination of the tags. We knew that our "tests" i.e fileset tags had to be in the condition but didn't know how. The resourcecount though short out the issue:
<target name="export-report-icons" description="A description">
<condition property="test2" else="false">
<or>
<resourcecount when="gt" count="0" property="flength">
<fileset dir="d:\test" id="onlyinbar">
<present targetdir="DIRs_A" present="srconly"/>
</fileset>
</resourcecount>
<resourcecount when="gt" count="0" property="flength">
<fileset dir="DIR_A" id="differentbarfoo">
<different targetdir="d:\test" ignoreFileTimes="true"/>
</fileset>
</resourcecount>
</or>
</condition>
</target>
<target name="copyThis" depends="export-report-icons" if="${test2}">
....
</target>
So what this does, sets an OR for the case that one of the two filesets succeeds, the counter wraps the fileset resource so that it can be hosted under the condition and the condition has a property true or false depending on ORed counts. The "copy-This" target executes if the ${test2} is true. Please note that if you set id=test2 it will always qualify as true as in this case it checks for value presence.

The <union> set operator groups resources from multiple collections into one collection.
Similarly, take a look at <intersect> and <difference>.
You mention "if", which I assume refers to the <if> task from the third-party Ant-Contrib library. Here's an example that echoes if the filesets combined by <union> match any files:
<if>
<resourcecount when="gt" count="0">
<union id="Check">
<resources refid="onlyinbar"/>
<resources refid="differentbarfoo"/>
</union>
</resourcecount>
<then>
<echo>There are differences: ${toString:Check}</echo>
</then>
</if>

Related

Ant: If property contains certain string, change the name of the property

I am trying to check a folder's name, and if it contains a certain string, I want that folder path to be changed.
So far I came up with this:
<property name="component.release.dir" value="${install.dir}/${component.name}" />
<!-- Check if the component is a part of projectL -->
<condition property="projectLFolderSpotted">
<matches pattern="projectL" string="${component.release.dir}"/>
<!-- if so, put the component in an appropriate folder -->
<property name="component.release.dir" value="${install.dir}/projectL/${component.name}" />
<echo message="projectL component has been detected, and moved accordingly!"/>
</condition>
But I get the following error:
condition doesn't support the nested "property" element.
Is there a way to achieve this?
Thanks in advance.
Properties in ANT are immutable, once they're assigned a value it doesn't change.
Here's how I'd suggest you do it:
<project name="demo" default="build">
<property name="release.dir.seed" location="build/helloworld"/>
<condition property="release.dir" value="build/found/helloworld" else="build/notfound/helloworld">
<contains string="${release.dir.seed}" substring="helloworld"/>
</condition>
<target name="build">
<echo message="Result: ${release.dir}"/>
</target>
</project>

Where "if" task can be inserted?

I can see, that "if" tasks can be used in "target" root or anywhere, where some action is expected. But can I use it when I'm specifying some parameter?
For example there's ant-javafx task fx:deploy, in which we can declare classpath:
<fx:deploy ...>
<fx:resources>
<fx:fileset dir="..." ...>
<fx:fileset dir="..." ...>
</fx:resources>
</fx:deploy>
The question is can I use "if" in that usecase? Example:
<fx:deploy ...>
<fx:resources>
<if>
<available file="${lib.dir}" type="dir" />
<then><fx:fileset dir="${lib.dir}" ...></then>
</if>
<fx:fileset dir="..." ...>
</fx:resources>
</fx:deploy>
Okay, where are you getting your <if> tasks?
Are these from Ant-Contrib? If so, these are tasks and not sub-entities that can be used with in a task.
However, it MIGHT be possible to define a resource with in an <if> statement:
<if>
<avaliable file="${lib.dir}" type="dir"/>
<then>
<fileset dir="${lib.dir}" id="lib.fileset">
<includes name="..."/>
</fileset>
</then>
<else>
<fileset dir="${foo.dir}" id="lib.fileset">
<include name="..."/>
</fileset>
</else>
</fi>
Now you would have a fileset with an id of lib.fileset that could be one of two different definitions. You can then use that as part of a sub-entity:
<jar destfile="${jar.name}">
<fileset refid="lib.dir"/>
</jar>
I said MIGHT because I've never tried this, but I really can't see why it wouldn't. I have never used the JavaFX tasks, so I didn't want to give an example with that, but the documentation does say that <fx:resources> can use a reference id.

Ant: Conditional Copy

I want to overwrite the hosts file on the Windows machine if the user allows it:
<input message="Do you want to overwrite the HOSTS file?"
addproperty="overwrite.hosts" validargs="yes,no" />
<copy tofile="${env.WINDIR}/system32/drivers/etc/hosts.backup">
<fileset file="${env.WINDIR}/system32/drivers/etc/hosts" />
</copy>
<copy todir="${env.WINDIR}/system32/drivers/etc">
<fileset file="${trainer.dir}/hosts" />
</copy>
How do I do the copies only if the user says yes?
EDIT:
I tried this:
<input message="Do you want to overwrite the HOSTS file?" addproperty="overwrite.hosts" validargs="yes,no" />
<if>
<equals arg1="${overwrite.hosts}" arg2="yes" />
<then>
<copy tofile="${env.windir}/system32/drivers/etc/hosts.backup">
<fileset file="${env.windir}/system32/drivers/etc/hosts">
</fileset>
</copy>
<copy todir="${env.windir}/system32/drivers/etc">
<fileset file="${trainer.dir}/hosts">
</fileset>
</copy>
</then>
</if>
and I get this output:
C:\trainer\build.xml:16: Problem: failed to create task or type if
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
I'm an ant rookie... What do I need to do?
You could use a condition or a if task for that. (The latter is part of the ant-contrib project.)
You can use an "if" parameter on a target to make it conditional on the property being set.
I've never used the "input" task -- I didn't know it existed until just now (thanks for the heads up!) -- but a quick look at the documentation indicates that it sets the named property to the value entered, i.e. after an "input" the property is always set. So I guess you would need a "condition" to test the value and set or not set some other property.
Something like this. I just ran a quick test and this does work. Namely, if you answer the question "y" it prints the message, and if you answer "n" it does not.
<project name="test" default="do.whatever">
<target name="decide.do.whatever">
<input message="So you wanna do this or not?" validargs="y,n" addproperty="wanna"/>
<condition property="wanna.yes">
<equals arg1="${wanna}" arg2="y"/>
</condition>
</target>
<target name="do.whatever" depends="decide.do.whatever" if="wanna.yes">
<echo message="Yeah he wannas."/>
</target>
</project>

Ant: how to write optional nested elements

Say that I need to do something like:
<copy todir="${DEPLOYMENT_DIR}" overwrite="true">
<fileset dir="dir1" />
<fileset dir="dir2" />
<fileset dir="dir3" />
...
<if>
<equals arg1="${SPECIAL_BUILD}" arg2="true"/>
<then>
<fileset dir="dir7" />
<fileset dir="dir8" />
...
</then>
</if>
</copy>
(The real task is not copy, I'm just using it to illustrate the point.)
Ant will complain that my task doesn't support nested <if> which is fair enough. I've been thinking along these lines:
I could add a macrodef with an "element" attribute like this:
<macrodef name="myCopy">
<element name="additional-path" />
<sequential>
<copy todir="${DEPLOYMENT_DIR}" overwrite="true">
<fileset dir="dir1" />
<fileset dir="dir2" />
<fileset dir="dir3" />
...
<additional-path/>
</copy>
</sequential>
</macrodef>
But that would mean that the caller (target) must specify the additional path which I want to avoid (if many targets call this task, they would have to repeat the fileset definitions in the additional-path element).
How to code the additional filesets inside the macrodef so that Ant doesn't complain?
AntContrib has an Ant FileSet object augmented with if and unless conditions.
http://ant-contrib.sourceforge.net/fileset.html
if Sets the property name for the 'if' condition. The fileset will be
ignored unless the property is
defined. The value of the property is
insignificant, but values that would
imply misinterpretation ("false",
"no") will throw an exception when
evaluated.
unless Set the property name for the 'unless' condition. If named
property is set, the fileset will be
ignored. The value of the property is
insignificant, but values that would
imply misinterpretation ("false",
"no") of the behavior will throw an
exception when evaluated.
You could use it like this:
<copy todir="${DEPLOYMENT_DIR}" overwrite="true">
<fileset dir="dir1" />
<fileset dir="dir2" />
<fileset dir="dir3" />
...
<fileset dir="dir7" if="SPECIAL_BUILD" />
<fileset dir="dir8" if="SPECIAL_BUILD" />
</copy>
One way (not sure if a good one) to achieve that is to create two macrodefs - one "public" for general use and one "internal" that does the real work and is intended to be called only from the "public" macro. Like this:
<macrodef name="task-for-public-use">
<sequential>
<if>
<equal arg1="${SPECIAL_BUILD}" arg2="true" />
<then>
<internal-task>
<additional-path>
...
</additional-path>
</internal-task>
</then>
<else>
<internal-task ... />
</else>
</if>
</sequential>
</macrodef>
<macrodef name="internal-task">
<element name="additional-path" />
<sequential>
<copy ...>
...
<additional-path/>
</copy>
</sequential>
</macrodef>
I don't like it much though and hope there's a better way.

Using mapper & fileset to copy files into a different subdirectory?

I want to create an Ant target that copies files in a directory to a destination directory with the same folder structure, plus one more subfolder appended.
For example, the source is:
a/b/c/foo.pdf
d/e/f/bar.pdf
I want the destination to be:
a/b/c/x/foo.pdf
d/e/f/x/bar.pdf
Here is my target so far, but it doesn't appear to be doing anything:
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" casesensitive="yes">
<include name="**${file.separator}foo.pdf" />
</fileset>
<mapper type="glob"
from="foo.pdf" to="x${file.separator}foo.pdf" />
</copy>
What am I missing?
You could use a regexp mapper:
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" casesensitive="yes">
<include name="**/*.pdf"/>
</fileset>
<mapper type="regexp" from="^(.*)/(.*\.pdf)" to="\1/x/\2" />
</copy>
I've used hard-coded file.separators to shorten. Basically, you split the path to the input file (from) into directory and filename (capture \1 and \2) and then insert the \x extra element between them (to).
I'm not clear on your example - it looks like you want to match 'bar.pdf' and rename it to 'foo.pdf', as well as changing the directory. If you need to do that, you might consider chaining a couple of simpler regexp mappers, rather than trying to cook up one complex one:
<copy todir="${dest.dir}">
<fileset dir="${src.dir}" casesensitive="yes">
<include name="**/*.pdf"/>
</fileset>
<chainedmapper>
<mapper type="regexp" from="^(.*)/(.*\.pdf)" to="\1/x/\2" />
<mapper type="regexp" from="^(.*)/(.*\.pdf)" to="\1/foo.pdf" />
</chainedmapper>
</copy>
When using a glob mapper, you need to specify one wildcard * in the from field:
Both to and from are required and
define patterns that may contain at
most one *. For each source file that
matches the from pattern, a target
file name will be constructed from the
to pattern by substituting the * in
the to pattern with the text that
matches the * in the from pattern.
Source file names that don't match the
from pattern will be ignored.
So something like this might work:
<mapper type="glob" from="*/foo.pdf" to="*/x/foo.pdf" />

Resources