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.
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.
<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:
$ 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
<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.
$ 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.
<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.
<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 let's us control the property from the command line:
$ 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
No comments:
Post a Comment