Tech and Media Labs
This site uses cookies to improve the user experience.




Your First Ant Project

Jakob Jenkov
Last update: 2015-08-18

Once you have installed Ant it is time to create your first Ant build script and run it with Ant. This tutorial will show you how to do that.

The title of this tutorial is "Your First Ant Project". By "Ant Project" I mean a Java project which is built with Ant. The Java part of the project is just an ordinary Java project. What makes it an "Ant project" is only that it is built with Ant. Actually, you could build the same Java project with Ant, Maven and Gradle, so in that respect that is nothing specifically "Ant-ish" about your first Ant project.

Creating a Project Directory

Before we create the Ant build script we must first create a directory to contain the project we are going to build. Create a directory with the name of the project somewhere on your hard disk where you want the project located. E.g. my-first-ant-project .

The Ant Build Script

Inside the project directory you have just created, create an empty file named build.xml. By default Ant looks for a build script named build.xml in your project root directory, so you might as well call your Ant build script build.xml. You could use another name, but using build.xml makes your Ant commands shorter, and it makes it easier for other developers to find the Ant build script.

Open the build.xml file and insert the following text (XML) into it:

<project>

  <target name="firstTarget">
    <echo>My First Ant Project!</echo>
  </target>

</project>

You can test this build file by opening a command prompt and change directory into the directory that contains the build.xml file and execute this command:

ant firstTarget

This command will execute the firstTarget target inside the build script. The firstTarget target just prints out the text "My First Ant Project!" to the console. That is all. The output should look similar to this:

D:\data\projects\build-experiments>ant firstTarget
Buildfile: D:\data\projects\build-experiments\build.xml

firstTarget:
     [echo] My First Ant Project!!

BUILD SUCCESSFUL
Total time: 0 seconds

D:\data\projects\build-experiments>

That is it! You have now created your first Ant project with a single target inside. Don't worry if you don't understand projects and targets just yet. It will be explained in more detail in the following texts of this Ant tutorial.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC