Ant : As Build Tool :: Understanding target
Understanding target |
<target name="X">
<target name="Y" depends="X">
<target name="Z" depends="X">
<target name="A" depends="Y,Z">
- Now, suppose we execute Target A that depends on Y,Z
- Y depends on X
- Z depends on X
- So first X will get executed then Y and then Z and finally A
Sample of build.xml
<?xml version="1.0"?>
<project name="myfirstAntProject" default="mytarget2">
<target name="init">
<echo>Running Target init</echo>
</target>
<target name="mytarget1" depends="init">
<echo>Runnig target mytarget1</echo>
</target>
<target name="mytarget2" depends="mytarget1">
<echo>Runnig target mytarget2</echo>
</target>
</project>
Output
F:\antExample\Example4>ant
Buildfile: F:\antExample\Example4\build.xml
init:
[echo] Running Target init
mytarget1:
[echo] Runnig target mytarget1
mytarget2:
[echo] Runnig target mytarget2
BUILD SUCCESSFUL
Total time: 2 seconds
F:\antExample\Example4>
Executing individual target by specifying name
//no target named "target2", so generating error
F:\antExample\Example4>ant target2
Buildfile: F:\antExample\Example4\build.xml
BUILD FAILED
Target "target2" does not exist in the project "myfirstAntProject".
Total time: 0 seconds
//running target "mytarget1"
F:\antExample\Example4>ant mytarget1
Buildfile: F:\antExample\Example4\build.xml
init:
[echo] Running Target init
mytarget1:
[echo] Runnig target mytarget1
BUILD SUCCESSFUL
Total time: 0 seconds
F:\antExample\Example4>
Circular dependencies
<?xml version="1.0"?>
<project name="myfirstAntProject" default="mytarget2">
<target name="mytarget1" depends=" mytarget2 ">
<echo>Runnig target mytarget1</echo>
</target>
<target name="mytarget2" depends="mytarget1">
<echo>Runnig target mytarget2</echo>
</target>
</project>
Output
F:\antExample\Example4>ant
Buildfile: F:\antExample\Example4\build.xml
BUILD FAILED
Circular dependency: mytarget2 <- mytarget1 <- mytarget2
Total time: 0 seconds
F:\antExample\Example4>
If you have different name, you can run as below
Output
F:\antExample\Example4>ant
Buildfile: build.xml does not exist!
Build failed
F:\antExample\Example4>ant -f mybuild.xml
Buildfile: F:\antExample\Example4\mybuild.xml
init:
[echo] Running Target init
mytarget1:
[echo] Runnig target mytarget1
mytarget2:
[echo] Runnig target mytarget2
BUILD SUCCESSFUL
Total time: 1 second
F:\antExample\Example4>
Example |
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
//build.xml and src folder in current directory as below
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
//Files in src folder
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.
//Setting Jdk path
F:\antExample\Example2>set PATH=C:\Program Files\Java\jdk1.7.0_03\bin;.;
//Executing java code using created jar file by ant.
F:\antExample\Example2>java -jar firstAntApp-1.0.jar
Ant Example
Addition : 134
F:\antExample\Example2>
Recent Comments