英文:
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提供任何值时,我想将commandType
和valueType
的默认值设置为TYPE0
。
但我无法这样做,因为默认值将是build.xml文件中所述的commandType
和valueType
本身。
当用户尝试提供一个值时,他们可以使用-DcommandType=
和-DvalueType=
,需要将其存储在变量中。
但是当用户没有提供任何值时,这两个变量的值应该是TYPE0
。
我该如何实现这一点?
英文:
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?
答案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:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论