How to pass command line argument to Gradle Kotlin DSL

时间秒杀一切 提交于 2020-06-12 04:44:00

问题


Here's an example from Groovy that represents exactly what I would like to achieve:

Command line:

./gradlew jib -PmyArg=hello

build.gradle.kts

task myTask {
    doFirst {
       println myArg
       ... do what you want
    }
}

Source of this example is here - option 3.

How can I read pass and read myArg value in Kotlin DSL ?


回答1:


After some time found an answer:

build.gradle.kts

val myArg: String by project // Command line argument is always a part of project

task("myTask") {
    doFirst {
        if (project.hasProperty("myArg")) {
            println(myArg)
        }
    }
}

Command line:

gradle myTask -PmyArg=foo

Output:

$ gradle myTask -PmyArg=foo

> Task :myTask
foo

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

Related links:

  • How to pass arguments from command line to gradle
  • How to pass parameters or arguments into a gradle task
  • Gradle task check if property is defined


来源:https://stackoverflow.com/questions/56535720/how-to-pass-command-line-argument-to-gradle-kotlin-dsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!