Sie sind auf Seite 1von 4

ANT KICKSTART TUTORIAL Step1: install ant Step2: Create java project/source files Create a folder called src

c in a temporary directory : temp/src Create the following package : com/ant/demo Create a Java file named Runner.java inside com/ant/demo Add the following code to Runner.java
package com.ant.demo; import com.ant.vo.Person; public class Runner{ public static void main(String[] args){ Person man = new Person(); man.setName("vinay"); man.setPhone("9876543210"); man.setPlace("Bangalore"); System.out.println(man); } }

Create a Java file named Person.java inside com/ant/vo

Add the following code to Person.java


package com.ant.vo; public class Person{ String name = null; String phone = null; String place = null; public void setName(String name){ this.name = name; } public void setPhone(String phone){ this.phone = phone; } public void setPlace(String place){ this.place = place; } public String getName(){ return name; } public String getPhone(){ return phone; } public String getPlace(){ return place; } @Override public String toString(){ return new StringBuilder("Record:").append(getName()).append(getPhone()).append(getPlace()).toString(); } }

Now we have the following directory structure

Now the objective is: - Create a destination folder (called build/classes) to contain all compiled classes - Create a destination folder (called dist) to contain the distributable jar file - Compile all the java files in src folder and place it in build folder - Create a distributable jar file using the compiled classes - Provide option to clean the build and dist folder Creating ant build-script: Now navigate to temp directory and create build.xml file. If you prefer to use a different filename say project-build.xml then every time you run ant this file name needs to be passed as an argument. The only advantage of using name build.xml is that this name is taken as default filename and thus allows the user to type-in just ant without any command-line arguments. Create an ant project & specify the following - project name (mandatory - encloses all the targets that applies for a particular project) - default target - base directory to start with
<project name="demo" default="build" basedir="."> </project>

Create global properties inside project tag that describes folders in use - src contains java source files - build/classes contains compiled classes - dist contains jar file built using latest compiled classes - custom global variables can also be included at this point
<property <property <property <property name="source" value="src"/> name="build" value="build/classes"/> name="dist" value="dist"/> name="author" value="vinay"/>

Creating targets Target tag is a block that encloses small unit of tasks which are generally java or shell commands. Target: clean Our first target here would be to clean any existing directory with src and build/classes name. Add the following target inside project tag
<target name="clean" > <delete dir="${build}" /> <delete dir="${dist}" /> </target>

One thing here to note is since we are deleting ${build}, which has a value of build/classes, only classes folder will be deleted. If you want to delete complete build folder, either you can have another variable set or directly pass the build-directory name (in our case build). Target: init Once we are sure that there are no existing directories, next step would be to create the mentioned destination directory newly. Add the following target inside project tag
<target name="init" depends="clean"> <mkdir dir="${build}" /> <mkdir dir="${dist}" /> </target>

Here, the depends attribute makes sure that the dependent target is executed prior to the caller target. In our case, target clean is executed first and then target init will be executed. Target: compile Once all output directories are set, the next step is to compile all the classes in the src directory and place the compiled classes in the destination directory (build/classes). Add the following target inside project tag
<target name="compile" depends="init"> <javac srcdir="${source}" destDir="${build}"/> </target>

Here, inside the target we are calling the typical javac command to compile by specifying source and destination directories. Target: build The final step in our build-script is to package the compiled classes to a distributable jar file. While packaging we have to keep one thing in mind, if there is any starting point required that is if there is any class containing main method we need to specify it in the MANIFEST.MF file and place it inside the jar file.

Add the following target inside project tag


<target name="build" depends="compile"> <jar jarfile="${dist}/demo.jar" basedir="${build}"> <manifest> <attribute name="Built-By" value="${author}"/> <attribute name="Main-Class" value="com.ant.demo.Runner"/> </manifest> </jar> </target>

In the above sample, we create a section using jar command and to it we embed another section called manifest which contains the actual main class information. All the attributes mentioned within the manifest section will be written to META-INF/MANIFEST.MF file inside the newly created jar file. Sample SRC files
Runner.jav a

Person.jav a

(rename to build.xml)