I'm trying to use Java system properties to take values from Command line as follows in my build.xml file -
<target name="endPoint depends="standard-release">
<java classname="callPath.sampleClass" classpath="${bp:run.classpath}" fork="true">
<sysproperty key="commandType" value="${commandType}"/>
<sysproperty key="commandName" value="${Name}"/>
<sysproperty key="valueType" value="${valueType}"/>
<sysproperty key="valueName" value="${valueName}"/>
</java>
</target>
In my main method for sampleClass I'm retrieving the values passed using system properties and storing them in corresponding variables as follows -
public final class sampleClass {
public static void main(String[] args) throws Exception {
final String commandType = System.getProperty("commandType");
final String commandName = System.getProperty("commandName");
final String valueType = System.getProperty("valueType");
final String valueName = System.getProperty("valueName");
....
.....
}
}
However, I want to default the values of commandType
& valueType
to TYPE0
when the user doesn't provide with any value from CLI.
But I can't do that as the default value would be commandType
& valueType
itself as mentioned in the build.xml file.
When user tries to provide a value they can use -DcommandType=
& -DvalueType=
need to store that in the variables.
But when the user doesn't provide with any value then the values of both the variables should be TYPE0
How can I achieve this?
By design, Ant only allows a property to be set once. Any attempt to set a property which already has a value is simply ignored.
You can use this to your advantage. Simply set each property to your desired default. If that property was already set, your attempt to set it to a default will be ignored:
<target name="endPoint depends="standard-release">
<!-- Ignored if commandType is already set. -->
<property name="commandType" value="TYPE0"/>
<!-- Ignored if valueType is already set. -->
<property name="valueType" value="TYPE0"/>
<java classname="callPath.sampleClass" classpath="${bp:run.classpath}" fork="true">
<sysproperty key="commandType" value="${commandType}"/>
<sysproperty key="commandName" value="${Name}"/>
<sysproperty key="valueType" value="${valueType}"/>
<sysproperty key="valueName" value="${valueName}"/>
</java>
</target>