Skip to content

Commit 84acecd

Browse files
authored
Document relocate only trick (#1341)
* Document relocating project resources only * Test `relocateProjectResourcesOnly`
1 parent dbbceb9 commit 84acecd

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

docs/configuration/relocation/README.md

+28
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,31 @@ In versions before 8.1.0 it was necessary to configure a separate `ConfigureShad
127127
in the configurations declared to be shadowed. By default, this is the `runtime` or `runtimeClasspath` configurations.
128128
Be mindful that some Gradle plugins will automatically add dependencies to your class path. You may need to remove these
129129
dependencies if you do not intend to shadow them into your library.
130+
131+
## Relocating Project Resources Only
132+
133+
If you want to relocate the resources of the project only and exclude all dependencies (related to a normal JAR but with
134+
relocating), you can try out the trick like:
135+
136+
=== "Kotlin"
137+
138+
```kotlin
139+
tasks.shadowJar {
140+
// Empty configurations list will exclude all dependencies.
141+
configurations = emptyList()
142+
relocate("com.example", "shadow.com.example")
143+
}
144+
```
145+
146+
=== "Groovy"
147+
148+
```groovy
149+
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
150+
// Empty configurations list will exclude all dependencies.
151+
configurations = []
152+
relocate 'com.example', 'shadow.com.example'
153+
}
154+
```
155+
156+
This is useful in some cases like [#759](https://github.com/GradleUp/shadow/issues/759) mentioned. See
157+
[Configuring Shadowed Dependencies](../dependencies/README.md) for more information about `configurations`.

src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/RelocationTest.kt

+30
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,36 @@ class RelocationTest : BasePluginTest() {
517517
}
518518
}
519519

520+
@Test
521+
fun relocateProjectResourcesOnly() {
522+
val mainClassEntry = writeClass()
523+
projectScriptPath.appendText(
524+
"""
525+
dependencies {
526+
implementation 'junit:junit:3.8.2'
527+
}
528+
$shadowJar {
529+
configurations = []
530+
relocate('', 'foo/')
531+
}
532+
""".trimIndent(),
533+
)
534+
535+
run(shadowJarTask)
536+
537+
assertThat(outputShadowJar).useAll {
538+
containsEntries(
539+
"foo/$mainClassEntry",
540+
"foo/META-INF/MANIFEST.MF",
541+
)
542+
doesNotContainEntries(
543+
"META-INF/MANIFEST.MF",
544+
*junitEntries,
545+
*junitEntries.map { "foo/$it" }.toTypedArray(),
546+
)
547+
}
548+
}
549+
520550
private companion object {
521551
@JvmStatic
522552
fun prefixProvider() = listOf(

0 commit comments

Comments
 (0)