12

我必须将两个参数传递给我的主要方法。我的构建脚本是

// 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

}

如果我尝试:

gradlew main doc.json text.txt

然后发生了错误。

org.gradle.execution.TaskSelectionException: Task 'doc.json' not found in root project

如何轻松地将参数传递给我的主方法命令行?

4

3 回答 3

11
task run(type: JavaExec) {
    main = "pkg.MainClass"
    classpath = sourceSets.main.runtimeClasspath
    args = ["arg1", "arg2"]
}
于 2015-05-12T01:08:30.703 回答
6

您应该使用 Gradle命令行文档中列出的 -P 。

例如,以下将起作用:

gradlew main -Parg1=doc.json --project-prop arg2=text.txt

您可以像这样在 Gradle 脚本中访问它们:

println "$arg1 $arg2"
于 2014-03-15T12:25:35.363 回答
1
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"
于 2017-04-03T10:30:48.510 回答