Ant : As Build Tool :: Ant with Java Application
Ant with java Application |
We’ll construct a “Calculator” program and use Ant to build and assemble it for execution and distribution. We’ll have two Java classes and a properties file involved just to make it slightly more interesting, and we’ll end up with an executable Java Archive (JAR) file that is ready to run.
In this example, We have following files as below.
F:\antExample\Example2\
+-- build.xml
+-- src\
+-- Calculator.java
+-- TestDrive.java
The .java files will need to be compiled and placed in the proper package directory hierarchy. The .class files must be included in the executable JAR, and the MANIFEST.MF file must be used as the manifest for the executable JAR because it contains the magic to make a JAR execute. All four of these files must be included in a source distribution JAR file.
Examples |
Create two .java file with following name in src folder
Calculator.java
package com.javaskool;
class Calculator
{
public void Add(int a,int b)
{
System.out.println("Addition : "+(a+b));
}
}
TestDrive.java
package com.javaskool;
class TestDrive
{
public static void main(String[] args)
{
System.out.println("Ant Example");
Calculator s1=new Calculator();
s1.Add(45,89);
}
}
build.xml
<?xml version="1.0"?>
<project name="firstJavaProject" default="jar">
<!--
* compile the java sources
* create a jar file
* execute
-->
<target name="clean">
<delete dir="build/classes"/>
</target>
<target name="init" depends="clean">
<mkdir dir="build/classes" />
</target>
<target name="compile" depends="init">
<javac srcdir="src" destdir="build/classes" />
</target>
<target name="jar" depends="compile">
<jar destfile="firstAntApp-1.0.jar" basedir="build/classes">
<manifest>
<attribute name="Main-Class" value="com.javaskool.TestDrive"/>
</manifest>
</jar>
</target>
</project>
Follow the screen commands for Output
<em>//build.xml and src folder in current directory as below</em>
F:\antExample\Example2>dir
Volume in drive F is workspace
Volume Serial Number is E846-4436
Directory of F:\antExample\Example2
20-04-2014 17:06 <DIR> .
20-04-2014 17:06 <DIR> ..
20-04-2014 17:06 630 build.xml
20-04-2014 17:06 <DIR> src
1 File(s) 630 bytes
3 Dir(s) 5,008,269,312 bytes free
<em>//Files in src folder</em>
F:\antExample\Example2>dir src
Volume in drive F is workspace
Volume Serial Number is E846-4436
Directory of F:\antExample\Example2\src
20-04-2014 17:06 <DIR> .
20-04-2014 17:06 <DIR> ..
20-04-2014 17:02 135 Calculator.java
20-04-2014 17:03 193 TestDrive.java
2 File(s) 328 bytes
2 Dir(s) 5,008,265,216 bytes free
//Running ant , will automatically read build.xml
F:\antExample\Example2>ant
Buildfile: F:\antExample\Example2\build.xml
clean:
init:
[mkdir] Created dir: F:\antExample\Example2\build\classes
compile:
[javac] F:\antExample\Example2\build.xml:18: warning: 'includeantruntime' wa
s not set, defaulting to build.sysclasspath=last; set to false for repeatable bu
ilds
[javac] Compiling 2 source files to F:\antExample\Example2\build\classes
jar:
[jar] Building jar: F:\antExample\Example2\firstAntApp-1.0.jar
BUILD SUCCESSFUL
Total time: 7 seconds
F:\antExample\Example2>java -jar firstAntApp-1.0.jar
'java' is not recognized as an internal or external command,
operable program or batch file.
<em>//Setting Jdk path</em>
F:\antExample\Example2>set PATH=C:\Program Files\Java\jdk1.7.0_03\bin;.;
<em>//Executing java code using created jar file by ant.</em>
F:\antExample\Example2>java -jar firstAntApp-1.0.jar
Ant Example
Addition : 134
F:\antExample\Example2>
Recent Comments