Skip to content

Commit

Permalink
Show how to publish the shadowed jar only (GradleUp#1150)
Browse files Browse the repository at this point in the history
* Add publishShadowJarInsteadOfJarWithMavenPublishPlugin test

* Add an example in src/docs/publishing/README.md
  • Loading branch information
Goooler authored Jan 14, 2025
1 parent 73e5b48 commit 82c4557
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/docs/publishing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,45 @@ configuration of the POM file.
This automatic configuration occurs _only_ when using the above methods for
configuring publishing. If this behavior is not desirable, then publishing **must**
be manually configured.


## Publish the Shadowed JAR instead of the Original JAR

You may want to publish the shadowed JAR instead of the original JAR. This can be done by trimming
the `archiveClassifier` of the shadowed JAR like the following:

```groovy
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'com.gradleup.shadow'
group = 'shadow'
version = '1.0'
dependencies {
// This will be bundled in the shadowed JAR and not declared in the POM.
implementation 'some:a:1.0'
// This will be excluded from the shadowed JAR but declared as a runtime dependency in `META-INF/MANIFEST.MF`
// file's `Class-Path` entry, and also in the POM file.
shadow 'some:b:1.0'
// This will be excluded from the shadowed JAR and not declared in the POM or `META-INF/MANIFEST.MF`.
compileOnly 'some:c:1.0'
}
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
archiveClassifier = ''
}
publishing {
publications {
shadow(MavenPublication) {
from components.shadow
}
}
repositories {
maven {
url = "https://repo.myorg.com"
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.github.jengelman.gradle.plugins.shadow.util.Issue
import com.github.jengelman.gradle.plugins.shadow.util.JarPath
import com.github.jengelman.gradle.plugins.shadow.util.containsEntries
import com.github.jengelman.gradle.plugins.shadow.util.doesNotContainEntries
import com.github.jengelman.gradle.plugins.shadow.util.getMainAttr
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
Expand Down Expand Up @@ -62,6 +63,37 @@ class PublishingTest : BasePluginTest() {
assertPomCommon(repoPath("shadow/maven-all/1.0/maven-all-1.0.pom"))
}

@Test
fun publishShadowJarInsteadOfJarWithMavenPublishPlugin() {
projectScriptPath.appendText(
publishConfiguration(
shadowBlock = """
archiveClassifier = ''
""".trimIndent(),
publicationsBlock = """
shadow(MavenPublication) {
from components.shadow
}
""".trimIndent(),
),
)

publish()

assertThat(repoJarPath("shadow/maven/1.0/maven-1.0.jar")).useAll {
containsEntries(
"a.properties",
"a2.properties",
)
doesNotContainEntries(
"b.properties",
)
getMainAttr("Class-Path").isEqualTo("b-1.0.jar")
}
assertPomCommon(repoPath("shadow/maven/1.0/maven-1.0.pom"))
assertShadowVariantCommon(gmmAdapter.fromJson(repoPath("shadow/maven/1.0/maven-1.0.module")))
}

@Issue(
"https://github.com/GradleUp/shadow/issues/860",
"https://github.com/GradleUp/shadow/issues/945",
Expand Down

0 comments on commit 82c4557

Please sign in to comment.