[Gradle] Fix additional java resource dirs are erased on KGP/Jvm plugin apply
User scripts may apply KGP/Jvm plugin not immediately in 'plugins {}'
block. And a script may also already add some additional resources into
a java source set before plugin apply. On apply plugin has erased such
additionally configured resource dirs as it assumed only the default one
are set. Now it also syncs them.
^KT-60459 Fixed
This commit is contained in:
committed by
Space Team
parent
11cefc52a7
commit
3b878336dd
+54
-12
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.gradle
|
||||
import org.gradle.util.GradleVersion
|
||||
import org.jetbrains.kotlin.gradle.testbase.*
|
||||
import org.junit.jupiter.api.DisplayName
|
||||
import java.util.zip.ZipFile
|
||||
import kotlin.io.path.*
|
||||
|
||||
@DisplayName("Handles resources correctly")
|
||||
@@ -32,22 +31,18 @@ class ResourcesIT : KGPBaseTest() {
|
||||
|kotlin {
|
||||
| sourceSets.main.resources.srcDir("additionalRes")
|
||||
|}
|
||||
|
|
||||
|tasks.named("jar", Jar) {
|
||||
| duplicatesStrategy = DuplicatesStrategy.INCLUDE
|
||||
|}
|
||||
""".trimMargin()
|
||||
)
|
||||
|
||||
build("jar") {
|
||||
assertFileInProjectExists("build/libs/simpleProject.jar")
|
||||
ZipFile(projectPath.resolve("build/libs/simpleProject.jar").toFile()).use { jar ->
|
||||
assert(jar.entries().asSequence().count { it.name == mainResFile.name } == 1) {
|
||||
"The jar should contain one entry `${mainResFile.name}` with no duplicates\n" +
|
||||
jar.entries().asSequence().map { it.name }.joinToString()
|
||||
}
|
||||
|
||||
assert(jar.entries().asSequence().count { it.name == additionalResFile.name } == 1) {
|
||||
"The jar should contain one entry `${additionalResFile.name}` with no duplicates\n" +
|
||||
jar.entries().asSequence().map { it.name }.joinToString()
|
||||
}
|
||||
}
|
||||
projectPath.resolve("build/libs/simpleProject.jar").assertZipArchiveContainsFilesOnce(
|
||||
listOf(mainResFile.name, additionalResFile.name)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,4 +60,51 @@ class ResourcesIT : KGPBaseTest() {
|
||||
build("assemble")
|
||||
}
|
||||
}
|
||||
|
||||
@GradleTest
|
||||
@DisplayName("KT-60459: should not overwrite custom resource directories")
|
||||
@JvmGradlePluginTests
|
||||
fun notOverwriteCustomResDirs(gradleVersion: GradleVersion) {
|
||||
project("simpleProject", gradleVersion) {
|
||||
val mainResDir = projectPath.resolve("src/main/resources").apply { createDirectories() }
|
||||
val mainResFile = mainResDir.resolve("main.txt").apply { writeText("Yay, Kotlin!") }
|
||||
|
||||
val additionalResDir = projectPath.resolve("src/main/extra-resources").apply { createDirectory() }
|
||||
val additionalResFile = additionalResDir.resolve("test.txt").apply { writeText("Kotlin!") }
|
||||
|
||||
buildGradle.writeText(
|
||||
"""
|
||||
|plugins {
|
||||
| id 'java'
|
||||
| id 'org.jetbrains.kotlin.jvm' apply false
|
||||
|}
|
||||
|
|
||||
|repositories {
|
||||
| mavenLocal()
|
||||
| mavenCentral()
|
||||
|}
|
||||
|
|
||||
|dependencies {
|
||||
| implementation 'com.google.guava:guava:12.0'
|
||||
|}
|
||||
|
|
||||
|tasks.named("jar", Jar) {
|
||||
| duplicatesStrategy = DuplicatesStrategy.INCLUDE
|
||||
|}
|
||||
|
|
||||
|sourceSets.main.resources.srcDir "src/main/extra-resources"
|
||||
|
|
||||
|apply plugin: 'org.jetbrains.kotlin.jvm'
|
||||
|
|
||||
""".trimMargin()
|
||||
)
|
||||
|
||||
build("jar", forceOutput = true) {
|
||||
assertFileInProjectExists("build/libs/simpleProject.jar")
|
||||
projectPath.resolve("build/libs/simpleProject.jar").assertZipArchiveContainsFilesOnce(
|
||||
listOf(mainResFile.name, additionalResFile.name)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+14
@@ -9,6 +9,7 @@ import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
import java.util.zip.ZipFile
|
||||
import kotlin.io.path.exists
|
||||
import kotlin.io.path.isDirectory
|
||||
import kotlin.io.path.readLines
|
||||
@@ -288,4 +289,17 @@ fun assertGradleVariant(gradleModuleFile: Path, variantName: String, code: Gradl
|
||||
}
|
||||
|
||||
GradleVariantAssertions(variantJson.asJsonObject).apply(code)
|
||||
}
|
||||
|
||||
fun Path.assertZipArchiveContainsFilesOnce(
|
||||
fileNames: List<String>
|
||||
) {
|
||||
ZipFile(toFile()).use { zip ->
|
||||
fileNames.forEach { fileName ->
|
||||
assert(zip.entries().asSequence().count { it.name == fileName } == 1) {
|
||||
"The jar should contain one entry `$fileName` with no duplicates\n" +
|
||||
zip.entries().asSequence().map { it.name }.joinToString()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+12
-5
@@ -187,12 +187,19 @@ internal abstract class AbstractKotlinPlugin(
|
||||
if (duplicateJavaSourceSetsAsKotlinSourceSets) {
|
||||
val kotlinSourceSet = project.kotlinExtension.sourceSets.maybeCreate(kotlinCompilation.name)
|
||||
kotlinSourceSet.kotlin.source(javaSourceSet.java)
|
||||
// Registering resources from KotlinSourceSet as Java SourceSet resources. In case of KotlinPlugin
|
||||
// Java Sources set will create ProcessResources task to process all resources into output
|
||||
// 'kotlinSourceSet.resources' should contain Java SourceSet default resource directories and to avoid
|
||||
// duplication error we are replacing here already configured default one.
|
||||
|
||||
// Registering resources from KotlinSourceSet as Java SourceSet resources.
|
||||
// In the case of KotlinPlugin Java Sources set will create ProcessResources task to process all resources into output
|
||||
// 'kotlinSourceSet.resources' should contain Java SourceSet default resource directories,
|
||||
// and to avoid duplication error, we are replacing the already configured default one.
|
||||
|
||||
// If KGP was applied with delay, it is possible that 'javaSourceSet.resources' may already have some additional
|
||||
// configuration. So we are syncing it into 'kotlinSourceSet.resources'.
|
||||
kotlinSourceSet.resources.srcDirs(javaSourceSet.resources.sourceDirectories.files)
|
||||
javaSourceSet.resources.setSrcDirs(
|
||||
listOf { kotlinSourceSet.resources.sourceDirectories }
|
||||
listOf {
|
||||
kotlinSourceSet.resources.sourceDirectories
|
||||
}
|
||||
)
|
||||
@Suppress("DEPRECATION")
|
||||
kotlinCompilation.addSourceSet(kotlinSourceSet)
|
||||
|
||||
Reference in New Issue
Block a user