Step 6: Build previous   contents   next
next 

As we have now written the outline of the production classes, we should compile them to fix syntax errors. There are several ways to do this, e.g. you can compile by hand with javac, or you can use tools like make. Doing it by hand is only a quickshot method and make is not really platform independent. A replacement for make is Ant from the Jakarta project. It is implemented in Java and uses XML files to control the build process. There is even support for make and Ant in Together 5.5 and later. Ant can be accessed by the Together building block Ant Runner, allowing the creation and execution of Ant build targets.
During the tutorial, you should either use the Ant Runner module or the standalone build file presented for developers without Together 5.5. Both ways will be explained in detail.

I will now walk you through the steps required to get a working XML file for building a small project.


Together 5.5

In version 5.5, Ant 1.2 and Ant Runner became part of Together. You can create a basic build file by selecting 'Generate buildfile' from the 'Ant Runner' submenu in the 'Tools' menu. Before compilation you should change the output path to the classes directory of the tutorial project:


(click on the image for a fullsize screenshot)

Now modify the build.xml file according to your needs and choose 'Run Ant' from the 'Ant Runner' submenu in the 'Tools' menu:


(click on the image for a fullsize screenshot)

The output of Ant is shown in the message pane and if there are any error, you can just click on them for analysis. Per default, the compiled classes can be found in the out path of your Together installation.
Ant Runner is able to parse existing build files for targets, so you can use your selfmade build files with Together...long live XML. If you choose a different output directory in your ant build file like in the following example, you have to change the output path of the Builder in your Together project too. Otherwise the compiled tests will not be found later.


Ant hands-on

The following sections show you how to create and extend your own Ant build file. If you are content with the 'Ant Runner'-generated build file or if you are already fluent with the Ant syntax, you can skip this section and compile and bugfix the classes now.

  1. Install Ant
    Since XPTest 2.0 and JUnitX 5.0, Ant is no longer part of both distributions. Please download Ant separately from the Jakarta site and install it into tools/ant of your JUnitX or XPTest distribution or the tutorial. Use tools/ant/bin/ant (Unix) and tools/ant/bin/ant.bat (Windows) later to start Ant.

    On Windows 95/98, the Ant launch script will have problems with the limited environment space if the variable ANT_HOME is a long filename. This is due to limitations in the OS's handling of the "for" batch-file statement. It is recommended install Ant in a short path, such as D:\Ant. The tutorial assumes that you work on Windows NT/2000 or Unix.

  2. Install JUnit and JUnitX
    You can optionally use the latest version of JUnit (e.g. 3.7.6) instead of the version bundled with Together 5.5 or later. To decouple this tutorial from a specific Together version, please download JUnit from junit.org or copy the junit.jar archive from your Together installation to the tutorial project path. Please copy the JUnitX jar archive also.

  3. Create an initial build file
    The default file for controlling Ant is called build.xml. Create such a file in the main directory of the xptest-tutorial project with the following content:

    <project name="URLParser" default="main" basedir=".">
    
      <property name ="prj.dir"
                value="/shared/extreme-java/xptest-tutorial" />
      <property name ="junit.jar"
                value="/shared/extreme-java/xptest-tutorial/junit-3.7.jar" /> 
      <property name ="junitx.jar"
                value="/shared/extreme-java/xptest-tutorial/junitx-5.0.jar" /> 
    
    </project>
    

    Even if you see XML for the first time, it is not hard to understand. The tags between the opening tag <project> and the closing tag </project> define our project. The attribute name="URLParser" defines the project name. basedir="." defines the current directory where Ant is started as the base directory for Ant.
    The three properties define the project directory as well as the full paths for JUnit and JUnitX. These are both elements without a closing tag, so they end with '/>'. Change the property values to match your directories.

  4. Define the main target
    Each project is made up of targets. The default target to be invoked by Ant is defined above by the attribute default="main". Now its time to define that target between the properties and the </project> tag:

    <target name="main" depends="clean">
    
      <mkdir dir     ="${prj.dir}/classes" />
        
      <javac srcdir  ="${prj.dir}/src/"
             destdir ="${prj.dir}/classes"
             includes="**/*.java">
        <classpath>
          <pathelement location="${junit.jar}"  /> 
          <pathelement location="${junitx.jar}" /> 
        </classpath>
      </javac>
    
    </target>
          

    First the path classes is generated based on the value of the property prj.dir. Afterwards the compiler is run. It gets the path (srcdir="${prj.dir}/src/") where the sources are located. Here the value of the property prj.dir is referenced by ${prj.dir}. Afterwards the output path (destdir="${prj.dir}/classes") and the files to parse (includes="**/*.java") are defined. '**' stand for any number of nested paths.
    Since the test cases will need JUnit and JUnitX later, we added them in the classpath tags.

  5. Create the clean target
    As the attribute depends="clean" of the project tag shows, the compilation is started after a clean up has been done. Here is the code for cleanup:

    <target name="clean">
      <deltree dir="${prj.dir}/classes" /> 
    </target>
For the complete build file see Appendix B or one of the project snapshots.

You can now use the 'Ant Runner' building block of Together to execute the 'main' target of your build.xml file. Or you can run Ant by hand as described below.

If you are using Windows 95 or 98, you need to reconfigure your dos box to provide 4096 bytes for environment variables. You can open the appropriate configuration pane by starting a dos box and clicking on the 'MS DOS' icon in the left upper corner:


(click on the image for a fullsize screenshot)

Now you can compile the sources opening a shell or MS-DOS box, changing to the project path (e.g. /shared/extreme-java/xptest-tutorial) and starting Ant (e.g. /shared/extreme-java/xptest-tutorial/tools/ant/bin/ant.bat). The output looks like this on my Win 98 box:


(click on the image for a fullsize screenshot)

The class files are written to the classes directory created by Ant:


(click on the image for a fullsize screenshot)

I had luck and found no errors, hope you found none too ,) The next step is to define tests.

  previous   contents   next
next 

© 2001 A. Heilwagen