Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing registered task in kotlin build file

I need to convert a gradle build script, which is written in Groovy, into Kotlin. The problem is, that in the Groovy build file in one task another task, which was defined before, is executed. However, it appears that in Kotlin there is no support for that, at least I could not find any in the API.

I have studied the API and searched for similar problems, but I could not find anything useful.

The Groovy code looks like this:

task doSomething () {
   ...
}
task anotherTask () {
   ...
   doSomething.execute()
   ...
}

How would this call be translated into Kotlin?

doSomething.execute()
like image 962
Joey Avatar asked Mar 15 '26 14:03

Joey


1 Answers

You should never call execute() on a task. Instead set the inputs, outputs and dependencies correctly, and Gradle will call the task for you if it needs to.

something like:

tasks {
    val doSomething by registering {
        doLast {
            println("running doSomething")
        }
    }

    val anotherTask by registering {
        dependsOn(doSomething)
        doLast {
             println("Running anotherTask")
        } 
    }
}

Then:

$ gradle anotherTask

> Task :doSomething
running doSomething

> Task :anotherTask
Running anotherTask

BUILD SUCCESSFUL in 746ms
like image 80
tim_yates Avatar answered Mar 17 '26 04:03

tim_yates