kotlingradlegradle-plugin

How to run gradle task written in Kotlin before compile?


I've created a simple plugin, it looks like that:

open class CodeGeneratorPlugin: Plugin<Project> {

    override fun apply(project: Project) {
        project.tasks.create("simpleCodeGenerator", CodeGeneratorTask::class.java).run {
            description = "Test"
            group = "CodeGeneratorTask"
            project.tasks.withType(KotlinCompile::class.java){ dependsOn(it) }
        }
    }
}

My intention was to create a task that is strongly dependand to compile kotlin task, but line project.tasks.withType(KotlinCompile::class.java){ dependsOn(it) } simply does not work. I'm running gradle clean build and It does not generate any code before build, compileKotlin is running instead.

How can I add dependency in task in plugin that will be automatically started when build is called? I want to add dependency to KotlinCompile task or build task in Kotlin class.


Solution

  • try this:

    open class CodeGeneratorPlugin: Plugin<Project> {
    
        override fun apply(project: Project) {
            val simpleCodeGenerator = project.tasks.create("simpleCodeGenerator", CodeGeneratorTask::class.java).apply {
                description = "Test"
                group = "CodeGeneratorTask"
            }
    
            project.tasks.withType(KotlinCompile::class.java).configureEach {
                dependsOn(simpleCodeGenerator)
            }
        }
    }