![](/img/trans.png)
[英]SoapUI and Groovy: How to call a compiled jar file as if from the command line?
[英]How to pass an array arg to a Groovy JAR from command line?
我的Groovy應用程序具有以下代碼:
def doSomething() {
def props = [
"fizz",
"buzz"
]
// Do something with 'props'...
}
請注意 ,隨着時間的推移, props
可能會長得很長(10多個元素)。
我現在想要從命令行的開關讀取props
(因此,作為系統屬性)。 所以我想我的代碼是這樣的 :
def doSomething() {
def props = System.getProperty("props")
// Do something with 'props'...
}
所以我可以通過以下方式運行應用程序:
java -Dprops=??? -jar myapp.jar
但現在確定如何從命令行指定一個字符串數組。 有任何想法嗎?
似乎沒有內置支持,所以我最終得到:
System.getProperty("props").split(";")
然后從命令行:
java -Dprops="fizz;buzz;etc" -jar myapp.jar
正如你所說,沒有內置的支持,但你可以試試這個:
ArrayList props = System.getProperty("props").replaceAll("\\[","").replaceAll("]","").split(",")
並稱之為:
java -Dprops="['fizz','buzz']" -jar myapp.jar
您是否考慮過使用屬性文件並使用ConfigSlurper等
File configFile = new File(configDir, 'config.groovy')
ConfigObject config = new ConfigSlurper().parse(configFile.toURI().toURL())
def props = config.props
config.groovy的樣子
props = ["fizz","buzz","etc"]
或使用CliBuilder傳遞命令行參數
例如
CliBuilder cli = new CliBuilder(usage: "blah:")
cli.p(argName: 'prop', longOpt: 'prop', args: Option.UNLIMITED_VALUES, required: true, valueSeparator: ',', '')
def options = cli.parse(args)
def props = options.ps // ps gives you all the -p args
然后
java -jar myapp.jar -p fizz,buzz,etc
or alternate allowed syntax
java -jar myapp.jar -p fizz -p buzz -p etc
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.