From 22876ae20c7e51f9a4275b692916a89d0a8ae33a Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 26 Nov 2024 15:55:38 +0800 Subject: [PATCH 1/3] Resolve Configuration directly --- api/shadow.api | 2 +- .../plugins/shadow/internal/AbstractDependencyFilter.kt | 5 ++--- .../gradle/plugins/shadow/internal/DependencyFilter.kt | 9 +++++---- .../jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt | 5 +++-- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/api/shadow.api b/api/shadow.api index 4679b6b60..7a38f335c 100644 --- a/api/shadow.api +++ b/api/shadow.api @@ -100,7 +100,7 @@ public abstract interface class com/github/jengelman/gradle/plugins/shadow/inter public abstract fun project (Ljava/lang/String;)Lorg/gradle/api/specs/Spec; public abstract fun project (Ljava/util/Map;)Lorg/gradle/api/specs/Spec; public abstract fun resolve (Ljava/util/Collection;)Lorg/gradle/api/file/FileCollection; - public abstract fun resolve (Lorg/gradle/api/file/FileCollection;)Lorg/gradle/api/file/FileCollection; + public abstract fun resolve (Lorg/gradle/api/artifacts/Configuration;)Lorg/gradle/api/file/FileCollection; } public abstract interface class com/github/jengelman/gradle/plugins/shadow/internal/ZipCompressor : org/gradle/api/internal/file/archive/compression/ArchiveOutputStreamFactory { diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/AbstractDependencyFilter.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/AbstractDependencyFilter.kt index 74144effe..c0fbaf4af 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/AbstractDependencyFilter.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/AbstractDependencyFilter.kt @@ -22,16 +22,15 @@ internal sealed class AbstractDependencyFilter( excludedDependencies: MutableSet, ) - override fun resolve(configuration: FileCollection): FileCollection { + override fun resolve(configuration: Configuration): FileCollection { val includedDeps = mutableSetOf() val excludedDeps = mutableSetOf() - configuration as Configuration resolve(configuration.resolvedConfiguration.firstLevelModuleDependencies, includedDeps, excludedDeps) return project.files(configuration.files) - project.files(excludedDeps.flatMap { it.moduleArtifacts.map(ResolvedArtifact::getFile) }) } - override fun resolve(configurations: Collection): FileCollection { + override fun resolve(configurations: Collection): FileCollection { return configurations.map { resolve(it) } .reduceOrNull { acc, fileCollection -> acc + fileCollection } ?: project.files() diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DependencyFilter.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DependencyFilter.kt index c89fa8de9..341e3377e 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DependencyFilter.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DependencyFilter.kt @@ -1,6 +1,7 @@ package com.github.jengelman.gradle.plugins.shadow.internal import groovy.lang.Closure +import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.Dependency import org.gradle.api.artifacts.ResolvedDependency import org.gradle.api.file.FileCollection @@ -8,14 +9,14 @@ import org.gradle.api.specs.Spec public interface DependencyFilter { /** - * Resolve a FileCollection against the include/exclude rules in the filter. + * Resolve a [Configuration] against the include/exclude rules in the filter. */ - public fun resolve(configuration: FileCollection): FileCollection + public fun resolve(configuration: Configuration): FileCollection /** - * Resolve all FileCollections against the include/exclude rules in the filter and combine the results. + * Resolve all [Configuration]s against the include/exclude rules in the filter and combine the results. */ - public fun resolve(configurations: Collection): FileCollection + public fun resolve(configurations: Collection): FileCollection /** * Exclude dependencies that match the provided spec. diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt index cdcc43ab6..2e45233a3 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt @@ -20,6 +20,7 @@ import java.util.concurrent.Callable import java.util.jar.JarFile import org.apache.tools.zip.ZipOutputStream import org.gradle.api.Action +import org.gradle.api.artifacts.Configuration import org.gradle.api.file.DuplicatesStrategy import org.gradle.api.file.FileCollection import org.gradle.api.internal.DocumentationRegistry @@ -47,7 +48,7 @@ public abstract class ShadowJar : ShadowSpec { private val _transformers = mutableListOf() private val _relocators = mutableListOf() - private val _configurations = mutableListOf() + private val _configurations = mutableListOf() private val _stats = ShadowStats() private val _includedDependencies = project.files(Callable { _dependencyFilter.resolve(_configurations) }) @@ -158,7 +159,7 @@ public abstract class ShadowJar : @get:Classpath @get:Optional - public var configurations: List + public var configurations: List get() = _configurations set(value) { _configurations.clear() From f17b0212d8910594b106b888c6a4a7e6973740e5 Mon Sep 17 00:00:00 2001 From: Goooler Date: Tue, 26 Nov 2024 17:00:27 +0800 Subject: [PATCH 2/3] Must cast configurations to List to fix type mismatch in runtime --- .../github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt index 2e45233a3..34c35ff6c 100644 --- a/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt +++ b/src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt @@ -328,7 +328,8 @@ public abstract class ShadowJar : private fun configureRelocation() { val packages = mutableSetOf() - configurations.forEach { configuration -> + // Must cast configurations to List to fix type mismatch in runtime. + (configurations as List).forEach { configuration -> configuration.files.forEach { jarFile -> JarFile(jarFile).use { jf -> jf.entries().asSequence().forEach { entry -> From 681313d791c45b6cf548b408d11810576e2531f7 Mon Sep 17 00:00:00 2001 From: Zongle Wang Date: Tue, 26 Nov 2024 17:07:06 +0800 Subject: [PATCH 3/3] Update README.md --- src/docs/changes/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/docs/changes/README.md b/src/docs/changes/README.md index b22c2b02b..3ecfeb4d5 100644 --- a/src/docs/changes/README.md +++ b/src/docs/changes/README.md @@ -10,6 +10,7 @@ **Changed** - **BREAKING CHANGE:** Rewrite this plugin in Kotlin. ([#1012](https://github.com/GradleUp/shadow/pull/1012)) +- **BREAKING CHANGE:** Resolve `Configuration` directly in `DependencyFilter`. ([#1045](https://github.com/GradleUp/shadow/pull/1045)) **Removed**