Gradle 任务 - 将参数传递给 Java 应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27604283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Gradle task - pass arguments to Java application
提问by RecuencoJones
I have a Java application that runs with a custom gradle task and the application requires some arguments upon being invoked. These are:
我有一个使用自定义 gradle 任务运行的 Java 应用程序,并且该应用程序在调用时需要一些参数。这些是:
programName ( string | -f filename | -d key | -h)
Options:
string Message to be used.
-d key Use default messages, key must be s[hort], m[edium] or l[ong].
-f filename Use specified file as input.
-h Help dialog.
Gradle task looks like:
Gradle 任务如下所示:
task run (type: JavaExec){
description = "Secure algorythm testing"
main = 'main.Test'
classpath = sourceSets.main.runtimeClasspath
}
I've tried running gradle run -h
and it does not work.
我试过跑步gradle run -h
,但它不起作用。
采纳答案by AMing
Since Gradle 4.9, the command line arguments can be passed with --args. For example, if you want to launch the application with command line arguments foo --bar
, you can use
从 Gradle 4.9 开始,命令行参数可以通过 --args 传递。例如,如果您想使用命令行参数启动应用程序foo --bar
,您可以使用
gradle run --args='foo --bar'
gradle 运行 --args='foo --bar'
回答by cjstehno
回答by Francisco J. Lopez-Pellicer
You can find the solution in Problems passing system properties and parameters when running Java class via Gradle. Both involve the use of the args
property
您可以在通过 Gradle 运行 Java 类时传递系统属性和参数的问题中找到解决方案。两者都涉及args
财产的使用
Also you should read the difference between passing with -D
or with -P
that is explained in the Gradle documentation
你也应该阅读与传递的区别-D
或与-P
该解释的摇篮文档中
回答by xlm
Include the following in your build.gradle
:
在您的build.gradle
:
plugins {
// Implicitly applies Java plugin
id: 'application'
}
application {
// URI of your main class/application's entry point (required)
mainClassName = 'org.gradle.sample.Main'
}
Then to run: gradle run --args='arg1 arg2'
然后运行: gradle run --args='arg1 arg2'
Pre-Gradle 4.9
预毕业 4.9
Include the following in your build.gradle
:
在您的build.gradle
:
run {
if (project.hasProperty("appArgs")) {
args Eval.me(appArgs)
}
}
Then to run: gradle run -PappArgs="['arg1', 'args2']"
然后运行: gradle run -PappArgs="['arg1', 'args2']"
回答by RecuencoJones
Sorry for answering so late.
抱歉这么晚才回复。
I figured an answer alike to @xlm 's:
我想出了一个与@xlm 类似的答案:
task run (type: JavaExec, dependsOn: classes){
if(project.hasProperty('myargs')){
args(myargs.split(','))
}
description = "Secure algorythm testing"
main = "main.Test"
classpath = sourceSets.main.runtimeClasspath
}
And invoke like:
并像这样调用:
gradle run -Pmyargs=-d,s
回答by Claudio Fahey
If you want to use the same set of arguments all the time, the following is all you need.
如果您想一直使用相同的参数集,那么您只需要以下内容。
run {
args = ["--myarg1", "--myarg2"]
}
回答by Boaz Nahum
Of course the answers above all do the job, but still i would like to use something like
当然,以上所有的答案都可以完成工作,但我仍然想使用类似的东西
gradle run path1 path2
well this can't be done, but what if we can:
好吧,这是不可能的,但如果我们可以:
gralde run --- path1 path2
If you think it is more elegant, then you can do it, the trick is to process the command line and modify it before gradle does, this can be done by using init scripts
如果你认为它更优雅,那么你可以这样做,诀窍是在gradle之前处理命令行并修改它,这可以通过使用init脚本来完成
The init script below:
初始化脚本如下:
- Process the command line and remove --- and all other arguments following '---'
- Add property 'appArgs' to gradle.ext
- 处理命令行并删除 --- 以及“---”后面的所有其他参数
- 将属性“appArgs”添加到 gradle.ext
So in your run task (or JavaExec, Exec) you can:
因此,在您的运行任务(或 JavaExec、Exec)中,您可以:
if (project.gradle.hasProperty("appArgs")) {
List<String> appArgs = project.gradle.appArgs;
args appArgs
}
The init script is:
初始化脚本是:
import org.gradle.api.invocation.Gradle
Gradle aGradle = gradle
StartParameter startParameter = aGradle.startParameter
List tasks = startParameter.getTaskRequests();
List<String> appArgs = new ArrayList<>()
tasks.forEach {
List<String> args = it.getArgs();
Iterator<String> argsI = args.iterator();
while (argsI.hasNext()) {
String arg = argsI.next();
// remove '---' and all that follow
if (arg == "---") {
argsI.remove();
while (argsI.hasNext()) {
arg = argsI.next();
// and add it to appArgs
appArgs.add(arg);
argsI.remove();
}
}
}
}
aGradle.ext.appArgs = appArgs
Limitations:
限制:
- I was forced to use '---' and not '--'
- You have to add some global init script
- 我被迫使用“---”而不是“--”
- 您必须添加一些全局初始化脚本
If you don't like global init script, you can specify it in command line
如果你不喜欢全局初始化脚本,你可以在命令行中指定它
gradle -I init.gradle run --- f:/temp/x.xml
Or better add an alias to your shell:
或者更好地为您的 shell 添加一个别名:
gradleapp run --- f:/temp/x.xml