Apache Ant create new fileset from existing fileset with filenames renamed - ant

I have the following fileset in my Apache Ant build file (BuildFilesetXML.xml):
<fileset id="XMLFileset" dir="mydir/myxml">
<include name="**/1.xml"/>
<include name="**/2.xml"/>
</fileset>
I want to create dynamically in a different build file (BuildFilesetLog.xml) (do not know the contents of "XMLFileset" in my 2nd build file) a new fileset named "LOGFileset" that will have the same contents of "XMLFileset" but with names renamed to .log. So, in runtime it will have the same structure as the following fileset:
<fileset id="LOGFileset">
<include name="**/1.log"/>
<include name="**/2.log"/>
</fileset>
Can this be done in Ant?
Thanks

No, but some tasks support mappers. For an example see the following answer:
Change directory of FileList when referencing it

Related

Ant using Fileset in jar task and renaming files

I have the following piece of code:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
<include name="META-INF/persistence-prod.xml"/>
</fileset>
[...]
</jar>
The problem is that persistence-prod.xm1 should be persistence.xml when placed in the jar.
I know I could create a working directory and layout my whole jar there, and then jar that up. I know I can copy that one file elsewhere and rename it while copying. If I had a whole bunch of files named *-prod.xml to be renamed *.xml, I can use a file mapper inside the copy task. However, I want to be able to rename the file right in the <jar> task. I tried adding <globmapper> to the jar task, but I got the error message: jar doesn't support the nested "globmapper" element.
Any idea how this rename can take place while jaring the file?
Of course, the minute I asked the question, I figure out the answer:
I can't put <globmapper> directly into a <jar> task, but I can include <mappedresources> into the <jar> task, and place my <globmapper> in there:
Wrong:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
<include name="META-INF/persistence-prod.xml"/>
</fileset>
<globmapper from="*-prod.xml" to="*.xml"/>
[...]
</jar>
Right:
<jar destfile="${jar.file}">
<fileset dir="${basedir}/resources"/>
<include name="META-INF/ejb-jar.xml"/>
</fileset>
<mappedresources>
<fileset dir="${basedir}/resources">
<include name="META-INF/persistence-prod.xml"/>
</fileset>
<globmapper from="*-prod.xml" to="*.xml"/>
</mappedresources>
[...]
</jar>
I guess this makes sense since it limits my file mapping to just the <mappedresources> and not to all <fileset> of the <jar> task.

adding the specified folders in the jar created using ant

I have a folder structures
build/classes/com/vaannila/domain/.class files
build/classes/com/vaannila/service/.class files
build/classes/com/vaannila/web/.class files
My requirement is to create a jar file using ant, how can I include the specified folders in the jar,say I want include only domain and service (or) domain and web in the jar, like the way I create the jar using Eclipse(Export-->archive file).
Can anyone help me with this please?
You can use include and exclude tags in the fileset element, and choose specific files...
<jar destfile="${jar.dir}/${jar.file.name}" index="true"
manifest="${manif.dir}/${manif.file.name}">
<fileset dir="${build.classes.dir}" >
<include/>
<exclude/>
</fileset>
</jar>
<fileset dir="${build.classes.dir}">
<exclude name="**/property/*"/>
<include name="**/abcd/*"/>
</fileset>
This way you can remove/include respective directories.

ant target : copying directories and the respective files that are in it

Suppose I have a directory structure like the following
/a/b/testB.xml
/a/c/testC.xml
/a/testD.xml
and I want to copy everything inside /a to /build
so that I will have
/build/b/testB.xml
/build/c/testC.xml
/build/testD.xml
What ant command should I use? I have tried using fileset and it looks like that it only copies the files that are specified in the includes to the todir directory.
Try this:
<copy todir="build">
<fileset dir="a">
<include name="**/*"/>
</fileset>
</copy>
The docs on FileSet are here.

A zip file cannot include itself - Ant build error

I have error at the first line of the following code while building with Ant builder,
<war warfile="${wartemp.dir}/${name}.war" basedir="${wartemp.dir}" webxml="${wartemp.dir}/WEB-INF/web.xml">
<include name="*"/>
<include name="scripts/**"/>
<include name="styles/**"/>
<include name="images/**"/>
<include name="WEB-INF/*.*"/>
<include name="WEB-INF/lib/**"/>
<include name="WEB-INF/views/**"/>
<include name="WEB-INF/classes/**"/>
<include name="WEB-INF/jsp/**"/>
<include name="WEB-INF/resources/**"/>
<include name="WEB-INF/spring/**"/>
<include name="WEB-INF/messages/**"/>
<include name="WEB-INF/layouts/**"/>
<exclude name="WEB-INF/web.xml"/>
<exclude name="**/.*"/>
</war>
The error message is:
"... /WEB-INF/build.xml:67: A zip file cannot include itself"
line 67 is the first line of the snippet posted above.
I am beginner to Spring framework. I am using Spring version 3 with springsource toolsuite. How to fix this?
thanks.
Your basedir is the same dir as where you are sending the outputted war file. This is not a problem in itself, the problem is you are including * as input which will include the output file.
To fix this you could either exclude the output file from the included files, e.g.:
<exclude name="${name}.war"/>
or you could write the war file to a different directory structure than you are reading from, e.g.:
<mkdir dir="${war.output.dir}" />
<war warfile="${war.output.dir}/${name}.war" ...>
I guess I found another cause of the "A zip file cannot include itself" problems in any "zip-alike" Ant tasks (zip, jar ...):
Remember, setting the "basedir" attribute is already the first set of files to include! You need to explicitly exclude the zip file being created at this level (with "excludes" attribute. Or, starting with Ant 1.7, with nested "excludes" element).
The "fileset" nested element is another "set" for the zip task. You should ensure that the zip "itself" will be excluded from the set also with another explicit exclude. And so on...

How to create directories specified by a mapper in Ant

Given a fileset
<fileset id="myFiles" dir=".">
<include name="**/*.file"/>
</fileset>
How do I create a sub-directory at each file in the set, named after the filename without the extension?
For example, given the files folderA/X.file and folderA/folderB/Y.file, I want to create the directories folderA/X and folderA/folderB/Y
The ant touch task supports creating files, parent dirs and filename mapping, so can be used to achieve this:
<target name="mkdirs">
<touch mkdirs="true">
<fileset dir="the_dir"/>
<mapper type="glob" from="*.file" to="the_dir/*/.tmp" />
</touch>
<delete>
<fileset dir="the_dir" includes="**/.tmp"/>
</delete>
</target>
This creates temporary files in target dirs (creating the dirs if the don't exist) then deletes the temporary files leaving the dirs you wanted.
You would be using for task to iterate on your file list. But I have not come across any substring type of utility in Ant which you can use to strip the extension and create the directory. Do search for this utility, if its not there then you need to implement an Ant task to do that.
Sorry to answer my own question. Unless someone knows otherwise, there appears to be no way for out-of-the-box ANT to create directories (e.g. using mkdir) relative to entries in a fileset.
Ant-Contrib contains useful for loop tasks, as Bhushan suggests, which could possibly perform this sort of task.
Had some better things to be getting on with, so in the end, I just wrote a batch file called by an ANT task (apply tasks can iterate over filesets).
<apply executable="cmd" failonerror="1">
<arg value="/c"/>
<arg line="build\tools\makeRelDir.bat"/>
<fileset dir=".">
<include name="**/*.file"/>
</fileset>
</apply>
where the batch file does this:
mkdir %~dp1%~n1
(Why is it so hard to do something some simple in ANT? Am I missing something?)

Resources