ant warning: warning: ‘includeantruntime’ was not set

This is a WARNING. People say this is given by a misfeature introduced in Ant 1.8. You may need to add another attribute to javac as below

<javac srcdir="src" destdir="build/classes" includeantruntime="false">

Advertisements

Ant – the packaging system for Java – HowTo 2

Let’s be specific about java in this post. If you start reading this post afresh, please have a look at HowTo 1

ant

Create a folder structure

+
--src
----HelloAnt.java
--class
----HelloAnt.class
build.xml

HelloAnt.java

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

build.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="HelloAnt" basedir="." default="jar">
<target name="clean" description="delete all classes">
<delete dir="classes"/>
<delete file="HelloAnt.jar"/>
</target>

<target name="compile" description="compiles the task">
<mkdir dir="classes"/>
<javac srcdir="src" destdir="classes"/>
</target>

<target name="jar" description="jars the task">
<jar destfile="HelloAnt.jar" basedir="classes"/>
</target>
</project>

Build process

ant clean
ant compile
ant jar

Verify the build

pandian@Kannan:~/Downloads/ant/sample$ java -cp HelloAnt.jar HelloAnt
Hello Ant

Ant – the packaging system for Java – HowTo 1

ant

Step -1: Download Ant

You can download the latest binary from http://ant.apache.org/

Step -2: Download JDK

JDK is mandatory.

Step -3: Installation

This tutorial examples are taken from Linux. But the commands should be common for all the platforms.

Extract the downloaded ant binary.
Goto ant/bin folder
Try to run ant

pandian@Kannan:~/Downloads/ant$ ant
Buildfile: build.xml does not exist!
Build failed

The Ant binary will contains an xml called fetch.xml. Execute the following command where the fetch.xml is available

pandian@Kannan:~/Downloads/ant/apache-ant-1.8.3$ ant -f fetch.xml -Ddest=system
Buildfile: /home/pandian/Downloads/ant/apache-ant-1.8.3/fetch.xml
.....
BUILD SUCCESSFUL
Total time: 4 minutes 14 seconds

Step -4: Build XML file

The build instructions are given in XML format. By default ant looks for build.xml. That contains several tasks called targets. To explain the terminologies, A single project has multiple tasks – compile, build jar, upload etc.

A sample build.xml is given below.
The project name is “test project”. It contains a single target called ‘initialize’ (more will be added later)

pandian@Kannan:~/Downloads/ant$ cat build.xml
<?xml version="1.0"?>
<project name="test project" default="initialize">

<target name="initialize">
<echo>This is a test project</echo>
</target>
</project>

Execute ant

pandian@Kannan:~/Downloads/ant$ ant initialize
Buildfile: /home/pandian/Downloads/ant/build.xml

initialize:
[echo] This is a test project

BUILD SUCCESSFUL
Total time: 0 seconds

Step -5: Read externalized parameters from property files

You can have the important parameters or fields in a property file and import it to build process to make it more flexible. Here is how you can do it.

I have a property file with 3 parameters

pandian@Kannan:~/Downloads/ant$ cat ant.properties
name=grassfield
city=London
country=United Kingdom

I am adding a new target to read and print one of the params.

<target name="get_properties">
<property file="ant.properties"/>
<echo>Imported Properties for name ${name}</echo>
</target>

Now, lets execute the target

pandian@Kannan:~/Downloads/ant$ ant get_properties
Buildfile: /home/pandian/Downloads/ant/build.xml

get_properties:
[echo] Imported Properties for name grassfield

BUILD SUCCESSFUL
Total time: 0 seconds