Christoph Henrici
Christoph Henrici

Reputation: 329

Gradle Kotlin Dsl Build for a Kotlin Gradle Plugin, which depends on a Groovy Class in the same Project

I have a Gradle plugin implemented with Kotlin, which is built with a gradle kotlin DSL build script. This works fine. The build script is as follows and is located in the buildSrc directory of project:

plugins {
    groovy
    `kotlin-dsl`
}

repositories {
    mavenLocal()
    mavenCentral()
    jcenter()

}


dependencies {
    testImplementation(gradleTestKit())
    implementation(kotlin("gradle-plugin"))
    implementation ("com.bmuschko:gradle-docker-plugin:6.1.3")
}

Now i want to call a existing Groovy Class in the same buildSrc Source Tree from the Kotlin Plugin code. This works fine in Intellij.

But when building with gradle i get a : unresolved reference Class for the Groovy Class. Looking at the build, i see that the compileKotlin task is executed first. When i uncomment the failing reference, i see that the groovyCompile produces the correct binaries.

So i tried this:

tasks.compileKotlin {
    dependsOn(tasks.compileGroovy)
}

Naturally that is not good enough, but i tried to get the build to compile the Groovy code first.

I got the following error:

Circular dependency between the following tasks:
:buildSrc:compileGroovy
\--- :buildSrc:compileJava
     \--- :buildSrc:compileKotlin
          \--- :buildSrc:compileGroovy (*)

So i tried without succeeding , to remove the compileJava task dependency from compileGroovy:

tasks.compileGroovy {
    dependsOn.remove(tasks.compileJava)
}

Some problem as above. Basically it is unclear to me how to remove precondigured taskdependencies in gradle

What i really need is something equivalent to gradle groovy build as :

compileGroovy.dependsOn = compileGroovy.taskDependencies.values - 'compileJava'
compileKotlin.dependsOn compileGroovy
compileKotlin.classpath += files(compileGroovy.destinationDir)
classes.dependsOn compileKotlin

How would look that like the Gradle Kotlin Dsl? Or are there better ways to solve this Groovy / Kotlin Code Dependency problem?

Version Info:

------------------------------------------------------------
Gradle 5.2.1
------------------------------------------------------------

Build time:   2019-02-08 19:00:10 UTC
Revision:     f02764e074c32ee8851a4e1877dd1fea8ffb7183

Kotlin DSL:   1.1.3
Kotlin:       1.3.20
Groovy:       2.5.4
Ant:          Apache Ant(TM) version 1.9.13 compiled on July 10 2018
JVM:          1.8.0_232 (AdoptOpenJDK 25.232-b09)
OS:           Mac OS X 10.15.3 x86_64

Upvotes: 0

Views: 4263

Answers (1)

tim_yates
tim_yates

Reputation: 171184

I think this is equivalent, though I think it just knocks Java out of the picture, so Groovy/Kotlin/Java buildSrc code won't work...:

tasks {
    val compileJava = named("compileJava", JavaCompile::class).get()
    val compileKotlin = named("compileKotlin", KotlinCompile::class).get()
    val compileGroovy = named("compileGroovy", GroovyCompile::class).get()
    val classes by getting

    compileGroovy.dependsOn.remove("compileJava")
    compileKotlin.setDependsOn(mutableListOf(compileGroovy))
    compileKotlin.classpath += files(compileGroovy.destinationDir)
    classes.setDependsOn(mutableListOf(compileKotlin))
}

This has been vastly improved in Gradle 6.1

https://docs.gradle.org/6.1/release-notes.html#defining-compilation-order-between-groovy,-scala-and-java

And I'm not sure the above works for test ordering if they have unexpected language dependency ordering

Upvotes: 1

Related Questions