Skip to content

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

src/docs/publishing/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,42 @@ publishing {
9090
}
9191
}
9292
```
93+
94+
95+
## Publish Custom ShadowJar Task Outputs
96+
97+
It is possible to publish a custom `ShadowJar` task's output via the [`MavenPublication.artifact(java.lang.Object)`](https://docs.gradle.org/current/dsl/org.gradle.api.publish.maven.MavenPublication.html#org.gradle.api.publish.maven.MavenPublication:artifact(java.lang.Object)) method.
98+
99+
```groovy
100+
// Publishing a Shadow JAR with the Maven-Publish Plugin
101+
plugins {
102+
id 'java'
103+
id 'maven-publish'
104+
id 'com.gradleup.shadow'
105+
}
106+
107+
def testShadowJar = tasks.register('testShadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
108+
group = com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin.GROUP_NAME
109+
description = "Create a combined JAR of project and test dependencies"
110+
archiveClassifier = "tests"
111+
from sourceSets.test.output
112+
configurations = [project.configurations.testRuntimeClasspath]
113+
}
114+
115+
dependencies {
116+
testImplementation 'junit:junit:3.8.2'
117+
}
118+
119+
publishing {
120+
publications {
121+
shadow(MavenPublication) {
122+
artifact(testShadowJar)
123+
}
124+
}
125+
repositories {
126+
maven {
127+
url = "https://repo.myorg.com"
128+
}
129+
}
130+
}
131+
```

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import assertk.assertions.isEqualTo
99
import assertk.assertions.single
1010
import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHADOW_RUNTIME_ELEMENTS_CONFIGURATION_NAME
1111
import com.github.jengelman.gradle.plugins.shadow.internal.classPathAttributeKey
12+
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
1213
import com.github.jengelman.gradle.plugins.shadow.util.GradleModuleMetadata
1314
import com.github.jengelman.gradle.plugins.shadow.util.Issue
1415
import com.github.jengelman.gradle.plugins.shadow.util.JarPath
@@ -140,6 +141,37 @@ class PublishingTest : BasePluginTest() {
140141
assertShadowVariantCommon(gmmAdapter.fromJson(repoPath("my/maven/1.0/maven-1.0.module")))
141142
}
142143

144+
@Test
145+
fun publishCustomShadowJar() {
146+
projectScriptPath.appendText(
147+
publishConfiguration(
148+
projectBlock = """
149+
def testShadowJar = tasks.register('testShadowJar', ${ShadowJar::class.java.name}) {
150+
group = com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin.GROUP_NAME
151+
description = "Create a combined JAR of project and test dependencies"
152+
archiveClassifier = "tests"
153+
from sourceSets.test.output
154+
configurations = [project.configurations.testRuntimeClasspath]
155+
}
156+
""".trimIndent(),
157+
dependenciesBlock = """
158+
testImplementation 'junit:junit:3.8.2'
159+
""".trimIndent(),
160+
publicationsBlock = """
161+
shadow(MavenPublication) {
162+
artifact testShadowJar
163+
}
164+
""".trimIndent(),
165+
),
166+
)
167+
168+
publish()
169+
170+
assertThat(repoJarPath("my/maven/1.0/maven-1.0-tests.jar")).useAll {
171+
containsEntries(*junitEntries)
172+
}
173+
}
174+
143175
@ParameterizedTest
144176
@ValueSource(booleans = [false, true])
145177
fun publishShadowedGradlePlugin(legacy: Boolean) {

0 commit comments

Comments
 (0)