Ant exlude jar from path - ant

I have a path that looks like this
<path id="classpath-ant">
<fileset dir="${lib-ant-dir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${template-dir}/hibernate" />
<path refid="test-classpath" />
<path refid="ivy-ant-classpath" />
</path>
My test-classpath includes bunch of jars. I want to exclude one jar (hibernate3.5.2.jar) from that path. How can i do this.
I don't want to change the test-classpath and put an exclude there as it is used by other targets. I want to modify classpath-ant such that it does not include one jar from path refid test-classpath

You could rename your test-classpath to classpath-common and then add the exclude there.
Then, you could simply create a new path and include **/hibernate3.5.2.jar as follows:
<path id="test-classpath">
<path refid="classpath-common" />
<fileset dir="${hibernate.dir}">
<include name="**/hibernate3.5.2.jar" />
</fileset>
</path>
<path id="classpath-ant">
<fileset dir="${lib-ant-dir}">
<include name="**/*.jar" />
</fileset>
<pathelement location="${template-dir}/hibernate" />
<path refid="classpath-base" />
<path refid="ivy-ant-classpath" />
</path>

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.

Create specific WAR file

In a ANT script, is there a way to include a file from 'environnement' directory into the different war ?
My filesystem tree :
environnementDEVweb.xmllog4j.propertiesINTweb.xmllog4j.propertiesWebContentWEB-INFweb.xmllog4j.properties
Extract from build.xml :
<target name="createForDEV">
<delete file="environnement/DEV/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/DEV/${timeStampDay}/${warfile}.war" webxml="environnement/DEV/web.xml" update="true">
<classes dir="build/classes" />
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
</fileset>
</war>
</target>
<target name="createForINT">
<delete file="environnement/INT/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/INT/${timeStampDay}/${warfile}.war" webxml="environnement/INT/web.xml" update="true">
<classes dir="build/classes" />
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
<exclude name="**/test.jsp" />
</fileset>
</war>
</target>
I have two configuration files :
for DEV environment
for INT environment
When I make the WAR file, I would like to ignore some files and replace them by others specific files from 'environnement' directory ?
When making WAR in createForDEV target, I would like to take file from environnement/DEV and replace corresponding files
When making WAR in createForINT target, I would like to take file from environnement/INT and replace corresponding files
The trick here is to utilize the duplicate attribute of the war task, and to include multiple fileset elements. The value preserve for duplicate tells it to ignore duplicate entries. The files in the first fileset (from either DEV or INT) will be placed in the war first. Any additional files in WebContent will be included by the second fileset, but any files already included from DEV or INT will be ignored.
<target name="createForDEV">
<delete file="environnement/DEV/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/DEV/${timeStampDay}/${warfile}.war" webxml="environnement/DEV/web.xml" update="true" duplicate="preserve">
<classes dir="build/classes" />
<fileset dir="environnement/DEV">
<exclude name="web.xml" />
</fileset>
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
</fileset>
</war>
</target>
<target name="createForINT">
<delete file="environnement/INT/${timeStampDay}/${warfile}.war" />
<war destfile="environnement/INT/${timeStampDay}/${warfile}.war" webxml="environnement/INT/web.xml" update="true" duplicate="preserve">
<classes dir="build/classes" />
<fileset dir="environnement/INT">
<exclude name="web.xml" />
</fileset>
<fileset dir="WebContent">
<exclude name="WEB-INF/web.xml" />
<exclude name="**/Thumbs.db" />
<exclude name="**/test.jsp" />
</fileset>
</war>
</target>

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>

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>

Generate manifest class-path from <classpath> in Ant

In the build file below, the jar target refers to the jar.class.path property for the manifest class-path. The compile target refers to project.class.path
There is redundancy here, because jar.class.path and project.class.path are very similar. They must be both updated when libraries are added, which can be a pain if the list of libraries gets very long. Is there a better way? Any solution must be cross-platform and always use relative paths.
Edit:
It should generate the JAR classpath from a fileset and not the other way around, so I can use wildcards to e.g. include all JAR files in a directory.
<?xml version="1.0"?>
<project name="Higgins" default="jar" basedir=".">
<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>
<path id="project.class.path">
<pathelement location="build"/>
<fileset dir="lib">
<include name="forms-1.2.0.jar"/>
<include name="BrowserLauncher.jar"/>
</fileset>
</path>
<target name="prepare">
<mkdir dir="build"/>
</target>
<target name="compile" depends="prepare" description="Compile core sources">
<javac srcdir="src"
includes="**"
destdir="build"
debug="true"
source="1.5">
<classpath refid="project.class.path"/>
</javac>
</target>
<target name="jar" depends="compile" description="Generates executable jar file">
<jar jarfile="higgins.jar">
<manifest>
<attribute name="Main-Class" value="nl.helixsoft.higgins.Main"/>
<attribute name="Class-Path" value="${jar.class.path}"/>
</manifest>
<fileset dir="build" includes="**/*.class"/>
<fileset dir="src" includes="**/*.properties"/>
</jar>
</target>
</project>
<path id="build.classpath">
<fileset dir="${basedir}">
<include name="lib/*.jar"/>
</fileset>
</path>
<pathconvert property="manifest.classpath" pathsep=" ">
<path refid="build.classpath"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>
<target depends="compile" name="buildjar">
<jar jarfile="${basedir}/${test.jar}">
<fileset dir="${build}" />
<manifest>
<attribute name="Main-Class" value="com.mycompany.TestMain"/>
<attribute name="Class-Path" value="${manifest.classpath}"/>
</manifest>
</jar>
</target>
For further information check out this article.
Assuming Ant 1.7 or above, you can use the manifestclasspath task.
<path id="dep.runtime">
<fileset dir="./lib">
<include name="**/*.jar" />
</fileset>
</path>
<property name="dep_cp" value="${toString:dep.runtime}" />
<target name="default">
<manifestclasspath property="manifest_cp" jarfile="myjar.jar">
<classpath refid="dep.runtime" />
</manifestclasspath>
<echo message="Build Classpath: ${dep_cp}" />
<echo message="Manifest Classpath: ${manifest_cp}" />
</target>
If you just want a common subpath shared between two (or more) paths, that is easy to do:
<path id="lib.path>
<fileset dir="lib">
<include name="forms-1.2.0.jar"/>
<include name="BrowserLauncher.jar"/>
</fileset>
</path>
<path id="project.class.path">
<pathelement location="build"/>
<path refid="lib.path"/>
</path>
<property name="jar.class.path" refid="lib.path"/>
EDIT Sorry, I misunderstood the question. Try this:
<property name="jar.class.path" value="lib/forms-1.2.0.jar lib/BrowserLauncher.jar"/>
<path id="project.class.path">
<pathelement location="build"/>
<fileset dir="." includes="${jar.class.path}"/>
</path>
You can use <pathconvert> to convert a path (which can contain a fileset) into a plain string. You'll likely need to <echo> that string to a file, use either <replace> or <replaceregexp> to chop the leading path bits, then finally use <loadfile> to load the manipulated string into the final property.
Implementation left as an exercise to the reader.

Resources