What is **/*.fileExtension used for in ant? - ant

I can't find a direct definition of how this works? Is it a regex of some type?
for ex.
<ftp action="del"
server="ftp.apache.org"
userid="anonymous"
password="me#myorg.com">
<fileset>
<include name="**/*.tmp"/>
</fileset>
what is the include name = double asterisks used for?
How is it different from this
<ftp action="list"
server="ftp.apache.org"
userid="anonymous"
password="me#myorg.com"
listing="data/ftp.listing">
<fileset>
<include name="**"/>
</fileset>

These are what Ant calls 'patterns' and are similar to file patterns in Unix with the addition of the '**' pattern.
A single * matches zero or more characters, ? matches one character.
When ** is used as the name of a directory in the pattern, it matches zero or more directories. For example: /test/** matches all files/directories under /test/
So
<include name="**/*.tmp"/>
matches any file ending in .tmp in any directory
<include name="**"/>
matches anything.
A longer description is here

Related

Why excludesfile is not working?

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.

ANT: Foreach not excluding files when target attrbute is used

I have a foreach tag to execute all the files within a particular directory. As part of the fileset I have excluded two files.
But irrespective of excluding the files, the param still passes the two files to the target "runpART"
I do not want to call the target for the excluded files.
Let me know if there is any way to do it. The following does not seem to work
<foreach target="runART" param="p.schedulerFile" inheritall="true" inheritrefs="true">
<path>
<fileset dir="${p.dir}" casesensitive="no">
<!--<include name="**\*.xml"/>-->
<excludesfile name="${pART.dir}\CodeCoverage_NSR.xml"/>
<excludesfile name="${pART.dir}\CodeCoverageRegression_HLR.xml"/>
</fileset>
</path>
</foreach>
You don't want <excludesfile >, which expects a file containing exclude patterns, you want
<exclude name="CodeCoverage_NSR.xml">
<exclude name="CodeCoverageRegression_HLR.xml">
Now, that's assuming you meant to have ${p.dir} == ${pART.dir}.
Exclude patterns in a fileset have to be relative to the root dir of the fileset (here, its dir="${p.dir}").
For more info, see: http://ant.apache.org/manual/Types/fileset.html

Deleting files matching a placeholders in another directory tree

I have two directory trees:
source/aaa/bbb/ccc/file01.txt
source/aaa/bbb/file02.txt
source/aaa/bbb/file03.txt
source/aaa/ddd/file03.txt
source/file01.txt
and
template/aaa/bbb/ccc/file01.txt
template/aaa/bbb/DELETE-file03.txt
template/aaa/DELETE-ddd
template/DELETE-file01.txt
Using Ant, I want to do three things. First, I want to copy any files from "template" into "source", such that all files not starting with "DELETE-" are replaced. For example, "source/aaa/bbb/ccc/file01.txt" would be replaced. This is straightforward with:
<copy todir="source" verbose="true" overwrite="true">
<fileset dir="template">
<exclude name="**/DELETE-*"/>
</fileset>
</copy>
Second, I want to delete all files in the "source" tree whose name matches a "DELETE-" file in the corresponding directory of the "template" tree. For example, both "source/aaa/bbb/file03.txt" and "source/file01.txt" will be deleted. I have been able to accomplish this with:
<delete verbose="true">
<fileset dir="source">
<present present="both" targetdir="template">
<mapper type="regexp" from="(.*[/\\])?([^/\\]+)" to="\1DELETE-\2"/>
</present>
</fileset>
</delete>
Third, I'd like to delete any directories (empty or not) whose names match in the same way. For example, "template/aaa/DELETE-ddd" and all file(s) under it would be deleted. I'm not sure how to construct a fileset that matches directories (and all the files under them) in the "source" tree where the directory has a DELETE-* file in the "template" tree.
Is this third task even possible with Ant (1.7.1)? I'd preferrably like to do this without writing any custom ant tasks/selectors.
It seems the root issue that makes this difficult is that ant drives selectors/filesets based on files found within the target directory of fileset. Normally, however, one would want to drive things from the list of DELETE-* marker files.
The best solution I've found so far does require some custom code. I chose the <groovy> task, but also could have used <script>.
The gist: create a fileset, use groovy to add a series of excludes that skip files and directories with a DELETE-* marker, then perform the copy. This accomplishes the second and third task from my question.
<fileset id="source_files" dir="source"/>
<!-- add exclude patterns to fileset that will skip any files with a DELETE-* marker -->
<groovy><![CDATA[
def excludes = []
new File( "template" ).eachFileRecurse(){ File templateFile ->
if( templateFile.name =~ /DELETE-*/ ){
// file path relative to template dir
def relativeFile = templateFile.toString().substring( "template".length() )
// filename with DELETE- prefix removed
def withoutPrefix = relativeFile.replaceFirst( "DELETE-", "")
// add wildcard to match all files under directories
def exclude = withoutPrefix + "/**"
excludes << exclude
}
}
def fileSet = project.getReference("source_files")
fileSet.appendExcludes(excludes as String[])
]]></groovy>
<!-- create a baseline copy, excluding files with DELETE-* markers in the template directories -->
<copy todir="target">
<fileset refid="source_files"/>
</copy>
To delete a directory and its contents use delete with nested fileset, i.e. :
<delete includeemptydirs="true">
<fileset dir="your/root/directory" defaultexcludes="false">
<include name="**/DELETE-*/**" />
</fileset>
</delete>
With attribute includeemptydirs="true" the directories will be deleted too.

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