How to distribute Kotlin boiler configuration in several Gradle projects?

the typical Kotlin configuration in the Gradle project is very boilerplate, and I'm looking for a way to abstract it into an external build script so that it can be reused.

I have a working solution (see below), but it seems a bit like a hack since kotlin-w141> -plugin does not work out of the box this way.

It is random to apply any non-standard plugin from an external script, since you cannot apply the plugin by id , i.e.

apply plugin: 'kotlin' will result in Plugin with id 'kotlin' not found.

A simple (well, usually) workaround is to use the full class of the plugin name, i.e.

apply plugin: org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper

which in this case raises a small slight exception indicating that the plugin probably should not have been named as follows:

Failed to determine source cofiguration of kotlin plugin. 
Can not download core. Please verify that this or any parent project
contains 'kotlin-gradle-plugin' in buildscript classpath configuration.

( ), buildscript.

kotlin.gradle

buildscript {
    ext.kotlin_version = "1.0.3"
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
}

apply plugin: CustomKotlinPlugin
import org.jetbrains.kotlin.gradle.plugin.CleanUpBuildListener
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin
import org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider

/**
 * Wrapper around the Kotlin plugin wrapper (this code is largely a refactoring of KotlinBasePluginWrapper).
 * This is required because the default behaviour expects the kotlin plugin to be applied from the project,
 * not from an external buildscript.
 */
class CustomKotlinPlugin extends KotlinBasePluginWrapper {

    @Override
    void apply(Project project) {
        // use String literal as KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY constant isn't available
        System.setProperty("kotlin.environment.keepalive", "true")

        // just use the kotlin version defined in this script
        project.extensions.extraProperties?.set("kotlin.gradle.plugin.version", project.property('kotlin_version'))

        // get the plugin using the current buildscript
        def plugin = getPlugin(this.class.classLoader, project.buildscript)
        plugin.apply(project)

        def cleanUpBuildListener = new CleanUpBuildListener(this.class.classLoader, project)
        cleanUpBuildListener.buildStarted()
        project.gradle.addBuildListener(cleanUpBuildListener)
    }

    @Override
    Plugin<Project> getPlugin(ClassLoader pluginClassLoader, ScriptHandler scriptHandler){
        return new KotlinPlugin(scriptHandler, new KotlinTasksProvider(pluginClassLoader));
    }
}

(.. apply from: "kotlin.gradle"), Kotlin.

, , , ? , Kotlin.

+4

Source: https://habr.com/ru/post/1652296/


All Articles