Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do run a command line command with Gradle 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"
}
like image 201
Dan Largo Avatar asked Feb 04 '20 15:02

Dan Largo


People also ask

How do I pass a Gradle argument in command line?

We first read the arguments from a project property. Since this contains all the arguments as one string, we then use the split method to obtain an array of arguments. Next, we pass this array to the args property of our JavaExec task.

How do I run a Gradle test from command line?

Use the command ./gradlew test to run all tests.


1 Answers

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

like image 200
Yoni Gibbs Avatar answered Sep 18 '22 16:09

Yoni Gibbs