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 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

ANT_OPTS in Apache Ant: JVM Memory, Proxy Settings, and Startup Troubleshooting

ANT_OPTS passes JVM options to the Java process that runs Ant. This guide explains how ANT_OPTS differs from ANT_ARGS and JAVACMD, with practical examples for memory tuning, proxy setup, and startup error diagnosis.

Read More

Apache Ant Complete Guide: build.xml Structure, Commands, CI Workflow, and Tool Comparison

Learn how Apache Ant works in real DevOps workflows, from core build.xml concepts to practical command-line and CI usage. This revised guide also compares Ant with Maven and Gradle so you can choose the right build approach.

Read More

Apache Ant for DevOps Engineers: build.xml Basics, CLI Workflow, and Tool Comparison

This guide explains what Apache Ant does, how a build.xml file is structured, and how to run repeatable builds from the command line. It also compares Ant with Maven and Gradle to help with tool selection.

Read More

Using Apache Ant to Build J2EE Applications: Workflow, build.xml Template, and Commands

A practical, modernized guide to building legacy J2EE/Java EE applications with Apache Ant. Learn the build workflow, use a reusable build.xml template, and compare Ant with Maven and Gradle.

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