Samples/Examples of the Ant copy task – Guide

ant-copy-task

Samples of the Ant copy task

<copy file=”${resources.dir}/MANIFEST.MF” tofile=”${temp.dir.meta-inf}/MANIFEST.MF” overwrite=”true” />
<copy file=”${resources.dir}/managed-beans.xml” tofile=”${temp.dir.web-inf}/managed-beans.xml” overwrite=”true” />
<copy file=”${resources.dir}/navigation-rules.xml” tofile=”${temp.dir.web-inf}/navigation-rules.xml” overwrite=”true” />
<copy file=”${resources.dir}/monitoring-managed-beans.xml” tofile=”${temp.dir.web-inf}/monitoring-managed-beans.xml” overwrite=”true” />
<copy file=”${resources.dir}/monitoring-navigation-rules.xml” tofile=”${temp.dir.web-inf}/monitoring-navigation-rules.xml” overwrite=”true” />
<copy file=”${resources.dir}/faces-config.xml” tofile=”${temp.dir.web-inf}/faces-config.xml” overwrite=”true” />
<copy file=”${resources.dir}/log4j.properties” tofile=”${temp.dir.classes}/log4j.properties” overwrite=”true” />
<copy file=”${resources.dir}/commons-logging.properties” tofile=”${temp.dir.classes}/commons-logging.properties” overwrite=”true” />

<copy todir=”${temp.dir.classes}”>
<fileset dir=”${src.dir}”>
<include name=”**/*.xml”/>
<include name=”**/*.xsl”/>
<include name=”**/*.properties”/>
</fileset>
</copy>

<copy todir=”${dist}”>
<fileset dir=”${src}”
includes=”**/images/*”
excludes=”**/*.gif”
/>
</copy>

This copies all files in directories called images that are located in the directory tree defined by ${src} to the destination directory defined by ${dist}, but excludes all *.gif files from the copy.

<copy todir=”${dist}”>
<fileset dir=”${src}”>
<include name=”**/images/*”/>
<exclude name=”**/*.gif”/>
</fileset>
</copy>

The same as the example above, but expressed using nested elements.

<delete dir=”${dist}”>
<include name=”**/images/*”/>
<exclude name=”**/*.gif”/>
</delete>

Deleting the original set of files, the delete task can act as an implicit fileset.

Flatten directories when performing an Ant copy
Here’s a code snippet from an Ant build script where I begin with a hierarchical structure of jar files in my development environment, then flatten out all my lib subdirectories into one directory in my production environment when I copy all the jar files to my production library directory:

<!– build a temporary lib dir, and flatten out the jars into one folder –>

<copy todir=”${temp.dir.lib}” flatten=”true”>
<fileset dir=”${lib.dir}”>
<exclude name=”${cob.lib.dir}” />
<exclude name=”junit*” />
<include name=”**/*.jar”/>
<include name=”**/*.zip”/>
</fileset>
</copy>

 

Tagged : / / / / / / / / /