File Enumeration with Ant - ant

I feel like I'm missing something obvious at the moment. I want to collect a set of dirs / files together in Ant. I know I can do it using a fileset with an optional patternset inside it, but that involves searching for files based a specific criterion - filetype, name etc.
I already know the paths to the files I want to collect. I have n properties which reference these paths. I want a way to collect these paths in a fileset, though I cannot find a way to do it.
This represents what I want to achieve (I know it isn't valid code, but hopefully it will describe what I mean):
<fileset>
<path>${src.dir}</path>
<path>${test.dir}</path>
<path>${third.party.src.dir}</path>
<path>${bin.dir}</path>
<path>${docs.build.txt}</path>
</fileset>

You could try using a files element.
<files>
<include name="${src.dir}/**/*.*">
<include name="${test.dir}/**/*.*">
<include name="${third.party.src.dir}/**/*.*">
<include name="${bin.dir}/**/*.*">
<include name="${docs.build.txt}">
</files>

Thanks for the answer which works fine, however I managed to achieve the same thing even more simply using path with nested pathelements:
<path
id="srcdirs">
<pathelement location="${src.dir}"/>
<pathelement location="${test.dir}"/>
<pathelement location="${assets.dir}"/>
</path>

Related

Why does ANT update the contents of a fileset after it was created, and can I override this?

I think this may be easiest explained by an example, so here goes:
<target name="test">
<fileset id="fileset" dir="target">
<include name="*"/>
</fileset>
<echo>${toString:fileset}</echo>
<touch file="target/test"/>
<echo>${toString:fileset}</echo>
</target>
Outputs:
test:
[echo]
[touch] Creating target/test
[echo] test
What I ideally want is to have the fileset stay the same so I can have a before/after set (in order to get a changed set using <difference>, so if you know of a way to skip right to that...).
I've tried using <filelist> instead, but I can't get this correctly populated and compared in the <difference> task (they're also hard to debug since I can't seem to output their contents). I also tried using <modified/> to select files in the fileset, but it doesn't seem to work at all and always returns nothing.
Even if there is an alternative approach I would appreciate a better understanding of what ANT is doing in the example above and why.
The path selector is evaluated on the fly. When a file is added, it will reflect in the set when you use it.
You may able to evaluate and keep it in variable using pathconvert. Then this can be converted back to filest using pathtofilest
A fileset is something like a selector. It's a set of "instructions" (inclusions, exclusions, patterns) allowing to get a set of files.
Each time you actually do something with the fileset (like printing the files it "references"), the actual set of files is computed based on the "instructions" contained in the fileset.
As Jayan pointed out it might be worth posting the final outcome as an answer, so here's a simplified version with the key parts:
<fileset id="files" dir="${target.dir}"/>
<pathconvert property="before.files" pathsep=",">
<fileset refid="files"/>
</pathconvert>
<!-- Other Ant code changes the file-system. -->
<pathconvert property="after.files" pathsep=",">
<fileset refid="files"/>
</pathconvert>
<filelist id="before.files" files="${before.files}"/>
<filelist id="after.files" files="${after.files}"/>
<difference id="changed.files">
<filelist refid="before.files"/>
<filelist refid="after.files"/>
</difference>

Creating an empty placeholder fileset in Ant

So, here's the situation: I have a parent buildfile that defines a compilation task, and I want child buildfiles to optionally be able to add more JARs (which could be wherever) on to the classpath used by that compilation task.
Not all child buildfiles will have these additional dependencies, so I don't want to force them to define the additional dependency fileset. They should just be able to include the parent, and the compile task should just work.
(Obviously there are other required properties that configure the source directory and so on, but they don't enter into this. Also, the actual include/inheritance problem is a good bit more complicated, but hopefully whatever the right thing is for the simple case will work in the complex case too.)
I have something that works: The compile task in the parent buildfile refers to the additional dependency fileset regardless:
<target name="compile" depends="init-additional-dependencies">
<fileset id="global.dependency.fileset" dir="${global.library.directory}">
<include name="**/*.jar"/>
</fileset>
<javac ...>
<classpath>
<!-- should be the same for all buildfiles -->
<fileset refid="global.dependency.fileset"/>
<!-- should be populated by child buildfiles -->
<fileset refid="additional.dependency.fileset"/>
</classpath>
</javac>
</target>
...and the parent buildfile also has a task that creates this fileset, empty, so that javac doesn't blow up. However, the way I'm creating the empty fileset is dopey:
<target name="init-additional-dependencies">
<!-- override me! -->
<fileset id="additional.dependency.fileset" dir=".">
<include name="placeholder.does.not.exist.so.fileset.is.empty"/>
</fileset>
</target>
This works, but seems dumb, and it's hard to believe there isn't a better approach. What is that better approach?
I don't think there's been much discussion about this, so no 'convention' as such exists. The way that fileset works though, exclusions 'trump' inclusions, thus
<fileset refid="additional.dependency.fileset" dir="." excludes="**" />
should always be empty. That seems slightly preferable to both your placeholder file name technique, and the placeholder directory name and erroronmissingdir method.
The problem arises because, by default, there is an implicit include of all files beneath the parent directory of a fileset. Another option - perhaps not of direct use in your case - is to use a filelist instead. Because filelists are constructed from explicitly named files, if you don't name any, they are empty.
<filelist id="additional.dependency.filelist" />
By generalising, you can mix filesets and filelists, if you modify your classpath to use resources:
<filelist id="additional.dependency.resources" />
...
<classpath>
<!-- should be the same for all buildfiles -->
<fileset refid="global.dependency.fileset"/>
<!-- should be populated by child buildfiles -->
<resources refid="additional.dependency.resources"/>
</classpath>
the reference additional.dependency.resources can be either a fileset or a filelist (including the empty filelist), or any other file-based resource collection.
In the parent build file add:
<fileset id="additional.dependency.fileset" erroronmissingdir="false" dir="noop" />
For children that require additional artifacts to be added, define the fileset in the child build file:
<fileset id="additional.dependency.fileset" dir="..." includes="..." />

Ant exclude file based on it's content

Is there any way to exclude files from an ant fileset based on the file content?
We do have test servers where code files are mixed up with files that have been generated by a CMS.
Usually, the files are placed in different folders, but there is a risk that real code files are in the middle of generated code.
The only way to differentiate generated files is to open the files and look at it's content. If the file contains a keyword, it should be excluded.
Does anyone know a way to perform this with Ant?
From the answer provided by Preet Sangha, Ishould use a filterchain. However, I'm missing a step here.
Let's say I load a text file of exclusions to be performed:
<loadfile property="exclusions" srcFile="exclusions.txt" />
But I don't know how to integrate it into my current copy task:
<copy todir="${test.dir}">
<fileset dir="${src.dir}">
</fileset>
</copy>
I tried to add the following exclude to the fileset but it does not do anything:
<exclude name="${exclusions}"/>
I'm sure I'm missing a simple step...
Have a look at the not and contains selectors.
The not selector contains an example of pretty much exactly what you're trying to do.
<copy todir="${test.dir}">
<fileset dir="${src.dir}">
<not>
<contains text="your-keyword-here"/>
</not>
</fileset>
</copy>
There's also the containsregexp selector which might be useful if your criteria for exclusion is more complicated.
There's a load more selectors you can use to refine your selection if needed.
I don't know ant but reading the docs....
Can you build a files list using a filterchain, and put this into the excludefiles of a fileset?
or
perhaps create a fileset with a filterchain that uses a filterreader and linecontainsregexp

Why `**/*ant*` exclude pattern doesn't work, but `"**/*ant*/**` works fine?

Let's say I have this in one of my targets:
<path id="files">
<fileset dir="${env.DIRECTORY}" casesensitive="false">
<include name="**/*.html"/>
<exclude name="**/*ant*"/>
</fileset>
</path>
I'd like to group all the html files, except the ones containing the string ant. The way I wrote it above, it does not work. I also tried specifying the exclude like this:
<exclude name="*ant*"/>
Please notice that the fileset has it's case sensitiveness turned off. However, if I write:
<exclude name="**/*ant*/**"/>
This does work. Why don't the first and second versions of exclude work?
First and second case don't match because you are searching for directory name containing ant
Third case matches all files that have a ant element in their path, including ant as a filename.
You can also refer this Ant documentation

How to suppress ant jar warnings for duplicates

I don't want ant's jar task to notify me every time it skips a file because the file has already been added. I get reams of this:
[jar] xml/dir1/dir2.dtd already added, skipping
Is there a way to turn this warning off?
This is an older question, but there is one obvious way to exclude the duplicates warning, do not include the duplicate files. You could do this in one of two ways:
Exclude the duplicate files in some fashion, or
Copy the files to a staging area, so that the cp task deals with duplicates, not the jar task.
So, instead of doing:
<jar destfile="${dist}/lib/app.jar">
<fileset dir="a" include="xml/data/*.{xml,dtd}"/>
<fileset dir="b" include="xml/data/*.{xml,dtd}"/>
<fileset dir="c" include="xml/data/*.{xml,dtd}"/>
</jar>
do one of:
<jar destfile="${dist}/lib/app.jar">
<fileset dir="a" include="xml/data/*.{xml,dtd}"/>
<fileset dir="b" include="xml/data/*.xml"/>
<fileset dir="c" include="xml/data/*.xml"/>
</jar>
or
<copy todir="tmpdir">
<fileset dir="a" include="xml/data/*.{xml,dtd}"/>
<fileset dir="b" include="xml/data/*.{xml,dtd}"/>
<fileset dir="c" include="xml/data/*.{xml,dtd}"/>
</copy>
<jar destfile="${dist}/lib/app.jar">
<fileset dir="tmpdir" include="xml/data/*.{xml,dtd}"/>
</jar>
<delete dir="tmpdir"/>
Edit: Base on the comment to this answer, there is a third option, although it is a fair bit more work... You can always get the source to the jar task, and modify it so that it does not print out the warnings. You could keep this as a local modification to your tree, move it to a different package and maintain it yourself, or try to get the patch pushed back upstream.
I don't know of any options on the jar task to suppress these messages, unless you run the whole build with the -quiet switch, in which case you may not see other information you want.
In general if you have lots of duplicate files it is a good thing to be warned about them as a different one may be added to that which you expect. This possibly indicates that a previous target of the build has not done its job as well as it might, though obviously without more details it is impossible to say.
Out of interest why do you have the duplicate files?

Resources