In this Ant tutorial installment, we will use ant Properties to specify the work directory structure that will contain our .class and .jar files. The work directory structure tends to be a personal preference, and properties will allow us to centralize the structure definition so that any change will be relatively pain-free. We specify the directories with a location attribute, thereby binding the property to a file system location. This allows us to pass the property to another ant process without any local directory side effects.
This build file features:
- init: to build the work directory structure.
- clean-init: to remove the work directory structure.
- all: the build roll-up target.
- clean: to clean roll-up target. Note that my target naming convention is to pair a target with a clean-target. I reserve the all and the clean targets as roll-up targets for all build and clean targets respectively.
- obj-dir: the root directory for our .class files.
- lib-dir: the root directory for our .jar files.
We can now execute the init target to create the work directories, and the clean-init target to remove the work directories.
Now, let’s add targets to compile, jar and execute a java class. We’ll just copy over the hello.java class from our previous installment and place it in the src directory. The compile and jar targets from before remain largely the same; but we add the destdir attribute to the javac task to specify where the .class files are to be stored and we can remove the includes attribute from the jar task. Our new build.xml looks like this:
And now we can execute our class and clean up our work directories.
$ ant run
Disclaimer: I don’t claim to be an expert on ant. Please send comments and corrections.