Java Automating Compilation with Ant

Java Automating Compilation with Ant

Problem 

You get tired of typing javac and java commands. 

Solution 

Use the Ant program to direct your compilations. 

Explained

The intricacies of Makefiles have led to the development of a pure Java solution for automating the build process. Ant is free software; it is available in source form or ready-to-run from the Apache Foundation’s Jakarta Project web site, at http:// jakarta.apache.org/ant/. Like make, Ant uses a file or files—written in XML—listing what to do and, if necessary, how to do it. These rules are intended to be platformindependent, though you can of course write platform-specific recipes if necessary. 

To use Ant, you must create a 15 to 30 line file specifying various options. This file should be called build.xml; if you call it anything else, you’ll have to give a special command-line argument every time you run Ant. Example 1-1 shows the build script used to build the files in the starting directory. See Recipe 21.0 for a discussion of the XMLsyntax. For now, note that the tag.

Example 1-1. Ant example file (build.xml)

<project name="Java Cookbook Examples" default="compile" basedir=".">

 <!-- Set global properties for this build -->

 <property name="src" value="."/>

 <property name="build" value="build"/>

 <!-- Specify the compiler to use.



Using jikes is supported but requires rt.jar in classpath. --&gt;&nbsp;&nbsp;<property name="build.compiler" value="modern">
 
<target name="init">
 <!-- Create the time stamp -->
 <tstamp>
 <!-- Create the build directory structure used by compile -->
 <mkdir dir="${build}">
 </mkdir></tstamp></target>
 <!-- Specify what to compile. This builds everything -->
 <target depends="init" name="compile">
 <!-- Compile the java code from ${src} into ${build} -->
 <javac classpath="../darwinsys.jar" destdir="${build}" srcdir="${src}"> </javac></target></property>


When you run Ant, it produces a reasonable amount of notification as it goes:



$ ant compile
Buildfile: build.xml
Project base dir set to: /home/ian/javasrc/starting
Executing Target: init
Executing Target: compile
Compiling 19 source files to /home/ian/javasrc/starting/build
Performing a Modern Compile
Copying 22 support files to /home/ian/javasrc/starting/build
Completed in 8 seconds
$

0 comments:

Post a Comment