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/README.md b/README.md index 86b4966..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,60 +86,58 @@ 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 } 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.named("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.compileOnly.get().extendsFrom(shadowedDependencies) + configurations.testImplementation.get().extendsFrom(shadowedDependencies) } } else { - configurations.getByName("implementation").extendsFrom(shadowedDependencies) + configurations.implementation.get().extendsFrom(shadowedDependencies) } ``` @@ -162,13 +161,13 @@ 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") - addProgramJarsFrom(configurations.getByName("runtimeClasspath")) + addProgramJarsFrom(configurations.runtimeClasspath) systemClassesToolchain { - languageVersion.set(JavaLanguageVersion.of(11)) + languageVersion = JavaLanguageVersion.of(11) } } } 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)) } }