Skip to content

Commit bb77b55

Browse files
authored
Document publishing with custom archive properties (#1389)
* Update `publishShadowJarWithCustomClassifierAndExtension` * Update `applyPlugin` * Update `Configuring Output Name` * Note `Publish the Shadowed JAR with Custom Artifact Name` * Refine
1 parent 08d8445 commit bb77b55

File tree

5 files changed

+142
-20
lines changed

5 files changed

+142
-20
lines changed

docs/configuration/README.md

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,35 @@ details.
1313

1414
## Configuring Output Name
1515

16-
Shadow configures the default `shadowJar` task to set the output JAR's `destinationDirectory`, `archiveBaseName`, `appendix`,
17-
`archiveVersion`, and `extension` to the same default values as Gradle does for all `Jar` tasks.
18-
Additionally, it configures the `archiveClassifier` to be `all`.
16+
Shadow configures the default `shadowJar` task to set the output JAR's
17+
18+
- [`archiveAppendix`](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveAppendix)
19+
- [`archiveBaseName`](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveBaseName)
20+
- [`archiveExtension`](https://docs.gradle.org/current/dsl/org.gradle.jvm.tasks.Jar.html#org.gradle.jvm.tasks.Jar:archiveExtension)
21+
- [`archiveFile`](https://docs.gradle.org/current/dsl/org.gradle.jvm.tasks.Jar.html#org.gradle.jvm.tasks.Jar:archiveFile)
22+
- [`archiveFileName`](https://docs.gradle.org/current/dsl/org.gradle.jvm.tasks.Jar.html#org.gradle.jvm.tasks.Jar:archiveFileName)
23+
- [`archiveVersion`](https://docs.gradle.org/current/dsl/org.gradle.jvm.tasks.Jar.html#org.gradle.jvm.tasks.Jar:archiveVersion)
24+
- [`destinationDirectory`](https://docs.gradle.org/current/dsl/org.gradle.jvm.tasks.Jar.html#org.gradle.jvm.tasks.Jar:destinationDirectory)
25+
26+
to the same default values as Gradle does for all `Jar` tasks.
27+
Additionally, it configures the
28+
[`archiveClassifier`](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html#org.gradle.api.tasks.bundling.Jar:archiveClassifier)
29+
to be `all`. The listed ones are not full, you can view all the properties in
30+
[`Jar`](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Jar.html).
31+
The output shadowed JAR file will be named with the following format:
1932

20-
If working with a Gradle project with the name `myApp` and archiveVersion `1.0`, the default `shadowJar` task will output a
21-
file at: `build/libs/myApp-1.0-all.jar`
33+
```
34+
archiveBaseName-$archiveAppendix-$archiveVersion-$archiveClassifier.$archiveExtension
35+
```
2236

23-
As with all `Jar` tasks in Gradle, these values can be overridden:
37+
If working with a Gradle project with the name `myApp` and version `1.0`, the default `shadowJar` task will output a
38+
file at: `build/libs/myApp-1.0-all.jar`. You can override the properties listed above to change the output name of the
39+
shadowed JAR file. e.g.
2440

2541
=== "Kotlin"
2642

2743
```kotlin
2844
tasks.shadowJar {
29-
archiveBaseName = "shadow"
30-
archiveClassifier = ""
3145
archiveVersion = ""
3246
}
3347
```
@@ -36,12 +50,13 @@ As with all `Jar` tasks in Gradle, these values can be overridden:
3650

3751
```groovy
3852
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
39-
archiveBaseName = 'shadow'
40-
archiveClassifier = ''
4153
archiveVersion = ''
4254
}
4355
```
4456

57+
This will result in the output file being named `myApp-all.jar` instead of `myApp-1.0-all.jar`.
58+
59+
4560
## Configuring the Runtime Classpath
4661

4762
Each Java JAR file contains a manifest file that provides metadata about the contents of the JAR file itself.

docs/configuration/merging/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ which means it honors the `duplicatesStrategy` property as its parent classes do
368368
- `INHERIT`: Uses the same strategy as the parent copy specification.
369369
- `WARN`: Do not attempt to prevent duplicates, but log a warning message when multiple items are to be created at the same path.
370370

371-
You can see more details about them in
371+
see more details about them in
372372
[`DuplicatesStrategy`](https://docs.gradle.org/current/javadoc/org/gradle/api/file/DuplicatesStrategy.html).
373373

374374
`ShadowJar` recognizes `DuplicatesStrategy.INCLUDE` as the default, if you want to change the strategy, you can

docs/publishing/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,76 @@ It is possible to publish a custom `ShadowJar` task's output via the [`MavenPubl
218218
}
219219
}
220220
```
221+
222+
223+
## Publish the Shadowed JAR with Custom Artifact Name
224+
225+
It is possible to configure the artifact name of the shadowed JAR via properties like `archiveBaseName`, see more
226+
customizable properties listed in [Configuring Output Name](../configuration/README.md#configuring-output-name). e.g.
227+
228+
=== "Kotlin"
229+
230+
```kotlin
231+
plugins {
232+
java
233+
`maven-publish`
234+
id("com.gradleup.shadow")
235+
}
236+
237+
group = "my-group"
238+
version = "1.0"
239+
240+
tasks.shadowJar {
241+
archiveClassifier = "my-classifier"
242+
archiveExtension = "my-ext"
243+
archiveBaseName = "maven-all"
244+
}
245+
246+
publishing {
247+
publications {
248+
create<MavenPublication>("shadow") {
249+
from(components["shadow"])
250+
// This will override `archiveBaseName`.
251+
artifactId = "my-artifact"
252+
}
253+
}
254+
repositories {
255+
maven("https://repo.myorg.com")
256+
}
257+
}
258+
```
259+
260+
=== "Groovy"
261+
262+
```groovy
263+
plugins {
264+
id 'java'
265+
id 'maven-publish'
266+
id 'com.gradleup.shadow'
267+
}
268+
269+
group = 'my-group'
270+
version = '1.0'
271+
272+
tasks.named('shadowJar', com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar) {
273+
archiveClassifier = 'my-classifier'
274+
archiveExtension = 'my-ext'
275+
archiveBaseName = 'maven-all'
276+
}
277+
278+
publishing {
279+
publications {
280+
shadow(MavenPublication) {
281+
from components.shadow
282+
// This will override `archiveBaseName`.
283+
artifactId = 'my-artifact'
284+
}
285+
}
286+
repositories {
287+
maven { url = 'https://repo.myorg.com' }
288+
}
289+
}
290+
```
291+
292+
We modified `archiveClassifier`, `archiveExtension` and `archiveBaseName` in this example, the published artifact will
293+
be named `my-artifact-2.0-my-classifier.my-ext` instead of `1.0-all.jar`.

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import kotlin.io.path.name
3434
import kotlin.io.path.outputStream
3535
import kotlin.io.path.writeText
3636
import org.gradle.api.plugins.JavaPlugin
37+
import org.gradle.api.tasks.bundling.Jar
3738
import org.gradle.testfixtures.ProjectBuilder
3839
import org.junit.jupiter.api.Test
3940
import org.junit.jupiter.api.condition.DisabledForJreRange
@@ -60,20 +61,24 @@ class JavaPluginTest : BasePluginTest() {
6061
val shadowTask = project.tasks.getByName(SHADOW_JAR_TASK_NAME) as ShadowJar
6162
val shadowConfig = project.configurations.getByName(ShadowBasePlugin.CONFIGURATION_NAME)
6263

63-
with(shadowTask) {
64+
// Check extended properties.
65+
with(shadowTask as Jar) {
66+
assertThat(archiveAppendix.orNull).isNull()
6467
assertThat(archiveBaseName.get()).isEqualTo(projectName)
65-
assertThat(destinationDirectory.get().asFile)
66-
.isEqualTo(project.layout.buildDirectory.dir("libs").get().asFile)
67-
assertThat(archiveVersion.get()).isEqualTo(version)
6868
assertThat(archiveClassifier.get()).isEqualTo("all")
6969
assertThat(archiveExtension.get()).isEqualTo("jar")
7070
assertThat(archiveFileName.get()).isEqualTo("my-shadow-1.0.0-all.jar")
71+
assertThat(archiveVersion.get()).isEqualTo(version)
7172
assertThat(archiveFile.get().asFile).all {
7273
isEqualTo(destinationDirectory.file(archiveFileName).get().asFile)
7374
isEqualTo(project.projectDir.resolve("build/libs/my-shadow-1.0.0-all.jar"))
7475
}
75-
assertThat(archiveAppendix.orNull).isNull()
76+
assertThat(destinationDirectory.get().asFile)
77+
.isEqualTo(project.layout.buildDirectory.dir("libs").get().asFile)
78+
}
7679

80+
// Check self properties.
81+
with(shadowTask) {
7782
assertThat(minimizeJar.get()).isFalse()
7883
assertThat(enableRelocation.get()).isFalse()
7984
assertThat(relocationPrefix.get()).isEqualTo(ShadowBasePlugin.SHADOW)

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

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,26 +214,55 @@ class PublishingTest : BasePluginTest() {
214214
}
215215

216216
@Issue(
217+
"https://github.com/GradleUp/shadow/issues/614",
217218
"https://github.com/GradleUp/shadow/issues/860",
218219
"https://github.com/GradleUp/shadow/issues/945",
219220
)
220221
@Test
221-
fun publishShadowJarWithCustomClassifierAndExtension() {
222+
fun publishShadowJarWithCustomArtifactName() {
222223
projectScriptPath.appendText(
223224
publishConfiguration(
225+
projectBlock = """
226+
group = 'my-group'
227+
version = '2.0'
228+
""".trimIndent(),
224229
shadowBlock = """
225230
archiveClassifier = 'my-classifier'
226231
archiveExtension = 'my-ext'
227232
archiveBaseName = 'maven-all'
228233
""".trimIndent(),
234+
publicationsBlock = """
235+
shadow(MavenPublication) {
236+
from components.shadow
237+
artifactId = 'my-artifact'
238+
}
239+
""".trimIndent(),
229240
),
230241
)
231242

232243
publish()
233244

234-
assertShadowJarCommon(repoJarPath("my/maven-all/1.0/maven-all-1.0-my-classifier.my-ext"))
235-
assertPomCommon(repoPath("my/maven-all/1.0/maven-all-1.0.pom"))
236-
assertShadowVariantCommon(gmmAdapter.fromJson(repoPath("my/maven-all/1.0/maven-all-1.0.module")))
245+
assertThat(repoPath("my-group/my-artifact/2.0/").listDirectoryEntries().map { it.name }).containsOnly(
246+
"my-artifact-2.0-my-classifier.my-ext.sha512",
247+
"my-artifact-2.0-my-classifier.my-ext",
248+
"my-artifact-2.0.pom.sha256",
249+
"my-artifact-2.0.module",
250+
"my-artifact-2.0.pom",
251+
"my-artifact-2.0.module.sha256",
252+
"my-artifact-2.0.module.sha1",
253+
"my-artifact-2.0.module.md5",
254+
"my-artifact-2.0.pom.sha512",
255+
"my-artifact-2.0-my-classifier.my-ext.sha256",
256+
"my-artifact-2.0.module.sha512",
257+
"my-artifact-2.0-my-classifier.my-ext.sha1",
258+
"my-artifact-2.0-my-classifier.my-ext.md5",
259+
"my-artifact-2.0.pom.md5",
260+
"my-artifact-2.0.pom.sha1",
261+
)
262+
263+
assertShadowJarCommon(repoJarPath("my-group/my-artifact/2.0/my-artifact-2.0-my-classifier.my-ext"))
264+
assertPomCommon(repoPath("my-group/my-artifact/2.0/my-artifact-2.0.pom"))
265+
assertShadowVariantCommon(gmmAdapter.fromJson(repoPath("my-group/my-artifact/2.0/my-artifact-2.0.module")))
237266
}
238267

239268
@Test

0 commit comments

Comments
 (0)