Skip to content
Lindsey edited this page Feb 17, 2014 · 3 revisions

Introduction

If you're reading this is because it's important, right? As you might have guessed, the JBrick executable JAR is generated by an Apache Ant script.

How do I use Ant to build JBrick?

We're using Eclipse which comes with ant built in. Inside of Eclipse, look at the JBrick project. There should be a file in the main directory called "build.xml". Right click this and run it as an Ant Build. This will build a jar for you.

The build.xml file

The build.xml is the script that Ant uses to automate the tasks. We'll try to explain a little bit about it because the purpose is not to write an Ant tutorial. There's a manual for that.

This script file was generated using the Eclipse plugin for Ant (whatever that is). Let's start with this:

<project default="create_jar" name="Create Runnable Jar for Project jbrick with Eclipse's Jar-in-Jar Loader">

create_jar is the default target. Target are sets of tasks. Scrolling through the script, we find the create_jar target.

<target name="create_jar" depends="test">

This means that the set of tasks for creating the JAR file are defined in this target element. But wait, it also means that this target depends on another target, test. test depends on compile-test, which depends on compile, and on and on.

Each target has a name to identify it and some comments to make clear their intended purpose.

Compiling

<target name="compile" depends="prepare">
  <javac target="${javaVersion}" source="${javaVersion}" destdir="${workDir}" classpath="${libsDir}">
    <src path="${srcDir}" />
    <classpath>
      <fileset dir="${libsDir}">
        <include name="**/*.jar"/>
      </fileset>
    </classpath>
  </javac>
</target>

See the previous script snippet? Well, it defines the compile target which is responsible for compiling the Java source code. The words in curly brackets reference some properties defined in the script body. If you've ever used the command line compiler (javac) then you know what the snippet is about. An over-simplification of it would read like "Execute after the prepare target. Compile the Java source code located in the directory ${srcDir} and place the compiled code in ${workDir}. Don't forget to include the JAR files from ${libsDir} in your classpath."

Test

<target name="test" depends="compile-test">
  <junit printsummary="false" fork="false" haltonfailure="false">
    <classpath> 
      <pathelement path="${testDir}"/>
      <fileset dir="${libsDir}">
        <include name="**/*.jar" />
      </fileset>
    </classpath>
    <formatter type="brief" usefile="false" />
    <batchtest todir="${testDir}">
      <fileset dir="${testSrcDir}">
        <include name="**/*Test*.java"/>
      </fileset>
    </batchtest>
  </junit>
</target>

Alright, so you're using JUnit and have to tell Ant to use it in order to test the software. Well, we have to compile the unit tests before executing them, that's the purpose of the compile-test target (similar to the compile target).

Then you can see how in the second line we're using a task. Using and configuring this task is what makes it possible to run the unit tests within Ant. Further reading can be found on their website about JUnit and Ant.