Ant - Using Resource Unions with Macrodef - ant

I have an Ant Macrodef that has an <element> placeholder. I am attempting to use the <element> to pass a resourceCollection for processing. The contents of the <element> is populated correctly prior to invoking the macrodef. Unfortunately, during invocation, it comes in as empty.
Macrodef:
<macrodef name="doStuff" description="Amazing macrodef that fails me.">
<element name="fs" optional="true" description="resource/element/fileset to be manipulated />
<sequential>
<echo message="fs: ${toString:fs}" />
<pathconvert property="outputProp" pathsep=" ">
<fs />
</pathconvert>
</sequential>
</macrodef>
and it is invoked via the following pieces of a build.xml file I have:
<fileset id="files1" dir=".">
<include name="inc/*" />
<include name="lib/*" />
</fileset>
<fileset id="files2" dir=".">
<include name="bin/*" />
</fileset>
<union id="allFiles">
<resources refid="files1" />
<resources refid="files2" />
</union>
<target name="doStuffToFiles">
<doStuff>
<fs>
<resources refid="allFiles" />
</fs>
</doStuff>
</target>

I'm unsure why, but going through the union and resources, instead of using a direct fileset with a refid, seemed to be confusing my Ant setup. The following has worked for me on Ant 1.9.2
<macrodef name="doStuff" description="Amazing macrodef that fails me.">
<element name="fs"
optional="false"
description="resource/element/fileset to be manipulated />
<sequential>
<pathconvert property="outputProp" pathsep=" ">
<fs />
<map from="${basedir}/" to=""/>
</pathconvert>
</sequential>
</macrodef>
<target name="doStuffToFiles">
<fileset id="files1" dir=".">
<include name="inc/**"/>
<include name="lib/**"/>
</fileset>
<fileset id="files2" dir=".">
<include name="bin/**"/>
</fileset>
<doStuff>
<fs>
<fileset refid="files1" />
<fileset refid="files2" />
</fs>
</doStuff>
</target>

Related

how to use<MacroDef> for recourcecount of all file in folder with different extension

While going through the scenario what i got is that folder collection of different extension file i have use resource count for all extension if i have 3 different extension file than try to get resource count of all file differ with extension
Eg:
<resourcecount property="firstfile">
<fileset dir="${basedir}">
<include name="*.xml" />
</fileset>
</resourcecount>
<echo message="There are ${firstfile} xml in This Folder ${basedir}" />
<resourcecount property="SecondFile">
<fileset dir="${basedir}">
<include name="*.jar" />
</fileset>
</resourcecount>
<echo message="There are ${SecondFile} xml in This Folder ${basedir}" />
How can i use the macrodef for this condition which help in count all file with it
You can do this in many ways, including using plugins, such as ant-contrib, or using ant language extensions. But all of them are overkill compared to this simple build script:
<target name="count">
<countresources type="xml" />
<countresources type="java" />
<countresources type="cpp" />
</target>
<macrodef name="countresources">
<attribute name="type" />
<sequential>
<resourcecount property="#{type}.count">
<fileset dir="${basedir}">
<include name="*.#{type}" />
</fileset>
</resourcecount>
<echo message="There are ${#{type}.count} #{type} files in folder ${basedir}" />
</sequential>
</macrodef>
Hope this helps.

Compiled classes are not included in generated Jar

