如何使用系统属性为字符串设置默认值?

huangapple go评论65阅读模式
英文:

How to set a default value to a string using System Poperties?

问题

我试图在我的build.xml文件中使用Java系统属性从命令行获取值,如下所示:

<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>

在我的sampleClass的主方法中,我正在检索使用系统属性传递的值,并将它们存储在相应的变量中,如下所示:

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");
        // ...
        // ...
    }
}

然而,当用户没有从CLI提供任何值时,我想将commandTypevalueType的默认值设置为TYPE0

但我无法这样做,因为默认值将是build.xml文件中所述的commandTypevalueType本身。

当用户尝试提供一个值时,他们可以使用-DcommandType=-DvalueType=,需要将其存储在变量中。

但是当用户没有提供任何值时,这两个变量的值应该是TYPE0

我该如何实现这一点?

英文:

I'm trying to use Java system properties to take values from Command line as follows in my build.xml file -

&lt;target name=&quot;endPoint depends=&quot;standard-release&quot;&gt;
        &lt;java classname=&quot;callPath.sampleClass&quot; classpath=&quot;${bp:run.classpath}&quot; fork=&quot;true&quot;&gt;
            &lt;sysproperty key=&quot;commandType&quot; value=&quot;${commandType}&quot;/&gt;
            &lt;sysproperty key=&quot;commandName&quot; value=&quot;${Name}&quot;/&gt;
            &lt;sysproperty key=&quot;valueType&quot; value=&quot;${valueType}&quot;/&gt;
            &lt;sysproperty key=&quot;valueName&quot; value=&quot;${valueName}&quot;/&gt;
        &lt;/java&gt;
    &lt;/target&gt;

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(&quot;commandType&quot;);
        final String commandName = System.getProperty(&quot;commandName&quot;);
        final String valueType = System.getProperty(&quot;valueType&quot;);
        final String valueName = System.getProperty(&quot;valueName&quot;);
        ....
        .....
 }
}

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?

答案1

得分: 2

按设计,Ant 只允许属性被设置一次。任何试图设置已经有值的属性的尝试都会被简单地忽略。

你可以利用这一点。只需将每个属性设置为你期望的默认值。如果该属性已经被设置过,你尝试将它设置为默认值的操作会被忽略:

<target name="endPoint depends="standard-release">
    <!-- 如果 commandType 已经被设置,将被忽略。 -->
    <property name="commandType" value="TYPE0"/>

    <!-- 如果 valueType 已经被设置,将被忽略。 -->
    <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>
英文:

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:

&lt;target name=&quot;endPoint depends=&quot;standard-release&quot;&gt;
    &lt;!-- Ignored if commandType is already set. --&gt;
    &lt;property name=&quot;commandType&quot; value=&quot;TYPE0&quot;/&gt;

    &lt;!-- Ignored if valueType is already set. --&gt;
    &lt;property name=&quot;valueType&quot; value=&quot;TYPE0&quot;/&gt;

    &lt;java classname=&quot;callPath.sampleClass&quot; classpath=&quot;${bp:run.classpath}&quot; fork=&quot;true&quot;&gt;
        &lt;sysproperty key=&quot;commandType&quot; value=&quot;${commandType}&quot;/&gt;
        &lt;sysproperty key=&quot;commandName&quot; value=&quot;${Name}&quot;/&gt;
        &lt;sysproperty key=&quot;valueType&quot; value=&quot;${valueType}&quot;/&gt;
        &lt;sysproperty key=&quot;valueName&quot; value=&quot;${valueName}&quot;/&gt;
    &lt;/java&gt;
&lt;/target&gt;

huangapple
  • 本文由 发表于 2023年3月7日 01:27:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653976.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定