Building a Naked Objects application with Ant

Once an application is written it must, like all Java programs, be compiled before it can be run. As you want to run your application within the framework there are dependencies that need to be satisfied to allow this. Naked Object promote two ways of doing this, using Ant and Maven. This section looks at how to use Ant to build a Naked Objects application, while the next section shows how to do the same thing with Maven.

Project organisation

How you organise your project and its dependencies is a matter of personal preference, but for this section we will assume the following directory structure, where nakedobjects-4.0 is the unzipped distribution and project is the directory containing the application you are working on.

nakedobjects-4.0/
    lib/
    resources/
    :
    :
project/
    src/
    xat/
    resources/
    config/
    build.xml
    :
    :

The source code for the domain objects is contained in the src directory and the tests are in xat. Resources to be made available via the class path (rather than loaded via the file system) should be added to the project/resources directory, while configuration files should be placed in config. The file build.xml should be copied across from the nakedobjects-4.0/resources directory.

Compilation

To build a Naked Objects application you need to have access to its needed libraries as well as your source code. The libraries are all contained in the lib directory within the distribution download. The following extract from a build.xml file

The complete file can be found in the resource directory

shows how you can use Ant to compile a Naked Objects application. The path element is used to link in all the Jar files that are provided by the distribution, while the compile target uses javac to compile your Java code found in the source directory. Make sure you change the lib.dir property to point to the lib directory within the binary download and the src.dir property to your source directory. The src directory will normally contain DOM classes, fixtures and application-framework integration code (such as repositories and services).

<property name="lib.dir" value="../nakedobjects-3.0/lib/" />
<property name="build.dir" value="./build" />
<property name="classes.dir" value="./build/classes" />
<property name="src.dir" value="./src/" />
<property name="xat.dir" value="./xat" />
<property name="compile.target" value="1.5" />
<property name="source.target" value="1.5" />


<fileset dir="${lib.dir}" id="libs.set">
  <include name="**/*.jar" />
</fileset>

<path id="libs.path">
  <fileset refid="libs.set" />
</path>

<target name="compile" description="Compile example">
  <mkdir dir="${build.dir}" />
  <mkdir dir="${classes.dir}" />

  <javac destdir="${classes.dir}" target="${compile.target}" source="${source.target}">
      <src path="${src.dir}" />
      <src path="${xat.dir}" />
      <classpath refid="libs.path" />
   </javac>
</target>

Running Ant with the compile target will compile your code, producing the following output indicating that the compilation was successful. The newly created build directory will contain the class files.

$ ant compile
Buildfile: build.xml

compile:
    [mkdir] Created dir: /home/rcm/workspace/project/build
    [javac] Compiling 17 source files to /home/rcm/workspace/project/build
    [javac] Note: Some input files use unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.

BUILD SUCCESSFUL
Total time: 2 seconds

Distribution

A fully runnable distribution and zip file can be created using the build file. Ensure the resource.dir property points the resource directory in the distribution.

<property name="resource.dir" value="../nakedobjects-4.0/resources" />

<target name="dist" depends="compile" description="Collects together files for distribution">
  <copy todir="${build.dir}/lib">
    <fileset refid="libs.set" />
  </copy>
  <copy todir="${build.dir}">
    <fileset dir=".">
      <include name="config/*" />
      <include name="images/*" />
    </fileset>
    <fileset dir="${resource.dir}">
      <include name="web/*" />
      <include name="run.*"/>
      <include name="lcp.bat"/>
    </fileset>
  </copy>
  <chmod file="${build.dir}/nakedobjects.sh" perm="ugo+x"/>

  <mkdir dir="${dist.dir}" />
  <zip destfile="dist/no-application.zip" >
    <zipfileset dir="${build.dir}" prefix="no-application" />
  </zip>
</target>

Running ant with the dist target will create the distribution, producing the following output.

$ ant dist
Buildfile: build.xml

compile:
dist:
     [copy] Copying 45 files to /home/rcm/workspace/project/build/lib
     [copy] Copying 7 files to /home/rcm/workspace/project/build
      [zip] Building zip: /home/rcm/workspace/project/dist/no-application.zip
BUILD SUCCESSFUL
Total time: 6 seconds

After this has completed the build directory will look like the following, and the dist directory will contain a zipped file of the same contents.

build/
   classes/
   config/
   images/
   lib/
   resources/
   web/
   lcp.bat
   nakedobjects.bat
   nakedobjects.sh