I am creating a jar bundle using ant build script. The problem is that the .class files are not included in the generated .jar file. I have also tried the {build.dest} in making the jar, but with no effect.
remaining all the files i require are in .jar file.
Here is my build script
<?xml version="1.0"?>
<project name="TaskNodeBundle" default="all" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="bundlename" value="task-node-bundle" />
<property name="src.dir" location="../src" />
<property name="lib.dir" location="../lib" />
<property name="build.dir" location="/buildoutput" />
<property name="build.dest" location="../build/dest" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="classpath">
<fileset dir="${lib.dir}/">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${build.dest}" />
</target>
<!-- Deletes the existing build directory -->
<target name="mkdir" depends="clean">
<mkdir dir="${build.dest}"/>
</target>
<!-- Compiles the java code -->
<target name="compile" depends="mkdir">
<javac srcdir="${src.dir}" destdir="${build.dest}" classpathref="classpath" />
</target>
<target name="package-bundle" depends="compile" description="Generates the bundle" >
<jar destfile="${build.dest}/${bundlename}.jar" manifest="${src.dir}/META-INF/MANIFEST.MF">
<fileset dir="${src.dir}">
<include name="**/**.class" />
<include name="**/**.properties"/>
<include name="/META-INF/**.*" />
<include name="/META-INF/spring/**.*" />
</fileset>
</jar>
</target>
<target name="all" depends="package-bundle">
</target>
</project>
Firstly, what do you mean by "tried {build.dest} in making the jar"?
Whatever, you need to take a look at this part of your build:
<jar destfile="${build.dest}/${bundlename}.jar" manifest="${src.dir}/META-INF/MANIFEST.MF">
<fileset dir="${src.dir}">
<include name="**/**.class" />
<include name="**/**.properties"/>
<include name="/META-INF/**.*" />
<include name="/META-INF/spring/**.*" />
</fileset>
</jar>
You compiled class files are in ${build.dest}, so you should use ${build.dest} as the root dir for the nested <fileset> of the <jar> task. But now you are pointing the <fileset> to your source code folder.
You should avoid putting the generated jar file in the same directory where the class files are. For example, you can put the jar in ${dist.dir}, which is another directory.
So try this:
You have a property:
<property name="dist.dir" value="../build/dist" />
And then,
<jar destfile="${dist.dir}/${bundlename}.jar" manifest="${src.dir}/META-INF/MANIFEST.MF">
<fileset dir="${build.dest}">
<include name="**/*.class" />
</fileset>
<fileset dir="${src.dir}">
<include name="**/*.properties"/>
<include name="/META-INF/**/*.*" />
<include name="/META-INF/spring/**/*.*" />
</fileset>
</jar>

MANIFEST.MF is override after running build.xml using ant

have following directory structure
src/com
src/META-INF/MANIFEST.MF
src/META-INF/spring
src/META-INF/spring/context.xml
now when i run the script, my menifest file is override, i don't want that, because i have to add custom enteries in it and i want that to be adding in generated .jar file. THing is all other files are copied, but this one is override.
my build.xml is as follows
<project name="TaskNodeBundle" default="all" basedir=".">
<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="bundlename" value="tasknodebundle" />
<property name="src.dir" location="../src" />
<property name="lib.dir" location="../lib" />
<property name="build.dir" location="/buildoutput" />
<property name="build.dest" location="../build/dest" />
<!--
Create a classpath container which can be later used in the ant task
-->
<path id="classpath">
<fileset dir="${lib.dir}/">
<include name="*.jar" />
</fileset>
</path>
<target name="clean">
<delete dir="${build.dir}" />
<delete dir="${build.dest}" />
</target>
<!-- Deletes the existing build directory-->
<target name="mkdir" depends="clean">
<mkdir dir="${build.dest}"/>
</target>
<!-- Compiles the java code -->
<target name="compile" depends="mkdir">
<javac srcdir="${src.dir}" destdir="${build.dest}" classpathref="classpath" />
</target>
<target name="package-bundle" depends="compile" description="Generates the bundle">
<jar destfile="${build.dest}/${bundlename}.jar">
<fileset dir="${src.dir}">
<include name="**/**.class" />
<include name="**/**.properties"/>
<include name="/META-INF/**.*" />
<include name="/META-INF/spring/**.*" />
</fileset>
</jar>
</target>
<target name="all" depends="package-bundle">
</target>
</project>
See http://ant.apache.org/manual/Tasks/jar.html.
If the manifest is omitted, a simple one will be supplied by Apache
Ant.
Just add manifest attribute or use zip task.
Also ant path masks are used incorrectly. See http://en.wikibooks.org/wiki/Apache_Ant/Fileset.
Corrected version:
<zip destfile="${build.dest}/${bundlename}.jar">
<fileset dir="${src.dir}">
<include name="META-INF/**" />
<include name="**/*.class" />
<include name="**/*.properties"/>
</fileset>
</zip>

Combine filesets using Ant

