Why excludesfile is not working? - ant

I have a list of class files to be excluded and i have added them in a file (say) exclude_class.txt as :
**/a/b/c/*.class
**/d/e/f/*.class
**/g/h/i/j/*.class
**/k/l/*.class
Now when I use excludesfile in fileset task it is not working:
<fileset dir=".">
<include name="A/**/*.class"/>
<include name="B/**/*.class:/>
<excludesfile name="exclude_class.txt"/>
</fileset>
Please let me know what is the issue here. What should be the syntax of file to use in excludesfile task.

excludesfile (and also excludes, includes, includesfile) is an attribute of <fileset> and not a nested tag. you may use it like this:
<fileset dir="." excludesfile="exclude_class.txt">
<include name="A/**/*.class"/>
<include name="B/**/*.class:/>
</fileset>
on the other hand, <include>, <exclude> are nested tags and may be used in the manner in which you've written.
as for the syntax within exclude_class.txt.. just make sure that there are no leading / trailing spaces in each line.

Related

Set ant property when comparing directories

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>

Exporting zipfilesets

We are currently generating a zip file using multiple targets as follows.
<zipfile>
<zipfileset dir="alpha" prefix="alpha" />
<zipfileset dir="beta" prefix="alpha" excludes="*.bar" />
<zipfileset dir="gamma/G" prefix="gamma" />
</zipfile>
A requirement has come up in that we need to generate (and include) a list of the included files and their corresponding MD5 checksum values.
If we use a <fileset>/<patternset>/<pathconvert> combination, I can get a text file containing all the files, and generate from there. However, I can't seem to find a way to do this with <zipfileset /> targets.
Is there a way to do a 'dry-run' and obtain a list of the targets that will be included? Or is there a (simple) method of extracting the required information from the generated ZIP itself?
If you have already generated file (with checksum) you can just add it with help of another fileset.
The sample:
<target name="ziptest">
<zip destfile="${src}\output.zip">
<fileset dir="${src}">
<include name="dir1\*"/>
<include name="dir2\fileprefix*"/>
</fileset>
<fileset dir="${src}">
<!-- You have property with filename: file.name.checksum-->
<include name="${file.name.checksum}"/>
</fileset>
</zip>
</target>

iajc fails to weave aspects from a jar but succeedes from class files

So I defined iajc task for my project that does intertype declarations just fine, then there is a separate jar task that creates a project.jar.
Then there is iajc task for junit test target, this task references the project.jar with the goal of weaving its ITDs into test classes like so:
<aspectpath>
<pathelement path="${dist}/project.jar"/>
<fileset dir="${lib.aspect}">
<include name="**/*.jar" />
<include name="**/*.zip" />
</fileset>
</aspectpath>
That does not work - compiler produces errors like class "com....Foo" has to implement method doThings() defined in interface Bar
Then I change the <aspectpath> to this:
<aspectpath>
<pathelement location="${build.dir}"/>
<fileset dir="${lib.aspect}">
<include name="**/*.jar" />
<include name="**/*.zip" />
</fileset>
</aspectpath>
which is basically using the intermediate class files generated by the compiler and it works great.
Then I find that if I use the outjar option instead of destdir in my iajc task then that jar can be used in <aspectpath>.
So a question and a comment then:
Q. is there some parameter that would make it work with the regular <jar> task?
C. the fact that I have to use outjar is not documented at all - the option to output a jar is mentioned but nowhere does it say that that is the only way to produce a jar that will work.

need to check if filename contains the date.default property in ant script

Here is my script:
`
description="--> emails any file from a specified location">
<tstamp>
<format property="date.default" pattern="yyyy-MM-dd"
offset="-1" unit="day" />
</tstamp>
<echo message="${date.default}" />
<for param="file">
<path>
<fileset dir="${report.dir}">
<include name="**/*file*.doc" />
<contains text=" ${date.default}"/>
</fileset>
</path>
</for>`
It looks like you're trying to iterate over the set of files with names that match both the word 'file' (as you use this in the include element) and the value of '${date.default}'. You probably don't need to use a selector for that - the include directive is usually enough for file name matches. For example, you might use:
<include name="**/*file*${date.default}*.doc" />
The contains selector is for matching content of files, rather than the file names. If you have a complex filename-based matching rule, then you may need to make use of the filename selector in combination with includes, and possibly excludes. But 'filename' selectors are normally only needed when selection is based on filename plus some other criteria.

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