Ant fileset matching only files but excluding all sub directories - ant

How do I copy only files immediately under the directory and not its subdirectories? I don't know a priori the names of the files or the subdirectories. I've tried the following to no avail:
<include name="*"> # includes all files and subdirs
===
<include name="*">
<exclude name="*/**> # or "*/" or "**"
Any help would be appreciated.
UPDATE:
I ended up just adding a delete task to delete the copied subdirectories:
<delete includeemptydirs="true">
<fileset dir="${targetdir}">
<type type="dir"/>
</fileset>
</delete>

<?xml version="1.0" encoding="UTF-8"?>
<project default="copy-top-most-files-only" name="My Project">
<property name="to.dir" value="/path/to/dest/folder/" />
<property name="from.dir" value="/path/to/source/folder/" />
<target name="copy-top-most-files-only">
<copy todir="${to.dir}" includeEmptyDirs="false" >
<fileset dir="${from.dir}">
<exclude name="*/**/*" />
</fileset>
</copy>
</target>
</project>

One way to do this is to specify a <depth> selector, for example:
<copy todir="dest">
<fileset dir="src">
<depth max="0" />
</fileset>
</copy>
That will prevent subdirectories from being copied.
You can combine selectors with include/exclude rules if needed.

Related

how to excludes and include task in ant script

I want to copy only specific jar file using ant script. It is very easy task but i am not able to do this. I have written the below ant script for this task. I want to excludes all jar file except aopalliance-.jar file. But when i run this script it excludes all the jar file. can someone correct this script .
<?xml version="1.0" encoding="UTF-8"?>
<project default="mvcexample" name="MVCExample" basedir=".">
<property name="web.dir" value="WebContent" />
<property name="webinf.dir" value="WebContent/WEB-INF" />
<property name="lib.dir" value="WebContent/WEB-INF/lib" />
<property name="lib2.dir" value="WebContent/WEB-INF/lib2" />
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}" excludes="**/*.jar">
<include name="${lib.dir}/aopalliance-.jar"></include>
</fileset>
</copy>
</target>
</project>
Here's what you need to do:
<target name="copyjar">
<copy todir="${lib2.dir}">
<fileset dir="${lib.dir}">
<!-- <include name="${lib.dir}/aopalliance-.jar"/> -->
<include name="aopalliance-.jar"/> <!-- Don't put the dir name! -->
</fileset>
</copy>
</target>
When you specify a directory, you merely specify the name pattern under that directory. What you were asking for was the jar ${lib.dir}/${lib.dir}/aopalliance-.jar. By the way, is there suppose to be a version number in there somewhere? Like aopalliance-3.4.jar? If so, you need <include name="aopalliance-*.jar"/>.
Also notice that if I used <include .../> instead of <include...></include>.

Generating MD5 for files in an ant-contrib for loop in ANT

I am selecting set of files using file set and then using them to generate the checksum of all the files in the selected fileset
here is my script
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="MyTask1" basedir="." default="jar">
<property name="cms.dir" value="D:\Test" />
<property name="comma" value="," />
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<target name="A">
<fileset id="src.files" dir="${cms.dir}" casesensitive="yes">
<include name="**/*.uim"/>
<include name="**/*.properties"/>
<include name="**/*.txt"/>
</fileset>
<pathconvert pathsep="${line.separator}" property="sounds" refid="src.files">
<!-- To get the names of the files only then use mapper-->
<!-- <mapper type="flatten" />-->
</pathconvert>
<delete file="sounds.txt"/>
<for list="${sounds}" delimiter="${line.separator}" param="mod">
<sequential>
<checksum file="#{mod}" property="MD5_Value"/>
<echo file="sounds.txt" append="true">#{mod}${comma}${MD5_Value}${line.separator}</echo>
</sequential>
</for>
<!--<checksum file="Test.txt" property="foobarMD5"/>-->
<!--<echo file="sounds.txt">${foobarMD5}</echo>-->
</target>
</project>
However its failing and its generating duplicate MD5 value here is my output
D:\Test\Test1.txt,6d326741a99efbcda928e5096b43cb9a
D:\Test\Test2.txt,6d326741a99efbcda928e5096b43cb9a
Any help ...
The checksum task can process filesets...
<checksum>
<fileset dir=".">
<include name="foo*"/>
</fileset>
</checksum>
Lot simpler than using the for task, which is not part of standard ANT.

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>

