-
Notifications
You must be signed in to change notification settings - Fork 67
Description
I have a multiplatform project that contains a JVM target with additional Java sources (via withJava()
). When applying the atomicfu plugin, the Java sources are no longer present in the jar. This can be mitigated by disabling this transformation (i.e. disabling the JVM transformation or using IR transforms).
For reproducing, the mpp-sample
can be adjusted accordingly:
The bug is most likely caused by the rewiring of the compilations classes here:
Lines 322 to 364 in b5db8fc
val classesDirs = compilation.output.classesDirs | |
// make copy of original classes directory | |
@Suppress("UNCHECKED_CAST") | |
val compilationTask = compilation.compileTaskProvider as TaskProvider<KotlinCompileTool> | |
val originalDestinationDirectory = project.layout.buildDirectory | |
.dir("classes/atomicfu-orig/${target.name}/${compilation.name}") | |
compilationTask.configure { | |
if (it is Kotlin2JsCompile) { | |
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "EXPOSED_PARAMETER_TYPE") | |
it.defaultDestinationDirectory.value(originalDestinationDirectory) | |
} else { | |
it.destinationDirectory.value(originalDestinationDirectory) | |
} | |
} | |
val originalClassesDirs: FileCollection = project.objects.fileCollection() | |
.from(compilationTask.flatMap { it.destinationDirectory }) | |
.from({ project.files(classesDirs.from).filter { it.exists() } }) | |
originalDirsByCompilation[compilation] = originalClassesDirs | |
val transformedClassesDir = project.layout.buildDirectory | |
.dir("classes/atomicfu/${target.name}/${compilation.name}") | |
val transformTask = when (target.platformType) { | |
KotlinPlatformType.jvm, KotlinPlatformType.androidJvm -> { | |
// create transformation task only if transformation is required and JVM IR compiler transformation is not enabled | |
if (config.transformJvm) { | |
project.registerJvmTransformTask(compilation) | |
.configureJvmTask( | |
compilation.compileDependencyFiles, | |
compilation.compileAllTaskName, | |
transformedClassesDir, | |
originalClassesDirs, | |
config | |
) | |
.also { | |
compilation.defaultSourceSet.kotlin.compiledBy(it, AtomicFUTransformTask::destinationDirectory) | |
} | |
} else null | |
} | |
else -> error("Unsupported transformation platform '${target.platformType}'") | |
} | |
if (transformTask != null) { | |
//now transformTask is responsible for compiling this source set into the classes directory | |
compilation.defaultSourceSet.kotlin.destinationDirectory.value(transformedClassesDir) | |
classesDirs.setFrom(transformedClassesDir) |
At the very end, setFrom(...)
is called to overwrite the classes, but this never considers the output of the Java compile task for KotlinJvmCompilation
s. A naive fix looks as follows. However, I'm unsure if this is a proper fix for this, as I'm unfamiliar with how exactly the transform functions. Maybe the Java sources should also be considered by the transformation?
diff --git a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt
index 052550f..de08900 100644
--- a/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt
+++ b/atomicfu-gradle-plugin/src/main/kotlin/kotlinx/atomicfu/plugin/gradle/AtomicFUGradlePlugin.kt
@@ -17,6 +17,7 @@ import org.gradle.util.*
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.dsl.KotlinCompile
import org.jetbrains.kotlin.gradle.plugin.*
+import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmCompilation
import org.jetbrains.kotlin.gradle.tasks.*
import java.io.*
import java.util.*
@@ -362,6 +363,9 @@ private fun Project.configureTransformationForTarget(target: KotlinTarget) {
//now transformTask is responsible for compiling this source set into the classes directory
compilation.defaultSourceSet.kotlin.destinationDirectory.value(transformedClassesDir)
classesDirs.setFrom(transformedClassesDir)
+ if (compilation is KotlinJvmCompilation) {
+ classesDirs.from(compilation.compileJavaTaskProvider)
+ }
classesDirs.setBuiltBy(listOf(transformTask))
tasks.withType(Jar::class.java).configureEach {
if (name == target.artifactsTaskName) {