Copy file using Apache Ant

Apache Ant is a tool for building Java projects. In the old days, when working at my old company, I always used it. But now, it is less used because of Apache Maven. In this tutorial, I will guide you how to copy a file using Apache Ant.

I will create a Java project as an example:

Copy file using Apache Ant

Next, I will create an XML file to work with Apache Ant. You can name this file by any name. But when you name it to build.xml, Eclipse will understand it is the file using Apache Ant to run. In this tutorial, I will name it to build.xml.

Copy file using Apache Ant

And I will copy a file named example.txt in the project directory to the src directory.

Copy file using Apache Ant

We will use Apache Ant’s copy task to copy. The contents of the build.xml file will look like this:

Notice the line:

This is a copy task of Apache Ant in which the file attribute is the filename to copy, while the todir attribute is the directory path that will contain the file after the copy.

Now, right-click on the build.xml file, select Run as then Ant build, and we’ll get the following output:

Copy file using Apache Ant

And:

Copy file using Apache Ant


Add Comment