[英]How to pass parameters to main method using Gradle?
I have to pass two arguments to my main method. 我必须将两个参数传递给我的main方法。 My build script is
我的构建脚本是
// Apply the java plugin to add support for Java
apply plugin: 'java'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'maven central' for resolving your dependencies.
mavenCentral()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'com.example:example-core:1.7.6'
}
task main(type: JavaExec, dependsOn: classes) {
description = 'This task will start the main class of the example project'
group = 'Example'
main = 'com.example.core.Example'
classpath = sourceSets.main.runtimeClasspath
}
If I try: 如果我尝试:
gradlew main doc.json text.txt
Then an error occured. 然后发生错误。
org.gradle.execution.TaskSelectionException: Task 'doc.json' not found in root project
How can I pass arguments to my main method command line easily? 如何轻松地将参数传递给我的main方法命令行?
task run(type: JavaExec) {
main = "pkg.MainClass"
classpath = sourceSets.main.runtimeClasspath
args = ["arg1", "arg2"]
}
You should use use -P as listed in the Gradle command line documentation . 您应该使用Gradle 命令行文档中列出的使用-P。
For example, the following will work: 例如,以下内容将起作用:
gradlew main -Parg1=doc.json --project-prop arg2=text.txt
And you access them in your Gradle script like this: 您可以在Gradle脚本中访问它们,如下所示:
println "$arg1 $arg2"
task run1(type: JavaExec) {
main = "pkg.mainclass"
classpath = sourceSets.main.runtimeClasspath
args = ["$arg1","$arg2",...]
}
//I have named as run1 it can be any task name
While invoking the gradle script:
c:\> gradle run1 -Parg1="test123" -Parg2="sss"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.