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:
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.
And I will copy a file named example.txt in the project directory to the src directory.
We will use Apache Ant’s copy task to copy. The contents of the build.xml file will look like this:
1 2 3 4 5 6 |
<project basedir="." default="copy-file"> <target name="copy-file"> <copy file="example.txt" todir="src" /> </target> </project> |
Notice the line:
1 |
<copy file="example.txt" todir="src" /> |
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:
And: