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 | import java.util.Properties; |
And here is the simple Ant build.xml that compiles and executes that class.
1 | <project default="run"> |
With the following result:
1 | $ 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 | <target name="run" depends="compile"> |
And we get the expected output on execution.
1 | $ 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 | <project default="run"> |
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 | <project default="run"> |
Which lets us control the property from the command line:
1 | $ 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.