kotlingradlegradle-kotlin-dslgradle-task

How to run a command line command with Kotlin DSL in Gradle 6.1.1?


I am trying to run the code block below, after reading multiple posts on the topic and the Gradle manual. I run the below and get the following error: execCommand == null!

Any ideas on what I am doing wrong with the below code block?

open class BuildDataClassFromAvro : org.gradle.api.tasks.Exec() {

    @TaskAction
    fun build() {
        println("Building data classes.....")
        commandLine("date")
    }
}

tasks.register<BuildDataClassFromAvro>("buildFromAvro") {
    description = "Do stuff"
}

Solution

  • To define a Gradle task that runs a command-line using the Gradle Kotlin DSL do something like this in your build file:

    task<Exec>("buildFromAvro") {
        commandLine("echo", "test")
    }
    

    In the example above the commandLine will simply run echo, outputting the value test. So replace that with whatever you want to actually do.

    You can then run that with gradle buildFromAvro

    More info here: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html