Working with Subprojects

Good software design sees a large application broken down into smaller modules that exhibit the characteristics of loose coupling and high internal cohesion. A build script is itself a software artifact, so the same rules apply when designing a build process, and we should look to break large builds down into smaller, self-contained units known as subprojects.

Ant provides a number of options for breaking down large build files into smaller, more manageable modules. One approach is to have a central, controlling build file responsible for delegating build tasks to each of the subprojects. Ant allows targets to be invoked between build files using the <ant> task.

The following extract depicts the use of the <ant> task to kick off a target in a separate build.xml file.

 <ant antfile="build.xml"      dir="${subproject.dir}"      target="package"      inheritAll="no">   <property name="package.dir"           value="${wls.url}"/> </ant> 

Attributes of the <ant> task specify the name of the build file, its location, the name of the target to be invoked, and whether the subproject is to have access to all the properties of the caller. The default for this final attribute is to have the called build file take on all the properties of the caller. In the example shown, this behavior is disabled, and instead, specific properties are passed in using the nested <property> element.

Breaking up build files in this manner is not to everyone's liking. Some people prefer keeping all the build processing in one location. Ant can support this preference by allowing build files to pull in other build files rather than delegate out to them.

Ant can achieve the include-type functionality of Make in one of two ways. Ant files are first and foremost XML documents, so we can leverage the power of XML to pull in build snippets. Listing 12-4 demonstrates this approach.

Listing 12-4. Including Build File Snippets
 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE project [   <!ENTITY properties SYSTEM "file:./config/properties.xml">   <!ENTITY libraries SYSTEM "file:./config/libraries.xml"> ]> <project name="include-example" default="make" basedir=".">   &properties;   &libraries;   .   .   . </project> 

From the example shown in Listing 12-4, the contents of the two files properties.xml and libraries.xml are inserted directly in the build.xml file at the location marked with &properties and &libraries respectively.

Ant 1.6 provides a second method for pulling in external build files. The <import> task, which pulls in an entire build file, has the advantage over the previous example in that it enables build files to inherit from other build files. Thus, we can use this behavior to override targets in imported build files if required.

    Rapid J2EE Development. An Adaptive Foundation for Enterprise Applications
    Rapid J2EEв„ў Development: An Adaptive Foundation for Enterprise Applications
    ISBN: 0131472208
    EAN: 2147483647
    Year: 2005
    Pages: 159
    Authors: Alan Monnox © 2008-2017.
    If you may any questions please contact us: