Skip to content

Commit e18dde6

Browse files
authored
Expose AbstractDependencyFilter from internal to public (#1538)
1 parent 15b8cb5 commit e18dde6

File tree

6 files changed

+95
-83
lines changed

6 files changed

+95
-83
lines changed

api/shadow.api

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,21 @@ public abstract interface class com/github/jengelman/gradle/plugins/shadow/tasks
159159
public abstract fun resolve (Lorg/gradle/api/artifacts/Configuration;)Lorg/gradle/api/file/FileCollection;
160160
}
161161

162+
public abstract class com/github/jengelman/gradle/plugins/shadow/tasks/DependencyFilter$AbstractDependencyFilter : com/github/jengelman/gradle/plugins/shadow/tasks/DependencyFilter {
163+
public fun <init> (Lorg/gradle/api/Project;Ljava/util/List;Ljava/util/List;)V
164+
public synthetic fun <init> (Lorg/gradle/api/Project;Ljava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
165+
public fun dependency (Ljava/lang/Object;)Lorg/gradle/api/specs/Spec;
166+
public fun exclude (Lorg/gradle/api/specs/Spec;)V
167+
protected final fun getExcludeSpecs ()Ljava/util/List;
168+
protected final fun getIncludeSpecs ()Ljava/util/List;
169+
public fun include (Lorg/gradle/api/specs/Spec;)V
170+
protected final fun isIncluded (Lorg/gradle/api/artifacts/ResolvedDependency;)Z
171+
public fun project (Ljava/lang/Object;)Lorg/gradle/api/specs/Spec;
172+
public fun resolve (Ljava/util/Collection;)Lorg/gradle/api/file/FileCollection;
173+
protected abstract fun resolve (Ljava/util/Set;Ljava/util/Set;Ljava/util/Set;)V
174+
public fun resolve (Lorg/gradle/api/artifacts/Configuration;)Lorg/gradle/api/file/FileCollection;
175+
}
176+
162177
public abstract interface class com/github/jengelman/gradle/plugins/shadow/tasks/InheritManifest : org/gradle/api/java/archives/Manifest {
163178
public fun inheritFrom ([Ljava/lang/Object;)V
164179
public abstract fun inheritFrom ([Ljava/lang/Object;Lorg/gradle/api/Action;)V

docs/changes/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
**Changed**
1616
- Restore Develocity Build Scan integration. ([#1505](https://github.com/GradleUp/shadow/pull/1505))
1717
It is still disabled by default, you can enable it by setting `com.gradleup.shadow.enableDevelocityIntegration = true`.
18+
- Expose `AbstractDependencyFilter` from `internal` to `public`. ([#1538](https://github.com/GradleUp/shadow/pull/1538))
19+
You can access it via `com.github.jengelman.gradle.plugins.shadow.tasks.DependencyFilter.AbstractDependencyFilter`.
1820

1921
## [9.0.0-rc1](https://github.com/GradleUp/shadow/releases/tag/9.0.0-rc1) - 2025-07-02
2022

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/AbstractDependencyFilter.kt

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultDependencyFilter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.github.jengelman.gradle.plugins.shadow.internal
22

3+
import com.github.jengelman.gradle.plugins.shadow.tasks.DependencyFilter
34
import org.gradle.api.Project
45
import org.gradle.api.artifacts.ResolvedDependency
56

67
internal class DefaultDependencyFilter(
78
project: Project,
8-
) : AbstractDependencyFilter(project) {
9+
) : DependencyFilter.AbstractDependencyFilter(project) {
910
override fun resolve(
1011
dependencies: Set<ResolvedDependency>,
1112
includedDependencies: MutableSet<ResolvedDependency>,

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/MinimizeDependencyFilter.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.github.jengelman.gradle.plugins.shadow.internal
22

3+
import com.github.jengelman.gradle.plugins.shadow.tasks.DependencyFilter
34
import org.gradle.api.Project
45
import org.gradle.api.artifacts.ResolvedDependency
56

67
internal class MinimizeDependencyFilter(
78
project: Project,
8-
) : AbstractDependencyFilter(project) {
9+
) : DependencyFilter.AbstractDependencyFilter(project) {
910
override fun resolve(
1011
dependencies: Set<ResolvedDependency>,
1112
includedDependencies: MutableSet<ResolvedDependency>,

src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/DependencyFilter.kt

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
package com.github.jengelman.gradle.plugins.shadow.tasks
22

33
import java.io.Serializable
4+
import org.gradle.api.Project
45
import org.gradle.api.artifacts.Configuration
6+
import org.gradle.api.artifacts.Dependency
7+
import org.gradle.api.artifacts.ProjectDependency
8+
import org.gradle.api.artifacts.ResolvedArtifact
59
import org.gradle.api.artifacts.ResolvedDependency
610
import org.gradle.api.file.FileCollection
11+
import org.gradle.api.provider.Provider
712
import org.gradle.api.specs.Spec
813

914
// DependencyFilter is used as Gradle Input in ShadowJar, so it must be Serializable.
@@ -37,4 +42,73 @@ public interface DependencyFilter : Serializable {
3742
* Create a [Spec] that matches the provided [dependencyNotation].
3843
*/
3944
public fun dependency(dependencyNotation: Any): Spec<ResolvedDependency>
45+
46+
public abstract class AbstractDependencyFilter(
47+
@Transient private val project: Project,
48+
@Transient protected val includeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf(),
49+
@Transient protected val excludeSpecs: MutableList<Spec<ResolvedDependency>> = mutableListOf(),
50+
) : DependencyFilter {
51+
52+
protected abstract fun resolve(
53+
dependencies: Set<ResolvedDependency>,
54+
includedDependencies: MutableSet<ResolvedDependency>,
55+
excludedDependencies: MutableSet<ResolvedDependency>,
56+
)
57+
58+
override fun resolve(configuration: Configuration): FileCollection {
59+
val included = mutableSetOf<ResolvedDependency>()
60+
val excluded = mutableSetOf<ResolvedDependency>()
61+
resolve(configuration.resolvedConfiguration.firstLevelModuleDependencies, included, excluded)
62+
return project.files(configuration.files) -
63+
project.files(excluded.flatMap { it.moduleArtifacts.map(ResolvedArtifact::getFile) })
64+
}
65+
66+
override fun resolve(configurations: Collection<Configuration>): FileCollection {
67+
return configurations.map { resolve(it) }
68+
.reduceOrNull { acc, fileCollection -> acc + fileCollection }
69+
?: project.files()
70+
}
71+
72+
override fun exclude(spec: Spec<ResolvedDependency>) {
73+
excludeSpecs.add(spec)
74+
}
75+
76+
override fun include(spec: Spec<ResolvedDependency>) {
77+
includeSpecs.add(spec)
78+
}
79+
80+
override fun project(notation: Any): Spec<ResolvedDependency> {
81+
@Suppress("UNCHECKED_CAST")
82+
val realNotation = when (notation) {
83+
is ProjectDependency -> return notation.toSpec()
84+
is Provider<*> -> mapOf("path" to notation.get())
85+
is String -> mapOf("path" to notation)
86+
is Map<*, *> -> notation as Map<String, Any>
87+
else -> throw IllegalArgumentException("Unsupported notation type: ${notation::class.java}")
88+
}
89+
return project.dependencies.project(realNotation).toSpec()
90+
}
91+
92+
override fun dependency(dependencyNotation: Any): Spec<ResolvedDependency> {
93+
val realNotation = when (dependencyNotation) {
94+
is Provider<*> -> dependencyNotation.get()
95+
else -> dependencyNotation
96+
}
97+
return project.dependencies.create(realNotation).toSpec()
98+
}
99+
100+
protected fun ResolvedDependency.isIncluded(): Boolean {
101+
val include = includeSpecs.isEmpty() || includeSpecs.any { it.isSatisfiedBy(this) }
102+
val exclude = excludeSpecs.isNotEmpty() && excludeSpecs.any { it.isSatisfiedBy(this) }
103+
return include && !exclude
104+
}
105+
106+
private fun Dependency.toSpec(): Spec<ResolvedDependency> {
107+
return Spec<ResolvedDependency> { resolvedDependency ->
108+
(group == null || resolvedDependency.moduleGroup.matches(group!!.toRegex())) &&
109+
resolvedDependency.moduleName.matches(name.toRegex()) &&
110+
(version == null || resolvedDependency.moduleVersion.matches(version!!.toRegex()))
111+
}
112+
}
113+
}
40114
}

0 commit comments

Comments
 (0)