簡體   English   中英

如何使用Gradle將參數傳遞給main方法?

[英]How to pass parameters to main method using Gradle?

我必須將兩個參數傳遞給我的main方法。 我的構建腳本是

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

如何輕松地將參數傳遞給我的main方法命令行?

task run(type: JavaExec) {
    main = "pkg.MainClass"
    classpath = sourceSets.main.runtimeClasspath
    args = ["arg1", "arg2"]
}

您應該使用Gradle 命令行文檔中列出的使用-P。

例如,以下內容將起作用:

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

您可以在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM