From d534e071605a7e95ef10c35e305c42b9df6c04c7 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 8 Apr 2025 19:02:30 +0200 Subject: [PATCH 1/4] README: Add "kotlin" to a code block for proper syntax highlighting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 86b4966..b367fae 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ If you want to keep them, you need to keep `kotlin.Metadata` and `kotlin.Unit`: You can specify the version of the java runtime to use with `systemClassesToolchain`: -``` +```kotlin gr8 { val shadowedJar = create("gr8") { proguardFile("rules.pro") From f75684e3b5f665db48d6906ed32deb1b14b882a5 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 8 Apr 2025 19:15:25 +0200 Subject: [PATCH 2/4] README: Remove double-nesting with `create()` --- README.md | 56 +++++++++++++++++++++++++++---------------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index b367fae..16e871d 100644 --- a/README.md +++ b/README.md @@ -106,36 +106,34 @@ dependencies { if (shadow) { gr8 { - create("default") { - val shadowedJar = create("default") { - addProgramJarsFrom(shadowedDependencies) - addProgramJarsFrom(tasks.getByName("jar")) - // classpath jars are only used by R8 for analysis but are not included in the - // final shadowed jar. - addClassPathJarsFrom(compileOnlyDependencies) - - proguardFile("rules.pro") - - // for more information about the different options, refer to their matching R8 documentation - // at https://r8.googlesource.com/r8#running-r8 - - // See https://issuetracker.google.com/u/1/issues/380805015 for why this is required - registerFilterTransform(listOf(".*/impldep/META-INF/versions/.*")) - } - - removeGradleApiFromApi() - - // Optional: replace the regular jar with the shadowed one in the publication - replaceOutgoingJar(shadowedJar) - - // Or if you prefer the shadowed jar to be a separate variant in the default publication - // The variant will have `org.gradle.dependency.bundling = shadowed` - addShadowedVariant(shadowedJar) - - // Allow to compile the module without exposing the shadowedDependencies downstream - configurations.getByName("compileOnly").extendsFrom(shadowedDependencies) - configurations.getByName("testImplementation").extendsFrom(shadowedDependencies) + val shadowedJar = create("default") { + addProgramJarsFrom(shadowedDependencies) + addProgramJarsFrom(tasks.getByName("jar")) + // classpath jars are only used by R8 for analysis but are not included in the + // final shadowed jar. + addClassPathJarsFrom(compileOnlyDependencies) + + proguardFile("rules.pro") + + // for more information about the different options, refer to their matching R8 documentation + // at https://r8.googlesource.com/r8#running-r8 + + // See https://issuetracker.google.com/u/1/issues/380805015 for why this is required + registerFilterTransform(listOf(".*/impldep/META-INF/versions/.*")) } + + removeGradleApiFromApi() + + // Optional: replace the regular jar with the shadowed one in the publication + replaceOutgoingJar(shadowedJar) + + // Or if you prefer the shadowed jar to be a separate variant in the default publication + // The variant will have `org.gradle.dependency.bundling = shadowed` + addShadowedVariant(shadowedJar) + + // Allow to compile the module without exposing the shadowedDependencies downstream + configurations.getByName("compileOnly").extendsFrom(shadowedDependencies) + configurations.getByName("testImplementation").extendsFrom(shadowedDependencies) } } else { configurations.getByName("implementation").extendsFrom(shadowedDependencies) From 8bfc62fcde8ad9cb1025de0439947c9e742dd2ab Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 8 Apr 2025 19:12:32 +0200 Subject: [PATCH 3/4] README: Modernize Kotlin DSL examples --- README.md | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 16e871d..1023792 100644 --- a/README.md +++ b/README.md @@ -34,14 +34,15 @@ dependencies { * Create a separate configuration to resolve compileOnly dependencies. * You can skip this if you have no compileOnly dependencies. */ -val compileOnlyDependencies: Configuration = configurations.create("compileOnlyDependencies") -compileOnlyDependencies.extendsFrom(configurations.getByName("compileOnly")) +val compileOnlyDependencies by configurations.creating { + extendsFrom(configurations.compileOnly.get()) +} gr8 { val shadowedJar = create("gr8") { // program jars are included in the final shadowed jar - addProgramJarsFrom(configurations.getByName("runtimeClasspath")) - addProgramJarsFrom(tasks.getByName("jar")) + addProgramJarsFrom(configurations.runtimeClasspath.get()) + addProgramJarsFrom(tasks.named("jar")) // classpath jars are only used by R8 for analysis but are not included in the // final shadowed jar. addClassPathJarsFrom(compileOnlyDependencies) @@ -85,22 +86,22 @@ Using Gr8 to shadow dependencies in Gradle plugin is a typical use case but requ To work around this, you can use, `removeGradleApiFromApi()`, `registerTransform()` and custom configurations: ```kotlin -val shadowedDependencies = configurations.create("shadowedDependencies") +val shadowedDependencies by configurations.creating -val compileOnlyDependencies: Configuration = configurations.create("compileOnlyDependencies") { +val compileOnlyDependencies by configurations.creating { attributes { attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_API)) - } - // this attribute is needed to filter out some classes, see https://issuetracker.google.com/u/1/issues/380805015 - attributes { + + // this attribute is needed to filter out some classes, see https://issuetracker.google.com/u/1/issues/380805015 attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType) } + + extendsFrom(configurationscompileOnly.get()) } -compileOnlyDependencies.extendsFrom(configurations.getByName("compileOnly")) dependencies { - add(shadowedDependencies.name, "com.squareup.okhttp3:okhttp:4.9.0") - add("compileOnly", gradleApi()) + shadowedDependencies("com.squareup.okhttp3:okhttp:4.9.0") + compileOnly(gradleApi()) // More dependencies here } @@ -108,7 +109,7 @@ if (shadow) { gr8 { val shadowedJar = create("default") { addProgramJarsFrom(shadowedDependencies) - addProgramJarsFrom(tasks.getByName("jar")) + addProgramJarsFrom(tasks.named("jar")) // classpath jars are only used by R8 for analysis but are not included in the // final shadowed jar. addClassPathJarsFrom(compileOnlyDependencies) @@ -132,11 +133,11 @@ if (shadow) { addShadowedVariant(shadowedJar) // Allow to compile the module without exposing the shadowedDependencies downstream - configurations.getByName("compileOnly").extendsFrom(shadowedDependencies) - configurations.getByName("testImplementation").extendsFrom(shadowedDependencies) + configurations.compileOnly.get().extendsFrom(shadowedDependencies) + configurations.testImplementation.get().extendsFrom(shadowedDependencies) } } else { - configurations.getByName("implementation").extendsFrom(shadowedDependencies) + configurations.implementation.get().extendsFrom(shadowedDependencies) } ``` @@ -164,9 +165,9 @@ You can specify the version of the java runtime to use with `systemClassesToolch gr8 { val shadowedJar = create("gr8") { proguardFile("rules.pro") - addProgramJarsFrom(configurations.getByName("runtimeClasspath")) + addProgramJarsFrom(configurations.runtimeClasspath) systemClassesToolchain { - languageVersion.set(JavaLanguageVersion.of(11)) + languageVersion = JavaLanguageVersion.of(11) } } } From 2b414f1f97aa1a5d5571bb26a9b0a10868589e3b Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Tue, 8 Apr 2025 21:57:09 +0200 Subject: [PATCH 4/4] Combine `attributes` in various places for simplicity --- CHANGELOG.md | 2 -- gr8-plugin/build.gradle.kts | 2 -- test-plugin/build.gradle.kts | 2 -- 3 files changed, 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 37d8867..7fdca07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -67,8 +67,6 @@ gr8 { val compileOnlyDependenciesForGr8 = configurations.create("compileOnlyDependenciesForGr8") { attributes { attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType) - } - attributes { attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_API)) } } diff --git a/gr8-plugin/build.gradle.kts b/gr8-plugin/build.gradle.kts index 793cb52..e53bc4b 100644 --- a/gr8-plugin/build.gradle.kts +++ b/gr8-plugin/build.gradle.kts @@ -12,8 +12,6 @@ plugins { val filteredClasspathDependencies: Configuration = configurations.create("filteredClasspathDependencies") { attributes { attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType) - } - attributes { attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_API)) } } diff --git a/test-plugin/build.gradle.kts b/test-plugin/build.gradle.kts index 67cca6e..2dab62a 100644 --- a/test-plugin/build.gradle.kts +++ b/test-plugin/build.gradle.kts @@ -21,8 +21,6 @@ dependencies { val compileOnlyDependenciesForGr8: Configuration = configurations.create("compileOnlyDependenciesForGr8") { attributes { attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, FilterTransform.artifactType) - } - attributes { attribute(Usage.USAGE_ATTRIBUTE, project.objects.named(Usage.JAVA_API)) } }