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
class CustomKotlinPlugin extends KotlinBasePluginWrapper {
@Override
void apply(Project project) {
System.setProperty("kotlin.environment.keepalive", "true")
project.extensions.extraProperties?.set("kotlin.gradle.plugin.version", project.property('kotlin_version'))
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.