diff --git a/src/docs/publishing/README.md b/src/docs/publishing/README.md index 15172841d..53b2de5fa 100644 --- a/src/docs/publishing/README.md +++ b/src/docs/publishing/README.md @@ -90,3 +90,42 @@ publishing { } } ``` + + +## Publish Custom ShadowJar Task Outputs + +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. + +```groovy +// Publishing a Shadow JAR with the Maven-Publish Plugin +plugins { + id 'java' + id 'maven-publish' + id 'com.gradleup.shadow' +} + +def testShadowJar = tasks.register('testShadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) { + group = com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin.GROUP_NAME + description = "Create a combined JAR of project and test dependencies" + archiveClassifier = "tests" + from sourceSets.test.output + configurations = [project.configurations.testRuntimeClasspath] +} + +dependencies { + testImplementation 'junit:junit:3.8.2' +} + +publishing { + publications { + shadow(MavenPublication) { + artifact(testShadowJar) + } + } + repositories { + maven { + url = "https://repo.myorg.com" + } + } +} +``` diff --git a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt index a068fc424..12d3c83a7 100644 --- a/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt +++ b/src/functionalTest/kotlin/com/github/jengelman/gradle/plugins/shadow/PublishingTest.kt @@ -9,6 +9,7 @@ import assertk.assertions.isEqualTo import assertk.assertions.single import com.github.jengelman.gradle.plugins.shadow.ShadowJavaPlugin.Companion.SHADOW_RUNTIME_ELEMENTS_CONFIGURATION_NAME import com.github.jengelman.gradle.plugins.shadow.internal.classPathAttributeKey +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import com.github.jengelman.gradle.plugins.shadow.util.GradleModuleMetadata import com.github.jengelman.gradle.plugins.shadow.util.Issue import com.github.jengelman.gradle.plugins.shadow.util.JarPath @@ -140,6 +141,37 @@ class PublishingTest : BasePluginTest() { assertShadowVariantCommon(gmmAdapter.fromJson(repoPath("my/maven/1.0/maven-1.0.module"))) } + @Test + fun publishCustomShadowJar() { + projectScriptPath.appendText( + publishConfiguration( + projectBlock = """ + def testShadowJar = tasks.register('testShadowJar', ${ShadowJar::class.java.name}) { + group = com.github.jengelman.gradle.plugins.shadow.ShadowBasePlugin.GROUP_NAME + description = "Create a combined JAR of project and test dependencies" + archiveClassifier = "tests" + from sourceSets.test.output + configurations = [project.configurations.testRuntimeClasspath] + } + """.trimIndent(), + dependenciesBlock = """ + testImplementation 'junit:junit:3.8.2' + """.trimIndent(), + publicationsBlock = """ + shadow(MavenPublication) { + artifact testShadowJar + } + """.trimIndent(), + ), + ) + + publish() + + assertThat(repoJarPath("my/maven/1.0/maven-1.0-tests.jar")).useAll { + containsEntries(*junitEntries) + } + } + @ParameterizedTest @ValueSource(booleans = [false, true]) fun publishShadowedGradlePlugin(legacy: Boolean) {