Problems with FileSet in Ant script? - ant

I am using Ant script to generate javadoc and I just only wnt Ant to look for some classes based on a certain pattern, so I wrote:
<javadoc access="public" source="1.6" sourcepath="src" destdir="dest" >
<fileset dir="src" casesensitive="yes" defaultexcludes="yes">
<filename name="**/ABC*.java"/>
</fileset>
</javadoc>
That means I only want Ant to look for source file that starts with "ABC" only and generate javadoc for these files. However, the results are awayls duplicate for each file starting with "ABC".
Did I do something wrong?
Thanks

The problem comes in from using both the sourcepath attribute and the nested fileset tag. If you scrap the sourcepath and just have the fileset, you ought to be fine. i.e., instead of
<javadoc access="public" source="1.6" sourcepath="src" destdir="dest" >
<fileset dir="src" casesensitive="yes" defaultexcludes="yes">
<filename name="**/ABC*.java"/>
</fileset>
</javadoc>
just do:
<javadoc access="public" source="1.6" destdir="dest" >
<fileset dir="src" casesensitive="yes" defaultexcludes="yes">
<filename name="**/ABC*.java"/>
</fileset>
</javadoc>

You cannot use complex file-patterns in the javadoc task.
The javadoc for the Ant Javadoc class mentions this as a limitation:
==Begin Quote===
Current known limitations are:
patterns must be of the form "xxx.*", every other pattern doesn't work.
...
==End Quote===

Can you try with a nested include inside fileset, instead of filename like
<include name="**/ABC*"/>
or use the packagenames attribute within javadoc tag as
<javadoc packagenames="*.abc*"

Related

Removing absent entries from an ant path

I've got a situation where we're generating an Ant <path> which may contain some directories which don't actually exist. Unfortunately this is being fed into bnd, which blows up if anything in the path is missing.
So what I want is a way to filter a <path> to keep only those path elements which actually exist.
Is there a simple way to say this in Ant, or do I have to write a task?
I believe I have found an answer:
<path id="bnd.cp.existing">
<restrict>
<path refid="bnd.cp"/>
<exists/>
</restrict>
</path>
<!-- To see when it happens, add the following: -->
<echo message="bnd classpath is: ${toString:bnd.cp.existing}"/>
<iff>
<not>
<equals arg1="${toString:bnd.cp.existing}"
arg2="${toString:bnd.cp}"/>
</not>
<then>
<echo message=" trimmed from: ${toString:bnd.cp}"/>
</then>
</iff>
The restrict operation can take a path-like structure as input and return a version of it with the requested filtering applied -- in this case keep only the path elements which actually exist. This is then re-bound to a new ID for use by the <bnd> operation.

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.

Phing alternative for fixlastline from ant

I'm rewriting build.xml file from Ant to Phing and everything goes fine with one exception.
I need to add new line at the end of each appended file but I can't find any alternative for fixlastline="true".
In Ant it was
<concat destfile="${libraryFilePrefix}.js" fixlastline="yes">
<!-- many filesets -->
</concat>
In Phing it's like
<append destfile="${libraryFilePrefix}.js">
<!-- many filesets -->
</append>
Is there any attribute that works like fixlastline or maybe I need to find another way to achieve this?
I believe, one of the approaches (and possibly the only one) is applying replaceregexp filter on each fileset. You only need to apply filterchain at the beginning and it will do the job for each fileset, like this:
<append destfile="${libraryFilePrefix}.js">
<filterchain>
<replaceregexp>
<regexp pattern="([^\n])$" replace="$1${line.separator}" ignoreCase="true"/>
</replaceregexp>
</filterchain>
<!-- many filesets -->
</append>
As of Phing 3.x the AppendTask is aware of the fixlastline attribute. Your Ant script provided is now working as expected
<project name="concat-supports-fixlastline" default="concat-fixed-lastline" basedir=".">
<target name="concat-fixed-lastline">
<concat destfile="${libraryFilePrefix}.js" fixlastline="yes">
<!-- many filesets -->
</concat>
</target>
</project>

How to identify if a file is copied from a particular archive?

I am new to ANT.
I have a very specific scenario to handle in this:
STEP-1: I need to look for the pattern of filenames in certain ear files. If the pattern matches then I need to extract those files.
STEP-2: And if any file is extracted from a certain ear (similar to zip-file) file, then I need to search for another set of files, and copy those set of files too.
The case to handle is "How to identify if a file is copied from a particular archive" if found then proceed to step 2, else move to next archive.
I have achieved STEP-1 but no idea how to achieve step-2.
STEP-1
<!-- Set via arguments passed -->
<patternset id="pattern.needtocopy" includes="${needtocopyfile.pattern}" excludes="${ignore.pattern}">
</patternset>
<target name="get-binaries-from-baseline">
<for param="binary">
<path>
<fileset dir="${baseline.dir}/target/aaa/bbb/ccc" includes="*.ear" />
</path>
<sequential>
<basename file="#{binary}" property="#{binary}.basename" />
<unzip src="#{binary}" dest="${baseline.dir}">
<patternset refid="pattern.needtocopy" />
<mapper type="flatten" />
</unzip>
</sequential>
</for>
</target>
STEP-2:
????
Need help in this.
Thanks.
Well I resolved the same, using a groovy script based on the resources I could find.
<target name="findJars">
<zipfileset id="found" src="${ear-name}">
<patternset refid="${patternsetref}" />
</zipfileset>
<groovy>
project.references.found.each {
println it.name
println project.properties.'ear-name'
println project.properties.'dest.dir'
}
</groovy>
</target>
And then I added another task which takes this filename and ear-file-name as input and extracts the related jars based on file to search pattern.

[ANT]Property and Classpathref inherit broken in "foreach"

To explain it briefly, here is an example:
In build.xml, local-db.xml is imported. And in local-db.xml, there is a target named "warmup" which calls one target of the third file -- local-gr.xml using task.
All the common properties and classpath defs are imported and set in build.xml.
in project.properties:
dest-dir=./destination
in build.xml:
<path id="gr-classpath"> ... </path>
<import file="local-db.xml" />
in local-db.xml:
<ant antfile="local-gr.xml" target="deploy" inheritAll="true" inheritRefs="true" />
In local-gr.xml, there are two targets like this:
<target name="deploy">
<tar ....../>
<foreach list="${list1}" delimiter="," parallel="true" trim="true" param="item" target="deploy-single" />
</target>
<target name="deploy-single">
something using property ${dest-dir} and path "gr-classpath"
</target>
Now here is the problem:
The property ${dest-dir} and path "gr-classpath" can be used in "deploy" because I set inheritAll and inheritRefs, but it can't be used directly in "deploy-single". "inherit" doesn't when the target is called by foreach?
I managed to pass ${dest-dir} to "deploy-single" with the help of the , but I didn't find any way to pass the classpathref "gr-classpath" to "deploy-single".
What I did to work around it was to claim the again in "deploy-single", but I don't like it at all.
Why this happens? What can I do to make it more elegant?
The ant-contrib foreach task doesn't by default propagate all properties and references to it's target. But it does have inheritall and inheritrefs attributes that you can use to make that happen.
<foreach list="${list1}" delimiter="," parallel="true" trim="true"
param="item" target="deploy-single"
inheritall="true" inheritrefs="true"
/>

Resources