Ant FAQ: Ant copy task examples

Java Ant build FAQ: Can you share some examples of the Ant copy task (the Ant copy task syntax)?

Sure. I've shared quite a few Ant examples on the website now (just search the website for “Ant”), and here’s another snippet of code from an Ant build script that shows how to use the Ant copy task.

As you can see from this sample code, I'm using the Ant copy task to copy files from source locations to other destination locations.

<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}/" tofile="${temp.dir.classes}/" overwrite="true" />
<copy file="${resources.dir}/" tofile="${temp.dir.classes}/" overwrite="true" />

If you’ve ever worked with Spring, JSF, or Log4J, a lot of those property names will look familiar to you.

Ant copy - how to copy a set of files (Ant fileset example)

While I'm in the Ant copy neighborhood, here’s how you copy a set of files from one location to another, using the Ant fileset task and the include directive of the fileset task:

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