First Apache Ant Build File Example

This section provides a tutorial example on how to write an Apache Ant build file to compile and run a simple Java application, Hello.java.

Now let's try to create Apache Ant build file to build and run the Hello Java application.

1. Create the application home folder and sub solders:

C:\>mkdir \herong\Hello
C:\>mkdir \herong\Hello\src
C:\>mkdir \herong\Hello\build

2. Copy the application source file, Hello.java, into the source folder:

C:\>copy Hello.java \herong\Hello\src

3. Create the build file, .\build.xml, using a text editor:

<project>
<!-- build.xml
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
    <target name="compile">
        <delete dir="build"/>

        <mkdir dir="build/classes"/>
        <javac srcdir="src" destdir="build/classes"/>

        <mkdir dir="build/jar"/>
        <jar destfile="build/jar/Hello.jar" basedir="build/classes">
            <manifest>
                <attribute name="Main-Class" value="Hello"/>
            </manifest>
        </jar>
    </target>

    <target name="run">
        <java jar="build/jar/Hello.jar" fork="true"/>
    </target>
</project>

4. Test the build process with the "ant" command:

C:\>cd \herong\Hello
C:\herong\Hello>\local\apache-ant-1.9.5\bin\ant compile
Buildfile: C:\herong\Hello\build.xml

compile:
   [delete] Deleting directory C:\herong\Hello\build
    [mkdir] Created dir: C:\herong\Hello\build\classes
    [javac] C:\herong\Hello\build.xml:9: 
    warning: 'includeantruntime' was not set, defaulting 
    to build.sysclasspath=last; set to false forepeatable builds
    [javac] Compiling 1 source file to C:\herong\Hello\build\classes
    [mkdir] Created dir: C:\herong\Hello\build\jar
      [jar] Building jar: C:\herong\Hello\build\jar\Hello.jar

BUILD SUCCESSFUL
Total time: 0 seconds

C:\herong\Hello>\local\apache-ant-1.9.5\bin\ant run
Buildfile: C:\herong\Hello\build.xml

run:
     [java] Hello world!

BUILD SUCCESSFUL
Total time: 0 seconds

Congratulations, you have successfully created a working Apache Ant build file!

Note that if you are using Ant 1.8 with JDK 1.8, you will get the following error:

BUILD FAILED
C:\herong\Hello\build.xml:9: Class not found: javac1.8

You need replace Ant 1.8 with Ant 1.9.

Last update: 2015.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

Installing Apache Ant 1.9 on Windows System

 Downloading and Installing Apache Ant 1.9

 Preparations on Using Apache Ant Tool

First Apache Ant Build File Example

 Developing First Android Application - HelloAndroid

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 Android 4.0.3 File Systems

 AboutAndroid - Application to Retrieve System Information

 android.app.Activity Class and Activity Lifecycle

 View Objects and Layout Resource Files

 Using "adb logcat" Command for Debugging

 Build Process and Package File Content

 Building Your Own Web Browser

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Outdated Tutorials

 References

 PDF Printing Version