copy subfolders to a directory using ant - java

I want to copy a lot of subfolders and its content from various other folders to one specific folder.
I also want to use a patternset to choose the subfolders to copy.
For example there may be a folder wich contains a lots of subfolders which then could contain again subfolders. Now I want a patternset to get all folders named org or com or whatever and copy them where I like.
I tried it this way:
<target name="copysrc">
<patternset id="set">
<include name="**/org/**"/>
<include name="**/com/**"/>
<include name="**/de/**"/>
<include name="**/net/**"/>
</patternset>
<copy todir="${tmp.dir2}">
<fileset dir="${tmp.dir}" casesensitive="no">
<patternset refid="set" />
</fileset>
</copy>
</target>
Almost did the trick but I still have all the folders above the org, com,... copied with them and not just org, com,.. and the contend below these folders.
Thanks in advance, mojoo.de

Okay got it working now .. had to use the ant contrib package code looks like
<target name="copysrc">
<patternset id="zuKopieren">
<include name="org/**"/>
<include name="com/**"/>
<include name="de/**"/>
<include name="net/**"/>
</patternset>
<for param="verzeichnisName">
<dirset dir="${tmp.dir}"></dirset>
<sequential>
<copy todir="${tmp.dir2}/src">
<fileset dir="#{verzeichnisName}" casesensitive="no"><patternset refid="zuKopieren" />
</fileset>
</copy>
</sequential>
</for>
</target>

Related

ANT unzip every jar except one

I have an ANT target that unzips every jar with a "for" task, but I want to exclude a specific jar called Neo.jar. This is what I have so far, but it is unzipping every single jar that is available to it.
<target name="unzipjars">
<for param="jar">
<sequential>
<unzip dest="${expanded.dirs}" src="#{jar}">
<exclude name="Neo.jar/**"/>
</unzip>
</sequential>
</for>
</target>
I was looking at trying to exclude something from "for param="jar"" but I don't think anything like that exists. The "exclude name" for Neo.jar doesn't seem to work because I believe it doesn't think it is a directory because it is a jar
As you can see here, it's quietly easy.
All you need is to add the tag patternset to your configuration (and you don't need to iterate by the way...). Like this:
<target name="unzipjars">
<unzip dest="${expanded.dirs}">
<patternset>
<exclude name="**/Neo.jar"/>
</patternset>
<fileset dir="${jar}">
<include name="**/*.*"/>
</fileset>
</unzip>
</target>
You can use the if task (http://ant-contrib.sourceforge.net/tasks/tasks/if.html) to filter out the Neo.jar file. For example
<for param="jar">
<sequential>
<if>
<not><equals arg1="#{jar}" arg2="Neo.jar" /></not>
<then><!-- unzip the jar --></then>
</if>
</sequential>
</for>

Simultaneously copying and renaming directory in ant

I've looked through ant tasks like copy, but can't really seem to find a way. With this kind of file structure:
/Resources/Docs/SpecificDoc/ScalaDocs
I want to rename "ScalaDocs" to "TargetDocs", having an end structure like so:
/Resources/Docs/TargetDocs
Try this.
<copy todir="/Resources/Docs/TargetDocs">
<fileset dir="/Resources/Docs/SpecificDoc/ScalaDocs">
<include name="*" />
</fileset>
</copy>

Ant build - javac task - exclude a dir then include a subdir

I have a tree like this
path/a
path/b
path/c
path/d/da
path/d/db
path/d/dc
Now in my javac task i want to
Compile everything in path/
Exclude everything in path/d/
Compile everything in path/d/db/
Like this:
path/a
path/b
path/c
path/d/db
I played with include/exclude and patternset but i couldn't achieve what i need.
Is there a way to do this?
The <difference> and <union> set operations will be handy for what you need.
The following Ant script shows how to combine several <fileset> elements into one:
<project name="ant-javac-include-and-exclude" default="run" basedir=".">
<target name="run">
<fileset id="all-files" dir="path">
<include name="**"/>
</fileset>
<fileset id="files-under-d" dir="path">
<include name="d/**"/>
</fileset>
<fileset id="files-under-d-db" dir="path">
<include name="d/db/**"/>
</fileset>
<!-- Matches all files under a, b, c -->
<difference id="all-files-NOT-under-d">
<fileset refid="all-files"/>
<fileset refid="files-under-d"/>
</difference>
<!-- Combine all files under a, b, c and under d/db -->
<union id="files-to-compile">
<difference refid="all-files-NOT-under-d"/>
<fileset refid="files-under-d-db"/>
</union>
<!-- Convert the absolute paths in "files-to-compile" to relative-->
<!-- paths. Also, "includes" of <javac> requires a comma-separated -->
<!-- list of files. -->
<pathconvert property="union-path" pathsep=",">
<union refid="files-to-compile"/>
<map from="${basedir}/" to=""/>
</pathconvert>
<javac
srcdir="."
includes="${union-path}"
includeantruntime="false"
/>
</target>
</project>
The above steps can be combined into the following:
<pathconvert property="union-path" pathsep=",">
<union>
<difference>
<fileset dir="path">
<include name="**"/>
</fileset>
<fileset dir="path">
<include name="d/**"/>
</fileset>
</difference>
<fileset dir="path">
<include name="d/db/**"/>
</fileset>
</union>
<map from="${basedir}/" to=""/>
</pathconvert>
<javac
srcdir="."
includes="${union-path}"
includeantruntime="false"
/>

Copy Multiple Files and Directories with ANT

I have an Ant Script, using the ANT LIBRARY how can i copy multiple files/folders to multiple directories. I use a properties file which contains
FileToCopy = DestinationFolder
FolderToCopy = FolderDestination
Copy a single file
<copy file="myfile.txt" tofile="mycopy.txt"/>
Copy a single file to a directory
<copy file="myfile.txt" todir="../some/other/dir"/>
Copy a directory to another directory
<copy todir="../new/dir">
<fileset dir="src_dir"/>
</copy>
Copy a set of files to a directory
<copy todir="../dest/dir">
<fileset dir="src_dir">
<exclude name="**/*.java"/>
</fileset>
</copy>
<copy todir="../dest/dir">
<fileset dir="src_dir" excludes="**/*.java"/>
</copy>
examples from copy ant task
Some additional information to sergiofbsilvas answer, because I was searching for such an example.
One can also specify multiple filesets in a single copy task.
Example:
<copy todir="${temp.dir}">
<fileset dir="${classes.dir}"/>
<fileset dir="${basedir}">
<include name="log4j.xml"/>
<include name="config.properties"/>
<include name="kfatransfer.bat"/>
</fileset>
</copy>
Tested with ant 1.10.6 on Windows.

How do I use ant dirset with the ear task?

I'm building an EAR with ant which needs to include the lib folder (including jars) from my EAR project. I've tried this but although a lib folder is created in the ear file no jars are included. Only the war files are copied into the ear.
<ear destfile="${ear.file}" appxml="META-INF/application.xml">
<dirset dir=".">
<include name="lib" />
</dirset>
<fileset dir="${temp.dir}">
<include name="*.war" />
</fileset>
</ear>
I used the zipfileset task instead, which does the trick:
<ear destfile="${ear.file}" appxml="META-INF/application.xml">
<zipfileset dir="lib" prefix="lib"/>
<fileset dir="${temp.dir}">
<include name="*.war" />
</fileset>
</ear>

Categories

Resources