Practical
Embedded Java

Ant Tips


A practical engineering approach to using embedded Java in real-world applications.


...



Here are some techniques which we've found useful.


About Eclipse and Ant - using Ant Internal and External to Eclipse

To use Ant from the command line, and external to Eclipse, you will want to install Ant in its own folder, and set up the enviroment variable ANT_HOME. Eclipse also includes Ant as a standard plugin. So when you install Eclipse you will have another Ant installation buried within the Eclipse plugins folders. When you run Ant within Eclipse it will use the Eclipse plugin Ant, and when you run Ant from the command line it will use the ANT_HOME files.

Confused yet?

If you want a "generic" build.xml to work both within Eclipse and external to Eclipse, from a command line, you just need to keep the fact that the versions of Ant which run within and without Eclipse will have different libraries and class paths.

It's possible to configure Eclipse to use your "external" Ant folders -- then you will always be using one Ant /bin, /lib, etc, and one consistent Ant classpath. When you add additional Ant tasks, you need only install them in the external Ant folders. To do this, change Window->Preferences->Ant->Runtime->Classpath "Ant Home" to point to your "external" Ant root folder.

But you will still have to configure each added task inside Eclipse - see the comments below about TiniAnt for an example.


Make Eclipse use your Ant build.xml for each "Auto-build" (new 2005 Nov 15)

You can tell Eclipse to Build Automatically whenever a project resource is saved with changes. Set this in the Eclipse main task bar Project->Build Automatically. Unfortunately for cross-compilation, this auto build is not useful since it doesn't do any of the actions in your Ant build.xml file.

But there's a way to make Eclipse do just that - use your Ant build file.

In Project->Properties->Builders you will see the default "Java Builder". Click New and select Ant Build. Give your builder a name, preferably without whitespace (See below for why). Then a series of tabbed dialogs opens - select the Main tab. Now you have a dialog box asking for the Buildfile and other options. Buildfile is the only critical one, this is typically the project's build.xml, but note that after you browse to is and select it, Eclipse refers to it in this way: ${workspace_loc:/backplane/build.xml}.

Now click the Targets tab, and change the Auto Build entry - it pulls up the targets in the build.xml file you specified in the Main tab. I like to set the Auto Build to my main compile task, and run my "init" task during a Clean.

That's it - now in the Console output you should see your Ant build tasks running every time you save a project resource.

Also note that Eclipse puts this configuration information into a new .externalToolBuilders folder, and creates a file called "{whatever_you_called_it}.launch which, perhaps oddly, is not an XML file but a list of key/value pairs. You will want to add this file to your version control system. It's text, so you could copy its settings into other projects (changing the folder paths of course). To make these settings portable to other developers, you must all check out the project with the same Eclipse project name. MyProject and My Project for example, will not be able to share the same Auto Build references.

One thing still unresolved is how to make this autobuild a system-wide setting instead of per-project. Given that Ant build files are not shared among projects, this may not be possible.


Use Eclipse and Ant with TiniAnt (new 2005 Nov 12)
To use TiniAnt with Ant, you need to
  1. get and install TiniAnt (see the Java tools list page
  2. read the TiniAnt docs
  3. copy TiniAnt.jar into your Ant lib folder
  4. refer to TiniAnt in your build.xml by adding an "external task" (one which Ant doesn't already include in its core) like this:
    <!-- Use the TINI task from TiniAnt.jar installed in the environment variable {ANT_HOME}\lib -->
    < taskdef name= "tini" classname="net.geeba.ant.Tini"/>
  5. Use the tini task in your Ant build.xml

To use TiniAnt within Eclipse/Ant, you need to tell Eclipse that such a task as "tini" is available and where to load the class which supports it. Eclipse includes Ant support, and the Eclipse Ant librarie files are in a folder such as C:\eclipse\plugins\org.apache.ant_1.6.5\lib. So to add a new task to Eclipse's Ant:

  1. copy TiniAnt.jar into your Eclipse Ant lib folder, which will be something like C:\eclipse\plugins\org.apache.ant_1.6.5\lib
  2. Tell Eclipse about the new "tini" task, in Window->Preferences->Ant->Runtime. Click on the Classpath tab, then click on "Add External JARs", then browse to the location of TiniAnt.jar and select it. Now click on the "Tasks" tab and then click "add Task". The new task will be called "tini", then for Location, select TiniAnt.jar in the pulldown list, expand it and select the Tini.class file. That's it.
  3. You've added the tini task to Eclipse/Ant, but if you want to be able to use the same build.xml either within Eclipse or from the command line, you should refer to TiniAnt in your build.xml by adding an "external task" (one which Ant doesn't already include in its core) like this:
    <!-- Use the TINI task from TiniAnt.jar installed in the environment variable {ANT_HOME}\lib -->
    < taskdef name= "tini" classname="net.geeba.ant.Tini"/>
  4. Use the tini task in your Ant build.xml

Getting Ant to create a JAR file

It took me an absurd amount of time figuring out this small snippet of Ant target code. Here's what I was trying to do, and what this code accomplishes:

  1. Create a JAR file with:
  2. store runnable classes in a package tree such as com.systronix.util so that they can be imported and run from within the JAR file. This means the Uart class file wants to be located in the JAR in the path com/systronix/util/Uart.class
  3. store source code in a package tree under a "src" folder. This means the Uart source file wants to be located in the JAR in the path src/com/systronix/util/Uart.java
  4. store javadocs - some of which are in the root folder "doc" and the rest of which are in the package tree under the "doc" folder
  5. store Amulet GUI files in an html folder (no package tree for those since they are not Java files)
  6. store build.xml and JemBuilder project files, which exist in the project's root folder.
  7. Create a way to do this which is easily re-used

The solution I arrived at uses multiple fileset parameters to select the right folders in the project, and Ant properties to tell Ant where the project is, and what to call the JAR file.

<!-- These are project-specific names and paths used by all the tasks.
Change these project values here, in one place, rather than sprinkling
hard-coded constants in each Ant task -->
<property name="PkgPath" value="com/systronix/amulet"/>
< property name="Project" value="Amulet"/>

< target name="jar" depends="init">

<jar
destfile="${Project}.jar"
>

<fileset dir="classes"
includes="${PkgPath}/*.class"
/>

<fileset dir="." includes="src/${PkgPath}/*" />
<fileset dir="." includes="doc/${PkgPath}/*" />
<fileset dir="." includes="doc/*" />
<fileset dir="." includes="*.xml, *.ajp" />

</jar>
< /target>

Note that the JAR target uses the Ant properties PkgPath and Project to determine where the files are, and what to call the resulting JAR file.


 
Systronix® 939 Edison St, Salt Lake City, Utah, USA 84111
Tel +1-801-534-1017, Fax +1-801-534-1019
contact us     Time Zone: MDT (UTC-6)
 

Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
Systronix is independent of Sun Microsystems, Inc.
TStik, JStik, JCX, JStamp, JSimm, JDroid, and JRealTime are trademarks of Systronix, Inc.
1-Wire, iButton and TINI are trademarks of Dallas Semiconductor
Simmstick is a trademark of Dontronics
LEGO® is a trademark of Lego A/S, Denmark