Migration to Kotlin DSL 1.0

Hi

We tried to migrate our build scripts from Groovy and Kotlin and it raised several questions that we couldn’t answer. Can you please help?

  1. In Groovy we have the following block:

eclipse { classpath { downloadSources=true }}

I assume that we can leave it as is in Kotlin version but it gives us compliation error:

Cannot access 'downloadSources': it is private in 'EclipseClasspath'

But we couldn’t find any API that changes this property. Is it possible to adjust it?

  1. In the root build.gradle.kts we declared Java plugin:

plugins { java }

But we also had to declare it in build.gradle.kts in each-subproject otherwise it didn’t compile. Is it possible not to re-declare all the plugins in sub-projects?

  1. In Groovy scripts we have the following block:

subprojects { sourceCompatibility = 1.8 targetCompatibility = 1.8 }

We assume that in Kotlin version it should look like this:

subprojects {
  java {  
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
  }
}

But it gave us error message:

Extension with name 'java' does not exist. Currently registered extension names: [ext]

Hi,

  1. you must use isDownloadSources = true, this is a Kotlin/Java interoperability thing
  2. you can also apply plugins from parent projects
  3. for cross-project configuration injection, the type-safe accessors aren’t available

For #1, please read https://kotlinlang.org/docs/reference/java-interop.html#getters-and-setters
For #2 and #3, please read https://docs.gradle.org/current/userguide/kotlin_dsl.html, this is explained and demonstrated.

Hi @eskatos

Thank you a lot for the answers. It seems this option works fine in Kotlin DSL:

subprojects {  
   java.sourceCompatibility = JavaVersion.VERSION_1_8
   java.targetCompatibility = JavaVersion.VERSION_1_8
}

Are you sure, @Strannik ? I copy & pasted your code and under Gradle 5.0 it leads to:

* What went wrong:
Script compilation error:

  Line 6:     java.sourceCompatibility = JavaVersion.VERSION_1_8
                   ^ Unresolved reference: sourceCompatibility

1 error

Currently I’m hacking this like this:

    configure<JavaPluginExtension> {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }

@bentolor
Yes, I’ve just checked with Gradle 5.0 and it worked. Here is more detailed script:

plugins { 
  java
}

subprojects {  
   apply(plugin = "java")

   java.sourceCompatibility = JavaVersion.VERSION_1_8
   java.targetCompatibility = JavaVersion.VERSION_1_8

  repositories {
     jcenter()
   } 
}

Thanks for your reply, @Strannik. Indeed this solution works in that sense, that with plugins { java } you add the “Java” Plugin to the Root-Project and set the properties in the root project.

I tested and indeed the sourceCompability settings are then successfully inherited to the subprojects.

Nevertheless this adds java-Plugin Properties to the parent project and I’m note sure if this is desirable as the parent project is a pure umbrella projects and should i.e. not have itself a src/main/java-Directory etc.

When doing configuration injection from a project to another (here from the root to children), type-safe accessors are not available and you need to rely on the API. The following should work:

subprojects {
    configure<JavaPluginExtension> {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8  
    }
}

For more information on this subject, please read this.