Apache Ant – A simple Ant build project

Haven’t worked with Apache Ant for a long time. It’s time to pick up it again. Here is a simple Java project could be built by Ant.

Project hierarchy

- hello-world-ant (project root folder)
  - src
    - com
      - eureka
        - HelloWorld.java
  - build.xml

 

HelloWorld.java

package com.eureka;

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello World");
  }
}

 

build.xml

<project name="hello-world-ant" basedir="." default="main">

  <!-- Ant properties -->
  <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="com.eureka.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>

 

4. Run Ant!
simple-ant-build-java-project
 

Done =)

Reference: Tutorial: Hello World with Apache Ant

Advertisements

8 thoughts on “Apache Ant – A simple Ant build project”

  1. Hi…followed the above steps :
    am getting an error in the run target
    Error: Could not find or load main class com.eureka.HelloWorld
    how do i solve the error?

    Like

    1. Can you check the MANIFEST.MF inside the built hello-world-ant.jar. it should contains the following text.

      Manifest-Version: 1.0
      Ant-Version: Apache Ant 1.9.2
      Created-By: 1.7.0_25-b17 (Oracle Corporation)
      Main-Class: com.eureka.HelloWorld
      

      Like

  2. verified the MANIFEST.MF,contains the following:
    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.8.3
    Created-By: 1.7.0_21-b11 (Oracle Corporation)
    Main-Class: com.eureka.HelloWorld

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s