3.6. Building a Java Library

Warning

Note that abuild's Java support is considered alpha as of version 1.0. Version 1.1 of abuild may include a Java solution that is not backward compatible with the solution in version 1.0.

In our next example, we'll demonstrate how to build a simple Java library. Abuild has two different ways to build Java code: a property-driven method, and a build.xml-driven method. The property-driven method of building Java code is truer to abuild's philosophy of using declarative build files but has proven to be inadequate for some of the more complex Java build problems that arise in an enterprise environment. It is hoped that a future version of abuild will provide a more complete answer to the Java build problem, perhaps by integrating with a Java build engine other than Apache Ant. In the mean time, we will demonstrate the property-driven approach to doing Java builds with abuild using this library example. For an example of the build.xml-based approach, see Section 19.3, “Build.xml-driven Java Example”.

You will find the Java example in basic/java-library. The files here are analogous to those in our C++ library example. First, here is a Java implementation of our BasicLibrary class:

basic/java-library/src/java/com/example/basic/BasicLibrary.java

package com.example.basic;

public class BasicLibrary
{
    private int n;

    public BasicLibrary(int n)
    {
        this.n = n;
    }

    public void hello()
    {
        System.out.println("Hello.  This is BasicLibrary(" + n + ").");
    }
}

Next, look at Abuild.conf:

basic/java-library/Abuild.conf

this: java-library
platform-types: java
parent-dir: ..

This is essentially identical to our C++ library except that the platform-types key has the value java instead of the value native. This is always true for Java build items. Next, we'll look at the Abuild-ant.properties file:

basic/java-library/Abuild-ant.properties

abuild.jar-name = java-library.jar

Property-driven Java build items have this file instead of Abuild.mk. The only thing we have to set here is the property abuild.jar-name. For Java build items, we don't explicitly list the source files. Instead abuild automatically finds sources in the src/java directory. There are more properties that can be set, and there are other ways to get files into the JAR file. We provide detailed information about the directory structure for property-driven Java builds in Section 16.2, “Directory Structure For Java Builds”. Finally, look at the Abuild.interface file. This file provides information to other build items about what they should add to their classpaths in order to make use of the JAR file created by this build item:

basic/java-library/Abuild.interface

abuild.classpath = $(ABUILD_OUTPUT_DIR)/dist/java-library.jar

As with the C++ library, it is possible to build this item by running abuild from the basic/java-library directory. Notice that abuild puts the JAR file in the dist subdirectory of the abuild output directory.