Apache Ant’s copy task is a core build step for moving resources into output directories. This guide helps build and release engineers choose the right copy pattern, avoid common mistakes, and reuse practical snippets in build.xml.
Originally published on 2010-07-22; fully revised and updated.

Quick syntax reference for the Ant copy task
Use file and tofile for one-to-one copies, and use todir with a nested fileset for bulk copies selected by patterns.
<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}/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' />Code language: HTML, XML (xml)
<copy todir='${temp.dir.classes}'>
<fileset dir='${src.dir}'>
<include name='**/*.xml' />
<include name='**/*.xsl' />
<include name='**/*.properties' />
</fileset>
</copy>Code language: HTML, XML (xml)
Choose the right copy pattern
| Pattern | Example form | Keeps directory tree | Best for |
|---|---|---|---|
| Single file to explicit destination | <copy file=’…’ tofile=’…’ /> | No | Manifest or one config file with a fixed target name |
| Bulk copy with fileset | <copy todir=’…’><fileset … /></copy> | Yes | Resource folders with many files |
| Pattern-filtered copy | Include and exclude patterns | Yes | Copy only certain file types or paths |
| Flattened bulk copy | flatten=’true’ | No | Collect jars and zips into one runtime lib directory |
Practical examples for include, exclude, flatten, and cleanup
Copy images while excluding GIF files (attribute form)
<copy todir='${dist}'>
<fileset dir='${src}' includes='**/images/*' excludes='**/*.gif' />
</copy>Code language: HTML, XML (xml)
The same filter using nested include and exclude elements
<copy todir='${dist}'>
<fileset dir='${src}'>
<include name='**/images/*' />
<exclude name='**/*.gif' />
</fileset>
</copy>Code language: HTML, XML (xml)
Flatten nested libraries into one output 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>Code language: HTML, XML (xml)
Delete the original selection after copying (when your process requires cleanup)
<delete dir='${dist}'>
<include name='**/images/*' />
<exclude name='**/*.gif' />
</delete>Code language: HTML, XML (xml)
Copy-stage workflow in a typical Ant build
This flow shows where targeted copies, filtered copies, and optional cleanup usually sit between compilation and packaging.
flowchart LR
A[Compile sources] --> B[Copy fixed config files]
B --> C[Copy fileset resources]
C --> D{Use flatten}
D -- Yes --> E[Write into one lib directory]
D -- No --> F[Keep source folder layout]
E --> G[Package artifact]
F --> G
G --> H[Optional cleanup]
Best practices and common pitfalls
- Use Ant properties for all source and destination paths so environments stay portable.
- Prefer explicit include and exclude rules instead of copying entire trees blindly.
- Be careful with
flatten='true': files with identical names from different folders can overwrite each other. - Use
overwrite='true'only when deterministic replacement is desired. - Keep copy and delete rules close to packaging targets so the build intent is obvious.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals