Skip to content

Commit

Permalink
Explicitly add classifier to maven publication (#904)
Browse files Browse the repository at this point in the history
It seems that after [960617f](960617f), archive classifiers are dropped from the archive file name due to the addition of the artifact via `publication.artifact(File)`. According to the [Gradle docs](https://docs.gradle.org/current/javadoc/org/gradle/api/publish/maven/MavenPublication.html#artifact-java.lang.Object-)
> Extension and classifier values are interpolated from the file name.

but for some reason it is not happening in `ShadowExtension`.

This PR provides a workaround by explicitly adding the classifier using the option to pass a `Map` to `MavenPublication#artifact`:
> A Map that contains a 'source' entry that can be resolved as any of the other input types, including file. This map can contain a 'classifier' and an 'extension' entry to further configure the constructed artifact.

---------

Co-authored-by: Zongle Wang <[email protected]>
  • Loading branch information
tha00 and Goooler authored Sep 2, 2024
1 parent 4e18245 commit 3b97870
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
15 changes: 14 additions & 1 deletion src/docs/changes/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Change Log


## [Unreleased]

**Added**

**Changed**

**Fixed**

- Explicitly add classifier to maven publication. ([#904](https://github.com/GradleUp/shadow/pull/904))



## [v8.3.0]

**Changed**
Expand Down Expand Up @@ -364,4 +377,4 @@ Instead, use the `enableRelocation = true` and `relocationPrefix = "<new package
[v8.3.0]: https://github.com/GradleUp/shadow/releases/tag/8.3.0
[v8.1.1]: https://github.com/GradleUp/shadow/releases/tag/8.1.1
[v8.1.0]: https://github.com/GradleUp/shadow/releases/tag/8.1.0
[v8.0.0]: https://github.com/GradleUp/shadow/releases/tag/8.0.0
[v8.0.0]: https://github.com/GradleUp/shadow/releases/tag/8.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import org.gradle.api.Project
import org.gradle.api.artifacts.ProjectDependency
import org.gradle.api.artifacts.SelfResolvingDependency
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.publish.maven.MavenPom
import org.gradle.api.publish.maven.MavenPublication

class ShadowExtension {
private final Provider<Provider<RegularFile>> archiveFile
private final Provider<Archive> archive
private final Provider<List<Dep>> allDependencies

ShadowExtension(Project project) {
archiveFile = project.provider { project.tasks.withType(ShadowJar).getByName("shadowJar").archiveFile }
archive = project.provider {
def archiveTask = project.tasks.withType(ShadowJar).getByName("shadowJar")
new Archive(archiveTask.archiveFile, archiveTask.archiveClassifier)
}
allDependencies = project.provider {
project.configurations.getByName("shadow").allDependencies.collect {
if ((it instanceof ProjectDependency) || !(it instanceof SelfResolvingDependency)) {
Expand All @@ -25,7 +29,10 @@ class ShadowExtension {
}

void component(MavenPublication publication) {
publication.artifact(archiveFile.get())
publication.artifact([
source : archive.get().file,
classifier: archive.get().classifier.get()
])

// Don't inline this variable, it seems Groovy closure capturing is confused by the field instead of a local variable.
final def allDeps = allDependencies
Expand All @@ -43,6 +50,16 @@ class ShadowExtension {
}
}

private class Archive {
Provider<RegularFile> file
Property<String> classifier

Archive(Provider<RegularFile> file, Property<String> classifier) {
this.file = file
this.classifier = classifier
}
}

private class Dep {
String group
String name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,54 @@ class PublishingSpec extends PluginSpecification {
assert dependency.version.text() == '1.0'
}

def "publish shadow jar with maven-publish plugin using custom classifier and extension"() {
given:
repo.module('shadow', 'a', '1.0')
.insertFile('a.properties', 'a')
.insertFile('a2.properties', 'a2')
.publish()
repo.module('shadow', 'b', '1.0')
.insertFile('b.properties', 'b')
.publish()

settingsFile << "rootProject.name = 'maven'"
buildFile << """
apply plugin: 'maven-publish'
dependencies {
implementation 'shadow:a:1.0'
shadow 'shadow:b:1.0'
}
shadowJar {
archiveClassifier = 'my-classifier'
archiveExtension = 'my-ext'
archiveBaseName = 'maven-all'
}
publishing {
publications {
shadow(MavenPublication) { publication ->
project.shadow.component(publication)
artifactId = 'maven-all'
}
}
repositories {
maven {
url "${publishingRepo.uri}"
}
}
}
""".stripIndent()

when:
run('publish')

then:
File publishedFile = publishingRepo.rootDir.file('shadow/maven-all/1.0/maven-all-1.0-my-classifier.my-ext').canonicalFile
assert publishedFile.exists()
}

def "publish multiproject shadow jar with maven-publish plugin"() {
given:

Expand Down

0 comments on commit 3b97870

Please sign in to comment.