Hands-on Tutorial: Hello World with Ant 3

(On Windows system)

 

 

 

 

 

 

 
 

Enhancing the build file

 

Now we have a working buildfile then we could do some enhancements: many times you are referencing the same directories, main-class and jar-name are hard coded, and while doing the invocation, you have to remember the right order of build steps.

The first and second point would be addressed with properties, the third with a special property - an attribute of the <project> tag and the fourth problem can be solved using dependencies.

 

<project name="HelloWorld" basedir="." default="main">

 

    <property name="src.dir"     value="src"/>

 

    <property name="build.dir"   value="build"/>

    <property name="classes.dir" value="${build.dir}/classes"/>

    <property name="jar.dir"     value="${build.dir}/jar"/>

 

    <property name="main-class" value="oata.HelloWorld"/>

 

    <target name="clean">

        <delete dir="${build.dir}"/>

    </target>

 

    <target name="compile">

        <mkdir dir="${classes.dir}"/>

        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>

    </target>

 

    <target name="jar" depends="compile">

        <mkdir dir="${jar.dir}"/>

        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">

            <manifest>

                <attribute name="Main-Class" value="${main-class}"/>

            </manifest>

        </jar>

    </target>

 

    <target name="run" depends="jar">

        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>

    </target>

 

    <target name="clean-build" depends="clean,jar"/>

 

    <target name="main" depends="clean,run"/>

 

</project>

 

So, edit your previous build.xml file using the given script. Now it's easier, just do an ant and you will get

 

 

Buildfile: build.xml

 

clean:

 

compile:

    [mkdir] Created dir: C:\...\build\classes

    [javac] Compiling 1 source file to C:\...\build\classes

 

jar:

    [mkdir] Created dir: C:\...\build\jar

      [jar] Building jar: C:\...\build\jar\HelloWorld.jar

 

run:

     [java] Hello World

 

main:

 

BUILD SUCCESSFUL

 

Compiling the Java source code, creating JAR file and running the JAR file using Ant tool

 

Complete messages are dump in the following paragraph.

 

C:\myantexercise>ant -v compile jar run

Apache Ant version 1.7.1 compiled on June 27 2008

Buildfile: build.xml

Detected Java version: 1.6 in: C:\Program Files\Java\jdk1.6.0_03\jre

Detected OS: Windows XP

parsing buildfile C:\myantexercise\build.xml with URI = file:/C:/myantexercise/build.xml

Project base dir set to: C:\myantexercise

[antlib:org.apache.tools.ant] Could not load definitions from resource org/apache/tools/ant/antlib.xml.

It could not be found.

Build sequence for target(s) `compile' is [compile]

Complete build sequence is [compile, clean, jar, run, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up t

o date.

Build sequence for target(s) `jar' is [compile, jar]

Complete build sequence is [compile, jar, clean, run, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up t

o date.

 

jar:

    [mkdir] Skipping C:\myantexercise\build\jar because it already exists.

      [jar] oata omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/ is up to date.

      [jar] oata\HelloWorld.class omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/HelloWorld.c

lass is up to date.

      [jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.

      [jar] Location: C:\myantexercise\build.xml:22:

Build sequence for target(s) `run' is [compile, jar, run]

Complete build sequence is [compile, jar, run, clean, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up t

o date.

 

jar:

    [mkdir] Skipping C:\myantexercise\build\jar because it already exists.

      [jar] oata omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/ is up to date.

      [jar] oata\HelloWorld.class omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/HelloWorld.c

lass is up to date.

      [jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.

      [jar] Location: C:\myantexercise\build.xml:22:

 

run:

     [java] Executing 'C:\Program Files\Java\jdk1.6.0_03\jre\bin\java.exe' with arguments:

     [java] '-jar'

     [java] 'C:\myantexercise\build\jar\HelloWorld.jar'

     [java]

     [java] The ' characters around the executable and arguments are

     [java] not part of the command.

     [java] Hello World

 

BUILD SUCCESSFUL

Total time: 0 seconds

C:\myantexercise>ant -v compile jar run

Apache Ant version 1.7.1 compiled on June 27 2008

Buildfile: build.xml

Detected Java version: 1.6 in: C:\Program Files\Java\jdk1.6.0_03\jre

Detected OS: Windows XP

parsing buildfile C:\myantexercise\build.xml with URI = file:/C:/myantexercise/build.xml

Project base dir set to: C:\myantexercise

[antlib:org.apache.tools.ant] Could not load definitions from resource org/apache/tools/ant/antlib.xml. It could not be fo

und.

Build sequence for target(s) `compile' is [compile]

Complete build sequence is [compile, clean, jar, run, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up to date.

Build sequence for target(s) `jar' is [compile, jar]

Complete build sequence is [compile, jar, clean, run, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up to date.

 

jar:

    [mkdir] Skipping C:\myantexercise\build\jar because it already exists.

      [jar] oata omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/ is up to date.

      [jar] oata\HelloWorld.class omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/HelloWorld.class is up to date

.

      [jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.

      [jar] Location: C:\myantexercise\build.xml:22:

Build sequence for target(s) `run' is [compile, jar, run]

Complete build sequence is [compile, jar, run, clean, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up to date.

 

jar:

    [mkdir] Skipping C:\myantexercise\build\jar because it already exists.

      [jar] oata omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/ is up to date.

      [jar] oata\HelloWorld.class omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/HelloWorld.class is up to date

.

      [jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.

      [jar] Location: C:\myantexercise\build.xml:22:

 

run:

     [java] Executing 'C:\Program Files\Java\jdk1.6.0_03\jre\bin\java.exe' with arguments:

     [java] '-jar'

     [java] 'C:\myantexercise\build\jar\HelloWorld.jar'

     [java]

     [java] The ' characters around the executable and arguments are

     [java] not part of the command.

     [java] Hello World

 

BUILD SUCCESSFUL

Total time: 0 seconds

C:\myantexercise>ant -v compile jar run

Apache Ant version 1.7.1 compiled on June 27 2008

Buildfile: build.xml

Detected Java version: 1.6 in: C:\Program Files\Java\jdk1.6.0_03\jre

Detected OS: Windows XP

parsing buildfile C:\myantexercise\build.xml with URI = file:/C:/myantexercise/build.xml

Project base dir set to: C:\myantexercise

[antlib:org.apache.tools.ant] Could not load definitions from resource org/apache/tools/ant/antlib.xml. It could not be found.

Build sequence for target(s) `compile' is [compile]

Complete build sequence is [compile, clean, jar, run, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up to date.

Build sequence for target(s) `jar' is [compile, jar]

Complete build sequence is [compile, jar, clean, run, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up to date.

 

jar:

    [mkdir] Skipping C:\myantexercise\build\jar because it already exists.

      [jar] oata omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/ is up to date.

      [jar] oata\HelloWorld.class omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/HelloWorld.class is up to date.

      [jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.

      [jar] Location: C:\myantexercise\build.xml:22:

Build sequence for target(s) `run' is [compile, jar, run]

Complete build sequence is [compile, jar, run, clean, main, clean-build, ]

 

compile:

    [mkdir] Skipping C:\myantexercise\build\classes because it already exists.

    [javac] oata\HelloWorld.java omitted as C:\myantexercise\build\classes\oata\HelloWorld.class is up to date.

 

jar:

    [mkdir] Skipping C:\myantexercise\build\jar because it already exists.

      [jar] oata omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/ is up to date.

      [jar] oata\HelloWorld.class omitted as C:\myantexercise\build\jar\HelloWorld.jar:oata/HelloWorld.class is up to date.

      [jar] No Implementation-Title set.No Implementation-Version set.No Implementation-Vendor set.

      [jar] Location: C:\myantexercise\build.xml:22:

 

run:

     [java] Executing 'C:\Program Files\Java\jdk1.6.0_03\jre\bin\java.exe' with arguments:

     [java] '-jar'

     [java] 'C:\myantexercise\build\jar\HelloWorld.jar'

     [java]

     [java] The ' characters around the executable and arguments are

     [java] not part of the command.

     [java] Hello World

 

BUILD SUCCESSFUL

Total time: 0 seconds

C:\myantexercise>

 


 

 Ant 2 | Back to Main | Ant 4