Copy files and folder to WEB-INF using Ant - ant

I want to copy a .properties file from a certain location to my WEB-INF/classes/com/infiniti folder(in a WAR file).
I have gone through this link How to get Ant to copy properties file to classes directory
using which I can copy the .properties file to WEB-INF/classes/ but not to WEB-INF/classes/com/infiniti
Code I am using is:
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
<lib dir="${lib}">
.......
.......
.......
<classes dir="${configHome}/config/com/infiniti">
<include name="accredit.properties" />
</classes>
...
....
.......
</war>
Also I need to copy ${configHome}/resources/com/infiniti/errorcode folder to
WEB-INF/classes/com/infiniti.
Is this possible using Ant?

yes, you can use for instance ZipFileSet like this
<war destfile="${deploy}/acc.war" webxml="${warSrc}/web/WEB-INF/web.xml">
...
<zipfileset dir="${configHome}/config/com/infiniti" includes="**/*.properties" prefix="WEB-INF/classes/com/infiniti"/>

Yes, it's possible using ant. Just use the copy or sync commands to move your file:
<copy todir="${distribution}/location" file="${local.path}/data/file.txt">
</copy>
You can also copy with rules:
<copy includeemptydirs="false" todir="${combined.bin}">
<fileset dir="${buildbin}"/>
<fileset dir="${output2buildbin}"/>
<fileset dir="${output3buildbin}"/>
</copy>
Using sync:
<sync includeemptydirs="false" todir="${distres}">
<fileset dir="${buildres}">
<include name="logging/**" />
</fileset>
</sync>
Tasks are described on their doc site:
http://ant.apache.org/manual/Tasks/copy.html
The same 'fileset' declarative applies to the war task:
Examples
Assume the following structure in the project's base directory:
thirdparty/libs/jdbc1.jar
thirdparty/libs/jdbc2.jar
build/main/com/myco/myapp/Servlet.class
src/metadata/myapp.xml
src/html/myapp/index.html
src/jsp/myapp/front.jsp
src/graphics/images/gifs/small/logo.gif
src/graphics/images/gifs/large/logo.gif
then the war file myapp.war created with
<war destfile="myapp.war" webxml="src/metadata/myapp.xml">
<fileset dir="src/html/myapp"/>
<fileset dir="src/jsp/myapp"/>
<lib dir="thirdparty/libs">
<exclude name="jdbc1.jar"/>
</lib>
<classes dir="build/main"/>
<zipfileset dir="src/graphics/images/gifs"
prefix="images"/>
</war>
will consist of
WEB-INF/web.xml
WEB-INF/lib/jdbc2.jar
WEB-INF/classes/com/myco/myapp/Servlet.class
META-INF/MANIFEST.MF
index.html
front.jsp
images/small/logo.gif
images/large/logo.gif
http://ant.apache.org/manual/Tasks/war.html

Related

How to do recursive copy of files and directories using ant task

Ant has inbuilt Copy task to copy multiple files.
I tried to define following target in build.xml file
<target name="copyFile">
<copy todir="../CHECK">
<fileset dir=".">
<patternset id="AllFiles">
<include name="*"/>
</patternset>
</fileset>
</copy>
</target>
It is copying files and directories. However content within directories is not copied, instead directories are copied as empty to destination "../CHECK". Does Ant copy task provides capability to do recursive copy of files and directories
I found the answer
name pattern in include should be "**" instead of "*". It does recursive copy of all contents
<target name="copyFile">
<copy todir="../CHECK">
<fileset dir=".">
<patternset id="AllFiles">
<include name="**"/>
</patternset>
</fileset>
</copy>
</target>

ant build.xml filename without file (base name of the file) only .(dot) and extension

I have in ant build.xml directive
<target name="war" depends="compile">
<war destfile="dist/app.war" webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent">
<include name="**/*.*"/>
<include name="._arrow.png"/>
</fileset>
<lib dir="WebContent/WEB-INF/lib"/>
<classes dir="build/classes"/>
</war>
</target>
in the war file I got only :
favicon.ico
robots.txt
sitemap.xml
I am unable to add the files without base name of the file and who has only .extention like
._arrow.png
Can someone help me, please?
Thanks Raul

Copying the most recent files with Ant

So I have a bunch of Jars in a directory that look like this:
library_2.4.3.jar 2/3/2012
library_3.0.1.jar 9/1/2012
api.lib_10.3.jar 3/2/2011
api.lib_12.4.5.jar 6/9/2012
I have already written the following using Ant 1.7 to copy the jars to where I want them and strip away the version number from the file
<copy todir="${lib.dir}" overwrite="true">
<fileset dir="${plugins.dir}">
<include name="library*.jar" />
<include name="api.lib*.jar" />
</fileset>
<regexpmapper from="(.*)_(.*).jar" to="\1.jar"/>
</copy>
The problem I'm running into is that I want it to copy the newer version of the file. Right now it seems to be copying only the older files. I have looked into the <sort> and <TimestampSelector> tasks but those are not supported under the copy task.
What can I do to copy the newer versions of the file?
Do not put them under the copy task directly... Create the property and use the property in the copy tag...
<timestampselector property="latest.modified">
<path>
<fileset dir="${my-directory.dir}">
<include name="file-*" />
</fileset>
</path>
</timestampselector>
<copy todir="." file="${latest.modified}">
Hope, it works.

how to specify set of files to be copied in lib while creating war

This is my build target
<target name="build-war" depends="build-java">
<war destfile="${dist.dir}/${std.war.file}" webxml="${resources.dir}/WEB-INF/web.xml">
<fileset dir="${jsp.dir}" />
<lib dir="${lib.dir}"/>
<classes dir="${build.classes.dir}" />
</war>
</target>
Here for <lib> can i specify some how a fileset / pattern set ?
Basically i want to copy different
jar from different places. (Not in
one directlry)
And That fileset or patternset i want to be defined in another build file which actually imports this build file.
<lib refid="${patternset.id}"/>
You can simply define several <lib> elements:
<war destfile="myjar.jar">
<lib dir="${dir1}" includes="*.jar"/>
<lib dir="${dir2}" includes="*.jar"/>
</war>

how can I reference a classpathref in a war task?

I declare the following classpath reference for my application librairies:
<path id="libraries">
<fileset dir="${lib.dir}" includes="**/*.jar" />
</path >
I can compile the code using the classpath of the librairies:
<javac srcdir="${src}" destdir="${build.classes}" classpathref="libraries"/>
But I cannot find a way to include the fileset of librairies also in my WAR file:
<war destfile="${release.dir}/rel.war" webxml="${webinf}">
<classes dir="${build.classes}"/>
<!-- I need to copy paste the same directory declaration! -->
<lib dir="${lib.dir}" includes="**/*.jar"/>
</war>
How can I replace the "lib" declaration with something that reuse the same path as in my javac task?
Declare the fileset out of the path and assign it an identifier:
<fileset id="xxx" dir="..." includes="..." />
Then reference the identifier in both declarations (lib specifies a fileset, so you don't have to use nesting):
<path id="libraries">
<fileset refid="xxx"/>
</path>
...
<lib refid="xxx"/>

Resources