1

I migrated from gradle groovy to gradle kotlin build script.

I used to have colorful output as suggested in stackoverflow answer

But as of gradle 6.x and kotlin dsl this does not seem to work anymore.

services... or project.serviceOf<>() methods do not exist (anymore/or in kotlin dsl) it seems.

Any idea on how to get colorful console output from a kotlin build.gradle.kts task?

Dirk Hoffmann
  • 1,444
  • 17
  • 35

1 Answers1

2

I don't know if it is possible to get the service registry in an ad-hoc task using the DSLs. But if you are OK with implementing your tasks as concrete classes (which can still be done in the DSLs if you like), you can inject even internal Gradle services.

For a Kotlin class, it could look like this:

import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.internal.logging.text.StyledTextOutput.Style
import org.gradle.internal.logging.text.StyledTextOutputFactory
import javax.inject.Inject

open class MyTask @Inject constructor(private val styledTextOutputFactory: StyledTextOutputFactory) : DefaultTask() {
    @TaskAction
    fun doStuff() {
        val out = styledTextOutputFactory.create(javaClass.name)
        out.withStyle(Style.Info).println("colored text")
    }
}

tasks.register<MyTask>("myTask") // DSL
Bjørn Vester
  • 6,851
  • 1
  • 20
  • 20