I have 2 different filesets defined in Ant as follows:
<fileset id="fileset1" dir="${classes.dir}">
</fileset>
<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class">
</zipfileset>
I want to create a third fileset which is the union of both the above filesets
<fileset id="merged">
</fileset>
Can someone tell me how to do this ? Is it even possible to do something like that ?
Thanks in advance!
One way to do this is with Ant resource collections, in particular a union.
<fileset id="fileset1" dir="${classes.dir}" />
<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class" />
<union id="onion">
<resources refid="fileset1" />
<resources refid="fileset2" />
</union>
Then you can refer to the 'onion' anywhere you might use a fileset, e.g.
<copy todir="dest">
<resources refid="onion" />
</copy>
I recommend using generic resources elements rather than filesets for maximum flexibility.
Try this: I think it should work, since <fileset> is an implicit <patternset>.
<fileset id="fileset1" dir="${classes.dir}">
</fileset>
<zipfileset id="fileset2" src="myArchive.zip" includes="**/*.class">
</zipfileset>
EDIT: odd. This perhaps?
<patternset id="merged">
<patternset refid="fileset1" />
<patternset refid="fileset2" />
</patternset>
problem with fileset is, that it requires a directory as a base upon it applies the patternset. Which means you have to find a common base directory that is shared by all filesets.
A <pathconvert> Task can take filesets via refid. You can put several filesets (e.g. from various build targets to assemble a compound set in a root/main target for a modular build environment):
<project name="root" basedir="." xmlns:if="ant:if" xmlns:unless="ant:unless">
<!--
it's important to take the xmlns:features in your project head
otherwhise this code won't work
-->
<target name="init">
<!-- set some common prerequisites -->
<property name="prerequisite.property.xyz" value="xyz" />
</target>
<target name="targetA" depends="init">
<fileset dir="${common.basedir}${file.separator}${targetA.subdir}" id="targetA.fileset">
<include name="**/*.html" />
</fileset>
<property name="targetA.fileset.exists" value="true" />
</target>
<target name="targetB" depends="init">
<fileset dir="${common.basedir}${file.separator}${targetB.subdir}" id="targetB.fileset">
<include name="**/*.java" />
</fileset>
<property name="targetB.fileset.exists" value="true" />
</target>
<target name="targetC" depends="init">
<fileset dir="${common.basedir}${file.separator}${targetC.subdir}" id="targetC.fileset">
<include name="**/*.class" />
</fileset>
<property name="targetC.fileset.exists" value="true" />
</target>
<target name="root" depends="init">
<pathconvert property="all.files.as.commaseparated.path" pathsep="," dirsep="/">
<fileset refid="targetA.fileset" if:true="${targetA.fileset.exists}" />
<fileset refid="targetB.fileset" if:true="${targetB.fileset.exists}" />
<fileset refid="targetC.fileset" if:true="${targetC.fileset.exists}" />
<map from="${common.basedir}/" to="" />
</pathconvert>
<!-- assemble new fileset from paths as comma separated property string -->
<fileset id="new.refid" dir="${common.basedir}" includes="${all.files.as.commaseparated.path}" />
</target>
</project>
This can be called via command line like:
ant targetA targetB targetC root
or
ant targetA root
Be aware that root is always the last target being called.

Ant concat fileset separator

When using concat with fileset in Ant, how do I make an action happen for each element in the fileset. Such as, adding a string:
<fileset dir="${project.path.scripts-library}"
excludes="!*.js, **/*.bak, **/dev.*"
>
<type type="file" />
<include name="**/0*.js" />
<include name="**/1*.js" />
<string>test</string>
</fileset>
Or echoing the current file name for each file in the fileset (and, how do I GET the file name for the current file???):
<fileset dir="${project.path.scripts-library}"
excludes="!*.js, **/*.bak, **/dev.*"
>
<echo file="${app.path.file}"
append="true"
message=",${the.file.name}" />
<type type="file" />
<include name="**/0*.js" />
<include name="**/1*.js" />
</fileset>
I think there is no such thing in default ant. The closest one is <apply>, but it's system specific:
<apply executable="echo"> <!-- run this command with each file name -->
<fileset dir="/tmp" includes="**/*.*"/>
</apply>
Also you can install ant-contrib to enable <for> task:
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<for param="file">
<path>
<fileset dir="/tmp" includes="**/*.*"/>
</path>
<sequential> <!-- run any task here -->
<echo>file [#{file}]</echo>
</sequential>
</for>

Resources