Fix applying subplugin from script

#KT-24497 fixed
This commit is contained in:
Alexey Tsvetkov
2018-05-30 03:12:59 +03:00
parent 6eebcf7f18
commit 42350ffb1a
7 changed files with 74 additions and 1 deletions
@@ -131,4 +131,13 @@ class SubpluginsIT : BaseGradleIT() {
assertFileExists("${kotlinClassesDir(subproject = "a/b", sourceSet = "test")}MyTestClass.class")
}
}
@Test
fun testAllopenFromScript() {
Project("allOpenFromScript").build("build") {
assertSuccessful()
assertFileExists("${kotlinClassesDir(sourceSet = "main")}MyClass.class")
assertFileExists("${kotlinClassesDir(sourceSet = "test")}MyTestClass.class")
}
}
}
@@ -0,0 +1,26 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
apply plugin: org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper
apply plugin: org.jetbrains.kotlin.allopen.gradle.AllOpenGradleSubplugin
repositories {
mavenLocal()
mavenCentral()
}
allOpen {
annotation("AllOpen")
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
@@ -0,0 +1,9 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
@AllOpen
class MyClass {
fun f() = 0
}
@@ -0,0 +1,6 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
annotation class AllOpen
@@ -0,0 +1,8 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
class MyTestClass : MyClass() {
override fun f() = 1
}
@@ -666,7 +666,21 @@ private fun SourceSet.clearJavaSrcDirs() {
private fun loadSubplugins(project: Project, kotlinPluginVersion: String): SubpluginEnvironment =
try {
val subplugins = ServiceLoader.load(KotlinGradleSubplugin::class.java, project.buildscript.classLoader)
val klass = KotlinGradleSubplugin::class.java
val buildscriptClassloader = project.buildscript.classLoader
val klassFromBuildscript = try {
buildscriptClassloader.loadClass(klass.canonicalName)
} catch (e: ClassNotFoundException) {
null
}
val classloader = if (klass == klassFromBuildscript) {
buildscriptClassloader
} else {
klass.classLoader
}
val subplugins = ServiceLoader.load(KotlinGradleSubplugin::class.java, classloader)
.map { @Suppress("UNCHECKED_CAST") (it as KotlinGradleSubplugin<AbstractCompile>) }
SubpluginEnvironment(subplugins, kotlinPluginVersion)