From 1e9d546bf128d269ea7146911b9b9dab76d4771f Mon Sep 17 00:00:00 2001 From: jrhee17 Date: Wed, 24 Jul 2024 14:50:54 +0900 Subject: [PATCH 1/4] minimal impl --- core/build.gradle | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index c01d1a9c3d2..dd326e235ac 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -62,9 +62,12 @@ tasks.withType(Jar) { // Proguard can't handle multi-release jars so we recreate it here. tasks.trimShadedJar.doLast { // outjars is a file, so only one jar generated for sure - def trimmed = tasks.trimShadedJar.outJarFiles[0].toPath() + def outJarFile = tasks.trimShadedJar.outJarFiles[0] + def trimmed = outJarFile.toPath().toString() + def tmp = "${trimmed}.tmp" - ant.jar(destfile: trimmed.toString(), update: true, duplicate: 'fail') { + ant.jar(destfile: tmp, duplicate: 'fail', filesetmanifest:'merge') { + zipfileset(src: outJarFile, excludes: 'META-INF/services/java.security.Provider') zipfileset(src: tasks.shadedJar.archiveFile.get().asFile) { include(name: 'META-INF/versions/**') } @@ -74,6 +77,7 @@ tasks.trimShadedJar.doLast { attribute(name: 'Created-By', value: "Gradle ${gradle.gradleVersion}") } } + ant.move(file: tmp, tofile: trimmed) } tasks.shadedTest.exclude 'META-INF/versions/**' From 946eb720babd6aa49e6a6ca70ec5dc95f8990dbb Mon Sep 17 00:00:00 2001 From: jrhee17 Date: Wed, 24 Jul 2024 17:37:36 +0900 Subject: [PATCH 2/4] address comment by @ikhoon --- core/build.gradle | 8 ++------ gradle.properties | 1 + gradle/scripts/lib/java-shade.gradle | 8 ++++++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index dd326e235ac..c01d1a9c3d2 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -62,12 +62,9 @@ tasks.withType(Jar) { // Proguard can't handle multi-release jars so we recreate it here. tasks.trimShadedJar.doLast { // outjars is a file, so only one jar generated for sure - def outJarFile = tasks.trimShadedJar.outJarFiles[0] - def trimmed = outJarFile.toPath().toString() - def tmp = "${trimmed}.tmp" + def trimmed = tasks.trimShadedJar.outJarFiles[0].toPath() - ant.jar(destfile: tmp, duplicate: 'fail', filesetmanifest:'merge') { - zipfileset(src: outJarFile, excludes: 'META-INF/services/java.security.Provider') + ant.jar(destfile: trimmed.toString(), update: true, duplicate: 'fail') { zipfileset(src: tasks.shadedJar.archiveFile.get().asFile) { include(name: 'META-INF/versions/**') } @@ -77,7 +74,6 @@ tasks.trimShadedJar.doLast { attribute(name: 'Created-By', value: "Gradle ${gradle.gradleVersion}") } } - ant.move(file: tmp, tofile: trimmed) } tasks.shadedTest.exclude 'META-INF/versions/**' diff --git a/gradle.properties b/gradle.properties index bcc699d8a57..f230d5fcd73 100644 --- a/gradle.properties +++ b/gradle.properties @@ -27,4 +27,5 @@ org.gradle.jvmargs=-Xmx2048m -XX:+HeapDumpOnOutOfMemoryError systemProp.https.protocols=TLSv1,TLSv1.1,TLSv1.2 jacocoExclusions=com/linecorp/armeria/internal/common/CurrentJavaVersionSpecific,com/linecorp/armeria/*/scalapb/**,META-INF/versions/** +shadowExclusions=META-INF/services/java.security.Provider org.gradle.caching = true diff --git a/gradle/scripts/lib/java-shade.gradle b/gradle/scripts/lib/java-shade.gradle index 061b1304470..fb661b690c6 100644 --- a/gradle/scripts/lib/java-shade.gradle +++ b/gradle/scripts/lib/java-shade.gradle @@ -33,6 +33,14 @@ configure(relocatedProjects) { exclude '/META-INF/*.RSA' // Exclude the files generated by Maven exclude '/META-INF/maven/**' + + def shadowExclusions = [] + if (rootProject.hasProperty('shadowExclusions')) { + shadowExclusions = rootProject.findProperty('shadowExclusions').split(",") + } + shadowExclusions.each { + exclude it + } } tasks.assemble.dependsOn tasks.shadedJar From 1f813be8f8407e9c81427816ebb3178449a5b3dc Mon Sep 17 00:00:00 2001 From: jrhee17 Date: Wed, 24 Jul 2024 17:42:42 +0900 Subject: [PATCH 3/4] documentation --- gradle/scripts/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/gradle/scripts/README.md b/gradle/scripts/README.md index 29bc8c81f0b..74fc0ac2ec1 100644 --- a/gradle/scripts/README.md +++ b/gradle/scripts/README.md @@ -576,6 +576,9 @@ relocations [ { from: "com.google.common", to: "com.doe.john.myproject.shaded.gu Unshaded tests are disabled by default when a shading task is configured. If you want to run unshaded tests, you can specify `-PpreferShadedTests=false` option. +If you would like to remove specific files when shading the Jar, you may specify the +`-PshadowExclusions=` option. + ### Trimming a shaded JAR with `trim` flag If you shade many dependencies, your JAR will grow huge, even if you only use From a4455c465de164bfda84bd623d2821c44b5d3401 Mon Sep 17 00:00:00 2001 From: jrhee17 Date: Wed, 24 Jul 2024 17:44:25 +0900 Subject: [PATCH 4/4] Jar -> JAR --- gradle/scripts/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/scripts/README.md b/gradle/scripts/README.md index 74fc0ac2ec1..3de234e76a3 100644 --- a/gradle/scripts/README.md +++ b/gradle/scripts/README.md @@ -576,7 +576,7 @@ relocations [ { from: "com.google.common", to: "com.doe.john.myproject.shaded.gu Unshaded tests are disabled by default when a shading task is configured. If you want to run unshaded tests, you can specify `-PpreferShadedTests=false` option. -If you would like to remove specific files when shading the Jar, you may specify the +If you would like to remove specific files when shading the JAR, you may specify the `-PshadowExclusions=` option. ### Trimming a shaded JAR with `trim` flag