0

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
2
  • 1
    Which version of gradle?
    – tim_yates
    Feb 13, 2020 at 21:28
  • @tim_yates good point. i added version info to the post Feb 13, 2020 at 22:03

1 Answer 1

1

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

2
  • totally cool , the first approach worked! I definitely learnt something. The remove is neat. But yes in the longer run, ii'll go for 6.1. Great thanks Feb 14, 2020 at 19:28
  • 1
    Small addition was necessary: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile Feb 15, 2020 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.