Build war with filtered/modified web.xml

At build time, I am updating some variables that are stored in the web.xml file using variables in a properties file, Is there a way to simplify what I am doing here:
<target name="war-test" depends="compile">
<mkdir dir="${dist}"/>
<mkdir dir="${dist}/tmp"/>
<copy file="WebContent/WEB-INF/web.xml.templ" tofile="${dist}/tmp/web.xml">
<filterchain>
<replacetokens>
<token key="smtp.hostname" value="${test.smtp.hostname}"/>
<token key="smtp.port" value="${test.smtp.port}"/>
</replacetokens>
</filterchain>
</copy>
<war destfile="${dist}/mywarfile-test.war" webxml="${dist}/tmp/web.xml">
<fileset dir="WebContent">
<exclude name="META-INF/**"/>
<exclude name="META-INF"/>
<exclude name="WEB-INF/**"/>
<exclude name="WEB-INF"/>
</fileset>
<lib dir="lib">
<exclude name="somelibrary.jar"/>
</lib>
<classes dir="${build}"/>
</war>
<delete dir="${dist}/tmp"/>
<antcall target="clean"/>
</target>
Do I need to make the tmp directory?
You need to put the web.xml in a temporary location if you do not want to copy it directly to WebContent/WEB-INF and remove the exclude for that folder.
There is no subelement for <war> that lets you create it on the fly, as there is for the manifest.
As oers says there seems to be something strange, typo or similar.
You create "${dist}/metainf/web.xml but include "${dist}/tmp/web.xml.
If you want less lines you can replace the filter chain with:
<filterset>
<filter token="smtp.hostname" value="${test.smtp.hostname}" />
<filter token="smtp.port" value="${test.smtp.port}" />
</filterset>

Ant: simplest way to copy over a relative list of files

Using Ant, I want to copy a list of files from one project to another, where each project has the same directory structure. Is there a way to get the following to work?
<project name="WordSlug" default="pull" basedir=".">
<description>
WordSlug: pull needed files
</description>
<property name="prontiso_home" location="../../prontiso/trunk"/>
<!-- I know this doesn't work, what's the missing piece? -->
<target name="pull" description="Pull needed files">
<copy todir="." overwrite="true">
<resources>
<file file="${prontiso_home}/application/views/scripts/error/error.phtml"/>
<file file="${prontiso_home}/application/controllers/CacheController.php"/>
<!-- etc. -->
</resources>
</copy>
</target>
</project>
Success is deriving the paths automatically:
${prontiso_home}/application/views/scripts/error/error.phtml copied to ./application/views/scripts/error/error.phtml
${prontiso_home}/application/controllers/CacheController.php copied to ./application/controllers/CacheController.php
Thanks!
Try creating a fileset:
<copy todir="." overwrite="true">
<fileset dir="${prontiso_home}/application/">
<include name="views/scripts/error/error.phtml,controllers/CacheController.php"/>
</fileset>
</copy>
Typically, however, you use fileset to define patterns of files to copy, such as "copy all PHP files in my application directory:
<fileset dir="${prontiso_home}/application/">
<include name="**/*.phtml,**/*.php"/>
</fileset>
OP here. I have a working solution:
<project name="WordSlug" default="pull" basedir=".">
<description>
WordSlug: pull needed files
</description>
<property name="prontiso_home" location="../../prontiso/trunk"/>
<target name="pull" description="Pull needed files">
<copy todir="." overwrite="true" verbose="true">
<fileset dir="${prontiso_home}">
<or>
<filename name="/application/views/scripts/error/error.phtml"/>
<filename name="/application/controllers/CacheController.php"/>
</or>
</fileset>
</copy>
</target>
</project>

Resources