[英]Gradle task - pass arguments to Java application
I have a Java application that runs with a custom gradle task and the application requires some arguments upon being invoked.我有一个使用自定义 gradle 任务运行的 Java 应用程序,并且该应用程序在被调用时需要一些参数。 These are:
这些是:
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
但它不起作用。
Gradle 4.9+摇篮 4.9+
gradle run --args='arg1 arg2'
This assumes your build.gradle
is configured with the Application plugin .这假设您的
build.gradle
配置了Application plugin 。 Your build.gradle
should look similar to this:您的
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'
}
Pre-Gradle 4.9预毕业 4.9
Include the following in your build.gradle
:在
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']"
Since Gradle 4.9, the command line arguments can be passed with --args.从 Gradle 4.9 开始,命令行参数可以通过 --args 传递。 For example, if you want to launch the application with command line arguments
foo --bar
, you can use例如,如果您想使用命令行参数
foo --bar
启动应用程序,您可以使用
gradle run --args='foo --bar'
gradle 运行 --args='foo --bar'
If you want to use the same set of arguments all the time, the following is all you need.如果您想一直使用相同的参数集,那么您只需要以下内容。
run {
args = ["--myarg1", "--myarg2"]
}
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
You can find the solution in Problems passing system properties and parameters when running Java class via Gradle .您可以在通过 Gradle 运行 Java 类时传递系统属性和参数的问题中找到解决方案。 Both involve the use of the
args
property两者都涉及
args
属性的使用
Also you should read the difference between passing with -D
or with -P
that is explained in the Gradle documentation此外,您应该阅读Gradle 文档中解释的使用
-D
或使用-P
传递之间的区别
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:初始化脚本如下:
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:限制:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.