Skip to content

Commit a2d82f7

Browse files
authored
Fix interaction with Gradle artifact transforms (#1949)
Ports 29cbc56.
1 parent 6bf9d7e commit a2d82f7

File tree

3 files changed

+109
-6
lines changed

3 files changed

+109
-6
lines changed

src/docs/changes/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
**Fixed**
77

88
- Fix compatibility with Isolated Projects. ([#1947](https://github.com/GradleUp/shadow/pull/1947))
9+
- Fix interaction with Gradle artifact transforms. ([#1949](https://github.com/GradleUp/shadow/pull/1949))
910

1011
## [v8.3.10] (2026-02-26)
1112

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ abstract class AbstractDependencyFilter implements DependencyFilter {
2626

2727
@Override
2828
FileCollection resolve(FileCollection configuration) {
29-
Set<ResolvedDependency> includedDeps = []
30-
Set<ResolvedDependency> excludedDeps = []
31-
resolve(configuration.resolvedConfiguration.firstLevelModuleDependencies, includedDeps, excludedDeps)
32-
return project.files(configuration.files) - project.files(excludedDeps.collect {
33-
it.moduleArtifacts*.file
34-
}.flatten())
29+
return configuration - project.files(project.provider {
30+
Set<ResolvedDependency> includedDeps = []
31+
Set<ResolvedDependency> excludedDeps = []
32+
resolve(configuration.resolvedConfiguration.firstLevelModuleDependencies, includedDeps, excludedDeps)
33+
excludedDeps.collectMany {
34+
it.moduleArtifacts.collect { it.file }
35+
}
36+
})
3537
}
3638

3739
@Override

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/ShadowPluginSpec.groovy

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,4 +1054,104 @@ class ShadowPluginSpec extends PluginSpecification {
10541054
JarFile jar = new JarFile(output)
10551055
assert jar.entries().collect().findAll { it.name.endsWith('.class') }.size() == 1
10561056
}
1057+
1058+
@Issue("https://github.com/GradleUp/shadow/issues/882")
1059+
def 'compat gradle artifact transform'() {
1060+
given:
1061+
file('settings.gradle') << "include('app', 'lib')\n"
1062+
file("lib/build.gradle") << """
1063+
plugins {
1064+
id 'java-library'
1065+
}
1066+
""".stripIndent()
1067+
1068+
file("lib/src/main/java/com/company/Utils.java") << """
1069+
package com.company;
1070+
1071+
public class Utils {
1072+
public static void foo() {
1073+
System.out.println("bar");
1074+
}
1075+
}
1076+
""".stripIndent()
1077+
1078+
file("app/build.gradle") << """
1079+
import org.gradle.api.artifacts.transform.TransformParameters
1080+
import org.gradle.api.artifacts.transform.TransformAction
1081+
import org.gradle.api.artifacts.transform.TransformOutputs
1082+
import org.gradle.api.artifacts.transform.InputArtifact
1083+
import org.gradle.api.file.FileSystemLocation
1084+
import org.gradle.api.provider.Provider
1085+
1086+
plugins {
1087+
id 'application'
1088+
id 'com.gradleup.shadow'
1089+
}
1090+
1091+
application {
1092+
mainClass = 'com.company.Main'
1093+
}
1094+
1095+
dependencies {
1096+
implementation project(':lib')
1097+
}
1098+
1099+
def transformedAttribute = Attribute.of('custom-transformed', Boolean)
1100+
1101+
dependencies {
1102+
attributesSchema {
1103+
attribute(transformedAttribute)
1104+
}
1105+
artifactTypes.maybeCreate('jar').attributes.attribute(transformedAttribute, false)
1106+
}
1107+
1108+
dependencies {
1109+
registerTransform(CustomTransformAction) {
1110+
from.attribute(Attribute.of('artifactType', String), 'jar').attribute(transformedAttribute, false)
1111+
to.attribute(Attribute.of('artifactType', String), 'jar').attribute(transformedAttribute, true)
1112+
}
1113+
}
1114+
1115+
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
1116+
configurations = [project.configurations.runtimeClasspath]
1117+
}
1118+
1119+
configurations.runtimeClasspath {
1120+
attributes.attribute(transformedAttribute, true)
1121+
}
1122+
1123+
abstract class CustomTransformAction implements TransformAction<TransformParameters.None> {
1124+
@InputArtifact abstract Provider<FileSystemLocation> getInputArtifact()
1125+
1126+
@Override
1127+
void transform(TransformOutputs outputs) {
1128+
File input = inputArtifact.get().asFile
1129+
File output = outputs.file(input.name)
1130+
output.bytes = input.bytes
1131+
}
1132+
}
1133+
""".stripIndent()
1134+
1135+
file("app/src/main/java/com/company/Main.java") << """
1136+
package com.company;
1137+
1138+
public class Main {
1139+
public static void main(String[] args) {
1140+
Utils.foo();
1141+
}
1142+
}
1143+
""".stripIndent()
1144+
1145+
when:
1146+
runWithSuccess(":app:shadowJar")
1147+
1148+
then:
1149+
File outputJar = getFile("app/build/libs/app-all.jar")
1150+
outputJar.exists()
1151+
contains(outputJar, [
1152+
"com/company/Main.class",
1153+
"com/company/Utils.class",
1154+
"META-INF/MANIFEST.MF"
1155+
])
1156+
}
10571157
}

0 commit comments

Comments
 (0)