Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

SE Tools and Practices lab 5: Automating Build

process with ANT

Creating a project builder Ant buildfile

 We will create a simple project with a single source file and an Ant buildfile that jars up the single
class file.
1. Create a Java project named HelloWorld.
2. Create a Java source file named HelloWorld with a main method.
3. Put a single System.out.println() statement in the main method, and make it print a greeting of your
choice.
4. Save changes.
5. Create a file named build.xml, open the Ant editor on it, enter the following content, and save
changes.
<?xml version="1.0" encoding="UTF-8"?>
<project name="HelloWorld.makejar" default="makejar" basedir=".">
<target name ="makejar" description="Create a jar for the HelloWorld project">
<jar jarfile="HelloWorld.jar" includes="*.class" basedir="bin"/> </target>
</project>

6. In one of the navigation views, select the HelloWorld project and choose Properties from its context
menu.
7. In the project properties dialog, select Builders, then click New....
8. In the Choose configuration type dialog, select Ant build, and click OK.

Prepared by Feyisa K. Page 1


SE Tools and Practices lab 5: Automating Build
process with ANT

9. The External Tools dialog appears. Set the name to Makejar. In the Main tab, click the Buildfile
Browse Workspace... and set the Location to be the build.xml buildfile created above. Then click
the Base Directory Browse Workspace... and set the Base Directory to be the HelloWorld project.

Prepared by Feyisa K. Page 2


SE Tools and Practices lab 5: Automating Build
process with ANT

10. In the Refresh tab, we want to be sure that when our HelloWorld.jar is created, we see it in
Eclipse. By default, no refreshing is done when a project builder finishes running, so check Refresh
resource upon completion, then select The project containing the selected resource in the list of
scoped variables. Because refreshing can be expensive, you should, in general, refresh the smallest
entity that contains all resources that will be affected by your buildfile.

Prepared by Feyisa K. Page 3


SE Tools and Practices lab 5: Automating Build
process with ANT

11. In the Targets tab, you can specify when this project builder is executed and which targets. By
default, the default target is set to run After a "Clean" and Manual Build. You can specify other
targets and other triggers. Running your project builder during auto builds is possible, though not
recommended because of performance concerns.

Prepared by Feyisa K. Page 4


SE Tools and Practices lab 5: Automating Build
process with ANT

Build File with Compile and Jar


<?xml version="1.0"?>
<project name="myproject" default="MainBuild" basedir=".">
<property name="bin" location="bin"/>
<property name="src" location="src"/>
<property name="jardir" location="${bin/lib}"/>
<property name="jarfile" location="${jardir}/myproject.jar"/>

<target name="MainBuild" depends="Initialize,Compile,Jar">


<echo message="Ant at Work"/>
</target>

<target name="Initialize">
<delete dir="${bin}"/>
<delete dir="${jardir}"/> <mkdir dir="${bin}"/>
<mkdir dir="${jardir}"/>
</target>
<target name="Compile" depends="Initialize">
<javac srcdir="${src}"
destdir="${bin}">

Prepared by Feyisa K. Page 5


SE Tools and Practices lab 5: Automating Build
process with ANT

</javac>
</target>
<target name="Jar" depends="Initialize,Compile">
<jar destfile="${jarfile}" basedir="${bin}"/>
</target>
</project>

Prepared by Feyisa K. Page 6

You might also like