Home > Back-end >  Converting SourceSet definition in plugin from Groovy DSL to Kotlin DSL
Converting SourceSet definition in plugin from Groovy DSL to Kotlin DSL

Time:12-08

I've got a method in my Gradle plugin which declares a new "scratch" source set. I'm trying to convert this method from Groovy to Kotlin, but I cannot figure out how I'm meant to declare the new SourceSet using Kotlin.

class JavaConventionsPlugin : Plugin<Project> {
    // ...
    def scratchConfiguration(project) {
        project.sourceSets {
            scratch {
            }
        }

        project.configurations {
            // make scratch configurations include all normal dependencies automatically
            scratchCompile.extendsFrom mainCompile
            scratchRuntime.extendsFrom mainRuntime
            scratchCompileOnly.extendsFrom compileOnly
            scratchImplementation.extendsFrom implementation
            scratchRuntimeOnly.extendsFrom runtimeOnly
        }

        project.dependencies {
            // add dependency on main java code from scratch java code
            scratchImplementation project.extensions.getByType(JavaPluginExtension).sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME).output
        }
    }
}

Within the converted scratchConfiguration method, all of these seem to be unresolved references:

  • sourceSets
  • project.sourceSets
  • java.sourceSets

So what should the new SourceSet declaration look like?

CodePudding user response:

They are unresolved because you are moving from a weakly typed/dynamic language (Groovy) to a strongly typed one (Kotlin). At its core, Gradle is written in plain Java. So, you need to make use of the Java APIs instead of the syntactic sugar that the Groovy DSL had provided.

Your snippet above would moreorless translate to the following:

import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.plugins.JavaPlugin
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer

class ScratchGradlePlugin: Plugin<Project> {

    override fun apply(project: Project) {
        val sourceSets = project.extensions.getByType(SourceSetContainer::class.java)
        val main = sourceSets.named(SourceSet.MAIN_SOURCE_SET_NAME)

        sourceSets.register("scratch") {
            it.compileClasspath.plus(main.get().output)
            it.runtimeClasspath.plus(main.get().output)
        }

        val configurations = project.configurations
        
        configurations.named("scratchImplementation") {
            it.extendsFrom(configurations.named(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME).get())
        }

        configurations.named("scratchRuntimeOnly") {
            it.extendsFrom(configurations.named(JavaPlugin.RUNTIME_ONLY_CONFIGURATION_NAME).get())
        }
        
    }

}

I have omitted the project.dependencies {} part in your snippet since the sample above has scratch extend from main which is your main Java code.

Ref: https://docs.gradle.org/current/userguide/java_gradle_plugin.html#java_gradle_plugin

  • Related