Skip to content

Commit 187cf15

Browse files
GooolerCopilot
andauthored
Move injecting Class-Path manifest attr logic from doFirst into copy (#1720)
* Replace `doFirst` with `parentJarTask` * Try out `parentManifest` * Add `SerializableManifest` * Remove non-serializable types * Fix CC * Fix `addShadowConfigurationToClassPathInManifest` * Update lint baseline * Clean up `DefaultInheritManifest` * Update changelog * Update docs/changes/README.md Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent 8b6090e commit 187cf15

File tree

4 files changed

+69
-48
lines changed

4 files changed

+69
-48
lines changed

docs/changes/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Merge Gradle Module descriptors into the modern `META-INF` path. ([#1706](https://github.com/GradleUp/shadow/pull/1706))
1515
The Gradle Module descriptors (`org.codehaus.groovy.runtime.ExtensionModule` files) defined under `META-INF/services/`
1616
and `META-INF/groovy` will be merged into `META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule`.
17+
- Move injecting `Class-Path` manifest attr logic from `doFirst` into `copy`. ([#1720](https://github.com/GradleUp/shadow/pull/1720))
1718

1819
### Fixed
1920

gradle/lint-baseline.xml

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<issues format="6" by="lint 8.12.0" type="baseline" client="gradle" dependencies="false" name="AGP (8.12.0)" variant="all" version="8.12.0">
2+
<issues format="6" by="lint 8.13.0" type="baseline" client="gradle" dependencies="false" name="AGP (8.13.0)" variant="all" version="8.13.0">
33

44
<issue
55
id="InternalGradleApiUsage"
@@ -8,7 +8,18 @@
88
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
99
<location
1010
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultInheritManifest.kt"
11-
line="5"
11+
line="6"
12+
column="1"/>
13+
</issue>
14+
15+
<issue
16+
id="InternalGradleApiUsage"
17+
message="Avoid using internal Gradle APIs"
18+
errorLine1="import org.gradle.api.internal.project.DefaultProject"
19+
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
20+
<location
21+
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultInheritManifest.kt"
22+
line="7"
1223
column="1"/>
1324
</issue>
1425

@@ -19,7 +30,7 @@
1930
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
2031
<location
2132
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultInheritManifest.kt"
22-
line="8"
33+
line="10"
2334
column="1"/>
2435
</issue>
2536

@@ -30,18 +41,40 @@
3041
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
3142
<location
3243
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultInheritManifest.kt"
33-
line="9"
44+
line="11"
3445
column="1"/>
3546
</issue>
3647

48+
<issue
49+
id="InternalGradleApiUsage"
50+
message="Avoid using internal Gradle APIs (method get from org.gradle.internal.service.ServiceRegistry)"
51+
errorLine1=" private val fileResolver: FileResolver = (project as DefaultProject).services.get(FileResolver::class.java),"
52+
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
53+
<location
54+
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultInheritManifest.kt"
55+
line="18"
56+
column="44"/>
57+
</issue>
58+
59+
<issue
60+
id="InternalGradleApiUsage"
61+
message="Avoid using internal Gradle APIs (method get from org.gradle.internal.service.ServiceRegistry)"
62+
errorLine1=" private val fileResolver: FileResolver = (project as DefaultProject).services.get(FileResolver::class.java),"
63+
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
64+
<location
65+
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultInheritManifest.kt"
66+
line="18"
67+
column="44"/>
68+
</issue>
69+
3770
<issue
3871
id="InternalGradleApiUsage"
3972
message="Avoid using internal Gradle APIs (method merge from org.gradle.api.java.archives.internal.DefaultManifestMergeSpec)"
4073
errorLine1=" base = it.merge(base, fileResolver)"
4174
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
4275
<location
4376
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/internal/DefaultInheritManifest.kt"
44-
line="31"
77+
line="37"
4578
column="14"/>
4679
</issue>
4780

@@ -96,7 +129,7 @@
96129
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
97130
<location
98131
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt"
99-
line="18"
132+
line="19"
100133
column="1"/>
101134
</issue>
102135

@@ -107,7 +140,7 @@
107140
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
108141
<location
109142
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt"
110-
line="19"
143+
line="20"
111144
column="1"/>
112145
</issue>
113146

@@ -118,7 +151,7 @@
118151
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
119152
<location
120153
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt"
121-
line="20"
154+
line="21"
122155
column="1"/>
123156
</issue>
124157

@@ -129,7 +162,7 @@
129162
errorLine2="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
130163
<location
131164
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt"
132-
line="21"
165+
line="22"
133166
column="1"/>
134167
</issue>
135168

@@ -140,7 +173,7 @@
140173
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
141174
<location
142175
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.kt"
143-
line="53"
176+
line="54"
144177
column="9"/>
145178
</issue>
146179

@@ -166,26 +199,4 @@
166199
column="1"/>
167200
</issue>
168201

169-
<issue
170-
id="InternalGradleApiUsage"
171-
message="Avoid using internal Gradle APIs (method get from org.gradle.internal.service.ServiceRegistry)"
172-
errorLine1=" manifest = DefaultInheritManifest(services.get(FileResolver::class.java))"
173-
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
174-
<location
175-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
176-
line="74"
177-
column="39"/>
178-
</issue>
179-
180-
<issue
181-
id="InternalGradleApiUsage"
182-
message="Avoid using internal Gradle APIs (method get from org.gradle.internal.service.ServiceRegistry)"
183-
errorLine1=" manifest = DefaultInheritManifest(services.get(FileResolver::class.java))"
184-
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
185-
<location
186-
file="src/main/kotlin/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.kt"
187-
line="74"
188-
column="39"/>
189-
</issue>
190-
191202
</issues>

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@ package com.github.jengelman.gradle.plugins.shadow.internal
22

33
import com.github.jengelman.gradle.plugins.shadow.tasks.InheritManifest
44
import org.gradle.api.Action
5+
import org.gradle.api.Project
56
import org.gradle.api.internal.file.FileResolver
7+
import org.gradle.api.internal.project.DefaultProject
68
import org.gradle.api.java.archives.Manifest
79
import org.gradle.api.java.archives.ManifestMergeSpec
810
import org.gradle.api.java.archives.internal.DefaultManifest
911
import org.gradle.api.java.archives.internal.DefaultManifestMergeSpec
1012

11-
internal class DefaultInheritManifest @JvmOverloads constructor(
12-
private val fileResolver: FileResolver,
13-
private val internalManifest: DefaultManifest = DefaultManifest(fileResolver),
13+
internal class DefaultInheritManifest(
14+
project: Project,
15+
manifest: Manifest? = null,
16+
// `AbstractTask.getServices` is protected, we need to get it via `DefaultProject`.
17+
// https://github.com/gradle/gradle/blob/master/subprojects/core/src/main/java/org/gradle/api/internal/AbstractTask.java#L194
18+
private val fileResolver: FileResolver = (project as DefaultProject).services.get(FileResolver::class.java),
19+
private val internalManifest: Manifest = manifest ?: DefaultManifest(fileResolver),
1420
) : InheritManifest,
1521
Manifest by internalManifest {
1622
private val inheritMergeSpecs = mutableListOf<DefaultManifestMergeSpec>()

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ import org.gradle.language.base.plugins.LifecycleBasePlugin
6464
@CacheableTask
6565
public abstract class ShadowJar : Jar() {
6666
private val dependencyFilterForMinimize = MinimizeDependencyFilter(project)
67+
private val shadowDependencies = project.provider { project.files(project.configurations.shadow) }
6768

6869
init {
6970
group = LifecycleBasePlugin.BUILD_GROUP
7071
description = "Create a combined JAR of project and runtime dependencies"
7172

7273
// https://github.com/gradle/gradle/blob/df5bc230c57db70aa3f6909403e5f89d7efde531/platforms/core-configuration/file-operations/src/main/java/org/gradle/api/internal/file/copy/DuplicateHandlingCopyActionDecorator.java#L55-L64
7374
duplicatesStrategy = EXCLUDE
74-
manifest = DefaultInheritManifest(services.get(FileResolver::class.java))
75+
manifest = DefaultInheritManifest(project)
7576

7677
outputs.doNotCacheIf("Has one or more transforms or relocators that are not cacheable") {
7778
transformers.get().any { !it::class.hasAnnotation<CacheableTransformer>() } ||
@@ -369,7 +370,7 @@ public abstract class ShadowJar : Jar() {
369370
}
370371
}
371372
}
372-
injectMultiReleaseAttrIfPresent()
373+
injectManifestAttributes()
373374
super.copy()
374375
}
375376

@@ -450,7 +451,14 @@ public abstract class ShadowJar : Jar() {
450451
}
451452
}
452453

453-
private fun injectMultiReleaseAttrIfPresent() {
454+
private fun injectManifestAttributes() {
455+
val classPathAttr = manifest.attributes[classPathAttributeKey]?.toString().orEmpty()
456+
val shadowFiles = shadowDependencies.get()
457+
if (!shadowFiles.isEmpty) {
458+
val attrs = listOf(classPathAttr) + shadowFiles.map { it.name }
459+
manifest.attributes[classPathAttributeKey] = attrs.joinToString(" ").trim()
460+
}
461+
454462
if (addMultiReleaseAttribute.get()) {
455463
logger.info("Adding $multiReleaseAttributeKey attribute to the manifest if any dependencies contain it.")
456464
} else {
@@ -496,16 +504,11 @@ public abstract class ShadowJar : Jar() {
496504
"module-info.class",
497505
)
498506

499-
@Suppress("EagerGradleConfiguration") // mergeSpec.from hasn't supported lazy configuration yet.
500-
task.manifest.inheritFrom(jarTask.get().manifest)
501-
val classPathAttr = jarTask.map { it.manifest.attributes[classPathAttributeKey]?.toString().orEmpty() }
502-
val shadowFiles = files(configurations.shadow)
503-
task.doFirst("Set $classPathAttributeKey attribute in the manifest") {
504-
if (!shadowFiles.isEmpty) {
505-
val attrs = listOf(classPathAttr.get()) + shadowFiles.map { it.name }
506-
task.manifest.attributes[classPathAttributeKey] = attrs.joinToString(" ").trim()
507-
}
508-
}
507+
task.manifest = DefaultInheritManifest(
508+
project,
509+
@Suppress("EagerGradleConfiguration") // The ctor doesn't support Provider.
510+
jarTask.get().manifest,
511+
)
509512

510513
@Suppress("EagerGradleConfiguration") // Can't use `named` as the task is optional.
511514
tasks.findByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME)?.dependsOn(task)

0 commit comments

Comments
 (0)