Skip to content

Commit 722b9b8

Browse files
authored
Fix the regression of registering ShadowJar tasks without ShadowPlugin applied (#1787)
* Test `registerShadowJarTaskWithoutShadowPluginApplied` * Fix `shadowDependencies` * Update changelog * Cleanups
1 parent f36fadf commit 722b9b8

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

docs/changes/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
## [Unreleased](https://github.com/GradleUp/shadow/compare/9.2.1...HEAD) - 2025-xx-xx
55

6+
## Fixed
7+
8+
- Fix the regression of registering `ShadowJar` tasks without `ShadowPlugin` applied. ([#1787](https://github.com/GradleUp/shadow/pull/1787))
69

710
## [9.2.1](https://github.com/GradleUp/shadow/compare/9.2.0) - 2025-09-24
811

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,14 @@ abstract class BasePluginTest {
144144
plugin: String = "java",
145145
withGroup: Boolean = false,
146146
withVersion: Boolean = false,
147+
applyShadowPlugin: Boolean = true,
147148
): String {
148149
val groupInfo = if (withGroup) "group = 'my'" else ""
149150
val versionInfo = if (withVersion) "version = '1.0'" else ""
150151
return """
151152
plugins {
152153
id '$plugin'
153-
id '$shadowPluginId'
154+
id '$shadowPluginId' apply $applyShadowPlugin
154155
}
155156
$groupInfo
156157
$versionInfo

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,62 @@ class JavaPluginsTest : BasePluginTest() {
604604
)
605605
}
606606

607+
@Issue(
608+
"https://github.com/GradleUp/shadow/issues/1784",
609+
)
610+
@Test
611+
fun registerShadowJarTaskWithoutShadowPluginApplied() {
612+
val mainClassEntry = writeClass(sourceSet = "test", withImports = true)
613+
val testShadowJarTask = "testShadowJar"
614+
projectScript.writeText(
615+
"""
616+
${getDefaultProjectBuildScript(withGroup = true, withVersion = true, applyShadowPlugin = false)}
617+
dependencies {
618+
testImplementation 'junit:junit:3.8.2'
619+
}
620+
def $testShadowJarTask = tasks.register('$testShadowJarTask', ${ShadowJar::class.java.name}) {
621+
description = 'Create a combined JAR of project and test dependencies'
622+
archiveClassifier = 'test'
623+
from sourceSets.named('test').map { it.output }
624+
configurations = project.configurations.named('testRuntimeClasspath').map { [it] }
625+
manifest {
626+
attributes '$mainClassAttributeKey': 'my.Main'
627+
}
628+
}
629+
afterEvaluate {
630+
def hasShadowPlugin = plugins.hasPlugin('${ShadowPlugin::class.qualifiedName}')
631+
def hasShadowBasePlugin = plugins.hasPlugin('${ShadowBasePlugin::class.qualifiedName}')
632+
logger.lifecycle("Has ShadowPlugin: " + hasShadowPlugin)
633+
logger.lifecycle("Has ShadowBasePlugin: " + hasShadowBasePlugin)
634+
}
635+
""".trimIndent(),
636+
)
637+
638+
val result = run(testShadowJarTask)
639+
640+
assertThat(result.output).contains(
641+
"Has ShadowPlugin: false",
642+
"Has ShadowBasePlugin: false",
643+
)
644+
645+
assertThat(jarPath("build/libs/my-1.0-test.jar")).useAll {
646+
containsOnly(
647+
"my/",
648+
mainClassEntry,
649+
*junitEntries,
650+
*manifestEntries,
651+
)
652+
getMainAttr(mainClassAttributeKey).isNotNull()
653+
}
654+
655+
val pathString = path("build/libs/my-1.0-test.jar").toString()
656+
val runningOutput = runProcess("java", "-jar", pathString, "foo")
657+
assertThat(runningOutput).contains(
658+
"Hello, World! (foo) from Main",
659+
"Refs: junit.framework.Test",
660+
)
661+
}
662+
607663
@Issue(
608664
"https://github.com/GradleUp/shadow/issues/443",
609665
)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ import org.gradle.language.base.plugins.LifecycleBasePlugin
6363
@CacheableTask
6464
public abstract class ShadowJar : Jar() {
6565
private val dependencyFilterForMinimize = MinimizeDependencyFilter(project)
66-
private val shadowDependencies = project.provider { project.files(project.configurations.shadow) }
66+
private val shadowDependencies = project.provider {
67+
// Find shadow configuration here instead of get, as the ShadowJar tasks could be registered without Shadow plugin applied.
68+
project.configurations.findByName(ShadowBasePlugin.CONFIGURATION_NAME) ?: project.files()
69+
}
6770

6871
init {
6972
group = LifecycleBasePlugin.BUILD_GROUP

0 commit comments

Comments
 (0)