MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

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

Jenkins Build and Release Training Blueprint: Git/SVN with Maven and Ant

This revised guide turns a legacy training announcement into a practical learning blueprint for build-and-release engineering. It maps the Jenkins workflow, compares core tools, and includes realistic command and pipeline examples.

Read More

Gradle for DevOps: Core Concepts, Build Workflow, and Practical Examples

Gradle is a flexible build automation tool used to compile, test, package, and publish software in repeatable pipelines. This guide explains how Gradle works, how it compares with Maven and Ant, and how to start with practical commands.

Read More

Apache Ant Training Guide: Curriculum, Build Workflow, and Hands-On Labs

Use this structured Apache Ant training guide to evaluate or design an Ant-focused course for Java build and release engineering. It summarizes the curriculum, demonstrates a realistic build.xml workflow, and compares Ant with Maven and Gradle.

Read More

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

Maven vs Ant: Key Differences, Build Workflow, and When to Use Each

Maven and Ant both automate Java builds, but they use very different models. This guide compares their workflows, configuration style, and CI implications with practical examples.

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
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Puja kushwaha
Puja kushwaha
27 days ago

The Ant copy task is useful for moving files and resources between build folders. Using clear source and destination paths helps prevent files from landing in the wrong place. Include and exclude patterns can also keep temporary files out of the final package. It is worth checking whether the destination needs cleaning before each build to avoid stale files. These practices make copy tasks more predictable and help keep build outputs organized.

1
0
Would love your thoughts, please comment.x
()
x