From 8e17e5410c3da523e54795f23685a6def20aa386 Mon Sep 17 00:00:00 2001 From: Goooler Date: Wed, 16 Apr 2025 15:54:13 +0800 Subject: [PATCH 1/3] Test `relocateStringConstantsByDefault` --- .../gradle/plugins/shadow/RelocationTest.kt | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt index eb1543a82..38d5369e5 100644 --- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt +++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt @@ -8,9 +8,11 @@ import assertk.assertions.isInstanceOf import assertk.assertions.isNotEmpty import assertk.fail import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHADOW_JAR_TASK_NAME +import com.github.jengelman.gradle.plugins.shadow.internal.mainClassAttributeKey import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction.Companion.CONSTANT_TIME_FOR_ZIP_ENTRIES import com.github.jengelman.gradle.plugins.shadow.util.Issue import com.github.jengelman.gradle.plugins.shadow.util.containsOnly +import com.github.jengelman.gradle.plugins.shadow.util.runProcess import java.net.URLClassLoader import kotlin.io.path.appendText import kotlin.io.path.writeText @@ -581,6 +583,44 @@ class RelocationTest : BasePluginTest() { } } + @Test + fun relocateStringConstantsByDefault() { + writeClass { + """ + package my; + public class Main { + public static final String junit = "junit.framework.Test"; + public static void main(String[] args) { + System.out.println(getValue() + junit); + } + // Use this method to force the compiler to not inline the string literal. + private static String getValue() { return "the value is "; } + } + """.trimIndent() + } + projectScriptPath.appendText( + """ + $shadowJar { + manifest { + attributes '$mainClassAttributeKey': 'my.Main' + } + relocate('junit', 'foo.junit') + } + """.trimIndent(), + ) + + run(shadowJarTask) { + it.withDebug(true) + } + + val pathString = outputShadowJar.use { it.toString() } + val result = runProcess("java", "-jar", pathString) + + assertThat(result).contains( + "the value is foo.junit.framework.Test", + ) + } + private companion object { @JvmStatic fun prefixProvider() = listOf( From 408799d4ea82df92dc41126d8daba86090e6b65e Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 17 Apr 2025 18:00:18 +0800 Subject: [PATCH 2/3] Fix the test --- .../jengelman/gradle/plugins/shadow/RelocationTest.kt | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt index 38d5369e5..633c10492 100644 --- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt +++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt @@ -589,12 +589,9 @@ class RelocationTest : BasePluginTest() { """ package my; public class Main { - public static final String junit = "junit.framework.Test"; public static void main(String[] args) { - System.out.println(getValue() + junit); + System.out.println("junit.framework.Test"); } - // Use this method to force the compiler to not inline the string literal. - private static String getValue() { return "the value is "; } } """.trimIndent() } @@ -609,15 +606,13 @@ class RelocationTest : BasePluginTest() { """.trimIndent(), ) - run(shadowJarTask) { - it.withDebug(true) - } + run(shadowJarTask) val pathString = outputShadowJar.use { it.toString() } val result = runProcess("java", "-jar", pathString) assertThat(result).contains( - "the value is foo.junit.framework.Test", + "foo.junit.framework.Test", ) } From 226a5566a081876648b3c5750db23234e61c61f0 Mon Sep 17 00:00:00 2001 From: Goooler Date: Thu, 17 Apr 2025 18:06:41 +0800 Subject: [PATCH 3/3] Cleanup --- .../github/jengelman/gradle/plugins/shadow/RelocationTest.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt index 633c10492..083d955ce 100644 --- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt +++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt @@ -607,9 +607,7 @@ class RelocationTest : BasePluginTest() { ) run(shadowJarTask) - - val pathString = outputShadowJar.use { it.toString() } - val result = runProcess("java", "-jar", pathString) + val result = runProcess("java", "-jar", outputShadowJar.use { it.toString() }) assertThat(result).contains( "foo.junit.framework.Test",