JaVa
   

An AntHill HelloWorld

With the AntHill example project experience under our belt, let's work on a HelloWorld-type example to see how to use AntHill within the scope of a new project. We'll start the process by creating a new software project based on the familiar HelloWorld code.

Creating the Project

For this project, we are are going to assume that you have acccess to AntHill as well as a working CVS repository. Within your home directory, create a new directory called helloworld and a subdirectory called src, and within the subdirectory, create a new file called HelloWorld.java and type in the following code:

import java.io.*;
public class HelloWorld {
 String myHello;
 public void buildString(String s) {
 myHello = s;
 }
 public String getString() {
 return myHello;
 }
 public static void main (String [] args) {
 HelloWorld hw = new HelloWorld();
 hw.buildString("Fancy HelloWorld");
 System.out.println(hw.getString());
 }
}


Next we need to create a simple build.XML Ant script to handle the compiling of our HelloWorld class. The following listing shows the simple Ant script that we need to put in the directory with our Java code.

<?xml version="1.0"?>
<project default="all" basedir=".">
 <target >
 <mkdir dir="dist"/>
 <mkdir dir="dist/classes"/>
 <mkdir dir="dist/lib"/>
 </target>
 <target name="build-src">
 <javac srcdir="src"
 destdir="dist/classes"
 classpath="${build-classpath}"
 debug="on"
 deprecation="off"/>
 </target>
 <target depends="init,build-src">
 </target>
 <target depends="build">
 <jar jarfile="dist/lib/HelloWorld.jar"
 basedir="dist/classes"/>
 </target>
 <target >
 <delete dir="dist"/>
 </target>

 <target >
 <antcall />
 <antcall />
 <antcall />
 <antcall />
 </target>
</project>


Impounding the Project

In order for our system to ultimately build fresh source code, we need to have our code within the configuration system. In this example, we will be using CVS as our repository of choice. Let's impound the code into CVS with a command like the following:

cvs -d ~/repository import -m "" hello hello initial


The result of the import should be:

N hello/build.xml cvs import: Importing /home/node2/repository/hello/src N hello/src/HelloWorld.java No conflicts created by this import


At this point we are ready to test whether our code and Ant build script are set up correctly before working with AntHill to automate the process.

Doing a Test Build Using Ant

Before setting up a project within AntHill it is important to make sure that our Ant build.xml script and environment are set up properly to ensure that the code will build and that we have an issue only with the code. The following shows an example of what we should see when we execute Ant on our build script and the HelloWorld source code:

[node2@node2 helloworld]$ ant Buildfile: build.xml clean:
 [delete] Deleting directory /home/node2/helloworld/dist init:
 [mkdir] Created dir: /home/node2/helloworld/dist
 [mkdir] Created dir: /home/node2/helloworld/dist/classes
 [mkdir] Created dir: /home/node2/helloworld/dist/lib build-src:
 [javac] Compiling 1 source file to /home/node2/helloworld/dist/classes build:
jar:
 [jar] Building jar: /home/node2/helloworld/dist/lib/HelloWorld.jar all:
BUILD SUCCESSFUL Total time: 16 seconds



Of course, the magic words from this build are BUILD SUCCESSFUL. If you see these words, we can move to the AntHill project.

Creating an AntHill Project

Now that we know that our build process is successful, we want to move closer to a continuous integration system by allowing AntHill to automatically handle all of the tasks necessary for a successful build. Remember that a successful build is determined by the project itself. If you need to accomplish Javadocs, PDF output, testing, and other processes, just include them in your project build.xml script. For this example, we've removed all of the projects and dependencies from the AntHill Administration page by clicking the Delete links. Now let's create a new AntHill project. Click the Create New Project link from the Administration page. You will receive a page like this:

Java Click To expand

The options available for a new project are the following:

For our HelloWorld project, we filled the New Project options with the following values:

Once we have entered all of the fields, the new project will appear on the AntHill Administration page and we will be ready for a build.

Doing a Build

After the new project has been created, you will have an option to Edit, Build, or Delete it. Let's manually build the project to be sure that our project properties are accurate. To build the project, just click the Build link. Be sure to enable the Force Build checkbox when prompted. If you have filled out everything correctly, the build will be successful and you will see a green field; otherwise you will see red. If the field is red, click the name of the project and view the buildLog to see what the problem was during the build. In most cases it will be an issue with the repository settings. This task has shown that we can use the AntHill system to accomplish a forced build just like we would have done by executing Ant within the directory of our project. Looking in the logs, we find that the build was successful and it was provided the appropriate label 1.0.1 based on the version of 1.0.0 that we supplied in the version file. If you take a close look at the output produced when you click the HelloWorld project name, you will see that there is no /dist directory. If you look back at our Ant build script, you will see that a /dist directory is supposed to be created. Let's take a moment to understand what we are looking at when we click on a project's name from the Administration page. Open a terminal window and browse to where you installed AntHill. The path might be something like /usr/local/anthill. Within the installation directory there will be another directory called publishDir. This is where AntHill puts all of the files produced from the build. Recall that during the setup of the HelloWorld project and the discussion of the deployDir property, this property will be passed to the Ant build script with the location of the directory where the project will be shared by AntHill. In our case, the property will have the value /usr/local/anthill/publishDir/HelloWorld. It is within this directory that we want to put our .jar file. Clearly the build.xml script we developed earlier won't do. So consider the following script:

<?xml version="1.0"?>
<project default="all" basedir=".">
 <property location="dist"/>
 <property name="dist.dir" location="${deployDir}"/>
 <target >
 <mkdir dir="classes"/>
 <javac srcdir="src"
 destdir="classes"
 classpath="${build-classpath}"
 debug="on"
 deprecation="off"/>
 </target>
 <target depends="build">
 <mkdir dir="${dist.dir}/lib"/>
 <jar jarfile="${dist.dir}/lib/HelloWorld.jar"
 basedir="classes"/>
 </target>
 <target >
 <delete dir="classes"/>
 </target>
 <target >
 <antcall target="clean"/>
 <antcall target="build"/>
 <antcall target="jar"/>
 </target>
</project>


Notice at the top of the script that we have two new property tags:

 <property location="dist"/>
 <property name="dist.dir" location="${deployDir}"/>


AntHill will pass in the value for the deployDir, and we will use that value in the jar <target> element so that we will have a specific place to put the results of our project. Now we need to set up a specific schedule for performing nightly scheduled builds.

Scheduling a Nightly Build

When we created a new project, we chose a schedule to use called default. If you take a look at the Administration page, you will see an entry for a schedule called default. Click the Edit link next to the schedule. Each schedule will have a specific name, the total number of minutes between builds, and the time when the build will actually start. The default build schedule is quite aggressive at a build every 30 minutes. All of the projects that are currently using a specific schedule will be shown at the bottom of the Schedule page.

If you click the Back button, you will also see that there is a link to create a new schedule. When you click the link, you will be required to enter a schedule name, interval, and start time. To use the new schedule, edit a specific project and select the newly entered schedule; then click Update.

Adding More Builds

If you have a situation where there is a need to have builds at a specific time of the day, you will need to have multiple schedules. For example, let's say we would like to have a build at 11:00 A.M. and 3:00 P.M. each day but also a nightly build at 2:00 A.M. Since the times aren't separated by equal intervals, we will need to create three different schedule tasks as well as three different projects. The reason is that the projects are able to be associated with only a single schedule. So we might ultimately have the following projects and their associated schedules:

These three builds will have all of the necessary support for our project by allowing the developers to submit code throughout the day and be sure that they don't break the build.


JaVa
   
Comments