Skip to main content

2 posts tagged with "Ant"

View All Tags

Hello World using Apache ANT

· 3 min read
Sandeep Bhardwaj

Old way of compile a project, making jar and running the jar

Project Structure


Make project structure using command prompt like

H:\>md DemoProject  
H:\>md DemoProject\src\hello
H:\>md DemoProject\build\classes

Make a java class


Now make a simple java class HelloWorld

package hello;  

public class HelloWorld {

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

}

Compile the code


Compile the java cod using the command below

H:\>javac -sourcepath DemoProject\src -d DemoProject\build\classes DemoProject\src\hello\HelloWorld.java  

Make the jar


To make the jar file we need to create manifest file containing the information of class with contain main method.

H:\>cd DemoProject  
H:\DemoProject>md build\jar
H:\DemoProject>jar cfm build\jar\HelloWorld.jar myManifest -C build\classes .

Run the code


Now we can run our HelloWorld jar file using the command below.

H:\DemoProject>java -jar build\jar\HelloWorld.jar Hello World  

Now doing with Smarter way using Ant

By using ant we just need to execute 4 steps.
Create only source (src) directory and place HelloWorld.java in to hello folder and manifest file parallel to src folder do not need to create build directory, it will created by ant.

H:\>md DemoProject  
H:\>md DemoProject\src\hello

Step 1:


Create a build.xml file (By default Ant uses build.xml) in to DemoProject directory (parallel to the src directory)

<project>
<target name="clean">
</target>

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

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

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

Step 2:


Compile the code (move to the DemoProject directory before running the commands)

H:\>cd DemoProject  

H:\DemoProject>ant compile
Buildfile: H:\DemoProject\build.xml

compile:
[mkdir] Created dir: H:\DemoProject\build\classes
[javac] H:\DemoProject\build.xml:9: warning: 'includeantruntime' was not set
, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to H:\DemoProject\build\classes

BUILD SUCCESSFUL
Total time: 8 seconds

Step 3


Make the jar (pack the code)

H:\DemoProject>ant jar  
Buildfile: H:\DemoProject\build.xml

jar:
[mkdir] Created dir: H:\DemoProject\build\jar
[jar] Building jar: H:\DemoProject\build\jar\HelloWorld.jar

BUILD SUCCESSFUL
Total time: 4 seconds

Step 4:


Run the code

H:\DemoProject>ant run  
Buildfile: H:\DemoProject\build.xml

run:
[java] Hello World

BUILD SUCCESSFUL
Total time: 4 seconds

Now you can see that using ANT makes our work easy and simpler than old technique. We can also execute all these commands in one line like this

H:\DemoProject>ant compile jar run  
Buildfile: H:\DemoProject\build.xml

compile:
[mkdir] Created dir: H:\DemoProject\build\classes
[javac] H:\DemoProject\build.xml:9: warning: 'includeantruntime' was not set
, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 1 source file to H:\DemoProject\build\classes

jar:
[mkdir] Created dir: H:\DemoProject\build\jar
[jar] Building jar: H:\DemoProject\build\jar\HelloWorld.jar

run:
[java] Hello World

BUILD SUCCESSFUL
Total time: 11 seconds

Hope you like this post.
I used apache reference manual for this blog its good and simple. Use Apache manual for depth knowledge.

Installing Apache Ant

· One min read
Sandeep Bhardwaj

Apache Ant is a Java-based build tool.

Download Ant


Download Apache ANT from this URL. http://ant.apache.org/bindownload.cgi

Installing Ant

The binary distribution of Ant consists of the following directory layout:
ant

   +--- bin  // contains launcher scripts  
|
+--- lib // contains Ant jars plus necessary dependencies
|
+--- Extra directories

Only the bin and lib directories are required to run Ant.
To install Ant, choose a directory and copy the distribution files and extract there. This directory will be known as ANT_HOME.

Setup


The ant.bat script makes use of three environment variables –

  1. ANT_HOME,
  2. CLASSPATH (optional not required )and
  3. JAVA_HOME.

Assume Ant is installed in c:\ apache-ant-1.8.1. The following sets up the environment:

set ANT_HOME=c:\apache-ant-1.8.1  
set JAVA_HOME=C:\jdk1.6.0_20
set PATH=%PATH%;%ANT_HOME%\bin

Check Installation


You can check the basic installation with opening a command prompt and typing

C:\>ant -version  
Apache Ant version 1.8.1 compiled on April 30 2010