Setting Java System Properties with Ant

In this installment, we’ll learn how to set a Java System Property with ant. Here is our Java class. It simply accesses the System Properties object and prints the value of the test.property key.

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Properties;
public class prop {
public static void main( String[] args )
{
String testProp = "test.property";
Properties sysProps = System.getProperties();
System.out.println( "Value of " + testProp + " is " +
sysProps.getProperty(testProp) );
}
}

And here is the simple Ant build.xml that compiles and executes that class.

1
2
3
4
5
6
7
8
9
10
11
12
13
<project default="run">
<target name="compile">
<javac srcdir="." />
</target>
<target name="run" depends="compile">
<java classname="prop"
fork="true">
</java>
</target>
</project>

With the following result:

1
2
3
4
5
6
7
8
9
10
11
$ ant run
Buildfile: build.xml
compile:
[javac] Compiling 1 source file
run:
[java] Value of test.property is null
BUILD SUCCESSFUL
Total time: 6 seconds

To specify a System Property we use the sysproperty attribute of the java task. We’ll change the run target to assign the value blue to the key test.property.

1
2
3
4
5
6
7
8
<target name="run" depends="compile">
<java classname="prop"
fork="true">
<sysproperty key="test.property"
value="blue"
/>
</java>
</target>

And we get the expected output on execution.

1
2
3
4
5
6
7
8
9
10
$ ant
Buildfile: build.xml
compile:
run:
[java] Value of test.property is blue
BUILD SUCCESSFUL
Total time: 3 seconds

So far, so good. Of course, if test.property is going to have a fixed value, then we might as well set it from within our application. If test.property will typically have a wide range of simple values, then it is probably best managed as a parameter with a default value that can be overridden from the command line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<project default="run">
<target name="compile">
<javac srcdir="." />
</target>
<property name="COLOR" value="red" />
<target name="run" depends="compile">
<java classname="prop"
fork="true">
<sysproperty key="test.property"
value="${COLOR}"
/>
</java>
</target>
</project>

And if the property has a small number of complex values, then it can be set based upon another parameter that can be set from the command line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<project default="run">
<target name="compile">
<javac srcdir="." />
</target>
<property name="COLOR" value="red" />
<target name="setup-run">
<condition property="COMPLEX-COLOR"
value="complex.property.when.red.">
<contains string="red" substring="${COLOR}" casesensitive="false" />
</condition>
<condition property="COMPLEX-COLOR"
value="complex.property.when.blue.">
<contains string="blue" substring="${COLOR}" casesensitive="false" />
</condition>
<condition property="COMPLEX-COLOR"
value="complex.property.when.unknown.">
<not>
<isset property="COMPLEX-COLOR" />
</not>
</condition>
</target>
<target name="run" depends="compile,setup-run">
<java classname="prop"
fork="true">
<sysproperty key="test.property"
value="${COMPLEX-COLOR}"
/>
</java>
</target>
</project>

Which lets us control the property from the command line:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$ ant
Buildfile: build.xml
compile:
setup-run:
run:
[java] Value of test.property is complex.property.when.red.
BUILD SUCCESSFUL
Total time: 3 seconds
$ ant -DCOLOR=BLue
Buildfile: build.xml
compile:
setup-run:
run:
[java] Value of test.property is complex.property.when.blue.
BUILD SUCCESSFUL
Total time: 3 seconds
$ ant -DCOLOR=purple
Buildfile: build.xml
compile:
setup-run:
run:
[java] Value of test.property is complex.property.when.unknown.
BUILD SUCCESSFUL
Total time: 3 seconds

Disclaimer: I don’t claim to be an expert on ant. Please send comments and corrections.