Refactor Kotlin classes registration for java-library plugin

This commit is contained in:
Sergey Igushkin
2020-06-06 00:16:43 +03:00
parent 58dd0fa3d2
commit 0b2d96c1ef
2 changed files with 9 additions and 61 deletions
@@ -37,7 +37,6 @@ import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin
import org.jetbrains.kotlin.gradle.internal.KaptVariantData
import org.jetbrains.kotlin.gradle.internal.checkAndroidAnnotationProcessorDependencyUsage
import org.jetbrains.kotlin.gradle.logging.kotlinDebug
import org.jetbrains.kotlin.gradle.logging.kotlinWarn
import org.jetbrains.kotlin.gradle.model.builder.KotlinModelBuilder
import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.scripting.internal.ScriptingGradleSubplugin
@@ -228,40 +227,20 @@ internal class Kotlin2JvmSourceSetProcessor(
configureJavaTask(kotlinTask, javaTask)
}
if (project.pluginManager.hasPlugin("java-library") && sourceSetName == SourceSet.MAIN_SOURCE_SET_NAME) {
registerKotlinOutputForJavaLibrary(kotlinTaskInstance.destinationDir, kotlinTaskInstance)
if (sourceSetName == SourceSet.MAIN_SOURCE_SET_NAME) {
project.pluginManager.withPlugin("java-library") {
registerKotlinOutputForJavaLibrary(kotlinTask.map { it.destinationDir }, kotlinTask)
}
}
}
}
private fun registerKotlinOutputForJavaLibrary(outputDir: File, taskDependency: Task): Boolean {
private fun registerKotlinOutputForJavaLibrary(outputDir: Provider<File>, taskDependency: TaskProvider<*>) {
val configuration = project.configurations.getByName("apiElements")
checkedReflection({
val getOutgoing = configuration.javaClass.getMethod("getOutgoing")
val outgoing = getOutgoing(configuration)
val getVariants = outgoing.javaClass.getMethod("getVariants")
val variants = getVariants(outgoing) as NamedDomainObjectCollection<*>
val variant = variants.getByName("classes")
val artifactMethod = variant.javaClass.getMethod("artifact", Any::class.java)
val artifactMap = mapOf(
"file" to outputDir,
"type" to "java-classes-directory",
"builtBy" to taskDependency
)
artifactMethod(variant, artifactMap)
return true
}, { reflectException ->
logger.kotlinWarn("Could not register Kotlin output of source set $sourceSetName for java-library: $reflectException")
return false
})
configuration.outgoing.variants.getByName("classes").artifact(outputDir) {
it.builtBy(taskDependency)
it.type = "java-classes-directory"
}
}
}
@@ -1,31 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle.utils
import java.lang.reflect.InvocationTargetException
internal inline fun <T> checkedReflection(block: () -> T, onReflectionException: (Exception) -> T): T {
return try {
block()
} catch (e: InvocationTargetException) {
throw e.targetException
} catch (e: ReflectiveOperationException) {
onReflectionException(e)
} catch (e: IllegalArgumentException) {
onReflectionException(e)
}
}