0

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?

VGR
  • 40,506
  • 4
  • 48
  • 63
jjl
  • 15
  • 2
  • Check if the parameter has been passed. If yes, store that value. If not, take it from properties. – m0skit0 Mar 06 '23 at 17:30
  • @m0skit0 How can we check if the parameter has been passed or not? Is there a way for that? Thank you – jjl Mar 06 '23 at 17:38
  • You already said they need to be passed as -DcommandType= and -DvalueType=. Check for that in the parameters array – m0skit0 Mar 06 '23 at 17:40

1 Answers1

2

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>
VGR
  • 40,506
  • 4
  • 48
  • 63