In this Ant tutorial installment, we’ll take a closer look at setting properties. The combination of properties and conditional execution allows more robust build files that adapt to the computing environment. Start with this basic build.xml file.
1 | <project default="set"> |
And then execute ant.
1 | $ ant Buildfile: build.xml set: [echo] the status is true BUILD SUCCESSFUL Total time: 2 seconds |
This demonstrates two characteristics of the property task:
- Properties are immutable. Once they are set, they stay set (with some inevitable exceptions). If properties were mutable, then the property assignment task
would result in the message the status is false. - Property tasks that are direct children of the project are executed before any target task is executed. In this case, the property assignment task
is a direct child of the project and is executed before the target set.
Now let’s see how we can use the available task to set a property value. The available task can be used to test for the existence of a file or directory, test for the existence of a class in a classpath, and test for the existence of a system resource. We’ll just work with the first option, please see the fine manual for the other choices. This build file tests for the presence of foo.
1 | <project default="all"> |
Execute this build.xml file twice. Once with an empty directory and once after creating file foo with the touch command.
1 | $ ls build.xml $ ant Buildfile: build.xml init: exists: all: BUILD SUCCESSFUL Total time: 3 seconds $ touch foo $ ant Buildfile: build.xml init: exists: [echo] foo exists all: BUILD SUCCESSFUL Total time: 3 seconds |
In the previous example, we only checked for the existence of foo. In this example, we will also identify whether it is a directory or a file. The available task’s type=”dir” attribute specifically tests for a directory and is used to set property isaDir. We could use the type=”file” attribute to set property isaFile but we’ll use the condition task instead. The condition task is the generalized method for setting parameters. Please see the manual for a complete list of the supported conditions.
1 | <project default="all"> |
Execute this build file three times. Once with an empty directory, once after creating directory foo, and once after removing directory and creating file foo.
1 | $ ls build.xml $ ant Buildfile: build.xml init: exists: isFile: isDir: all: BUILD SUCCESSFUL Total time: 3 seconds Ant/Setting 41 $ mkdir foo mkdir foo Ant/Setting 42 $ ant ant Buildfile: build.xml init: exists: [echo] foo exists isFile: isDir: [echo] foo is is a directory all: BUILD SUCCESSFUL Total time: 3 seconds $ rmdir foo $ touch foo $ ant Buildfile: build.xml init: exists: [echo] foo exists isFile: [echo] foo is is a file isDir: all: BUILD SUCCESSFUL Total time: 3 seconds |
Disclaimer: I don’t claim to be an expert on ant. Please send comments and corrections.