Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Apache Ant Copy Task Examples: File, FileSet, Include/Exclude, and Flatten Patterns

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.

Ant copy task example

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

PatternExample formKeeps directory treeBest for
Single file to explicit destination<copy file=’…’ tofile=’…’ />NoManifest or one config file with a fixed target name
Bulk copy with fileset<copy todir=’…’><fileset … /></copy>YesResource folders with many files
Pattern-filtered copyInclude and exclude patternsYesCopy only certain file types or paths
Flattened bulk copyflatten=’true’NoCollect 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.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
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.

Related Posts

Apache Ant Interview Questions Answered: Concepts, build.xml Examples, and Practical Snippets

This guide converts a long Apache Ant interview question list into clear, practical answers with working build.xml examples. It covers core Ant concepts, common tasks, debugging, and Ant vs Maven vs Make.

Read More

Apache Ant Comments and echo Task: Write Clear build.xml Files and Control Log Output

Learn the correct way to add comments in Apache Ant and when to use the echo task instead. This guide covers echo attributes, log-level behavior, and practical build.xml examples.

Read More

How to Run Ant Builds in IntelliJ IDEA: Setup, Troubleshooting, and Copy Task Patterns

This guide shows how to integrate a build.xml file with IntelliJ IDEA, run Ant targets from the IDE, and troubleshoot common environment-variable issues. It also compares practical Ant copy/fileset patterns with reusable examples.

Read More

Apache Ant clean, prepare, and compile Targets: A Practical build.xml Walkthrough

Learn how to structure a simple Ant build pipeline using clean, prepare, and compile targets with explicit dependencies. This revision includes a complete build.xml example, execution commands, and a workflow diagram.

Read More

Apache Ant Dependency Visualization: AntScript Visualizer, Grand, and Vizant

A practical, updated guide to three classic Apache Ant visualization tools and how they help you understand target dependencies. Includes a side-by-side comparison, workflow diagram, and example commands.

Read More

Apache Ant Task Reference for DevOps: zip, concat, replaceregexp, checksum, and fail

A practical, updated reference for five widely used Apache Ant tasks: zip, concat, replaceregexp, checksum, and fail. Learn key attributes, safe defaults, and production-ready build.xml examples.

Read More
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
1
0
Would love your thoughts, please comment.x
()
x