Home > Back-end >  Invalid publication 'mavenJava': multiple artifacts with the identical extension and class
Invalid publication 'mavenJava': multiple artifacts with the identical extension and class

Time:01-03

I have multiple ZIP artifacts that I would like to publish with the same artifact ID using maven-publish Gradle (kts) plugin but I keep getting the error:

Execution failed for task ':publishMavenJavaPublicationToMavenLocal'.
> Failed to publish publication 'mavenJava' to repository 'mavenLocal'
   > Invalid publication 'mavenJava': multiple artifacts with the identical extension and classifier ('zip', 'null').

Background:

Consider the following project structure:

.
├── aSrcFiles
│   ├── a-1.txt
│   └── a-2.txt
├── bSrcFiles
│   ├── b-1.txt
│   └── b-2.txt
├── build
│   └── dist
└── build.gradle.kts

And here is the following build.gradle.kts file:

group = "org.demo"
version = "DEV-SNAPSHOT"

plugins { `maven-publish` }
repositories(RepositoryHandler::mavenCentral)

val assembleASrcFiles = tasks.register<Zip>("assembleASrcFiles") {
    archiveFileName.set("demo-${project.version}-aSrcFiles.zip")
    destinationDirectory.set(layout.buildDirectory.dir("dist"))
    from(layout.projectDirectory.dir("aSrcFiles"))
}

val assembleBSrcFiles = tasks.register<Zip>("assembleBSrcFiles") {
    archiveFileName.set("demo-${project.version}-bSrcFiles.zip")
    destinationDirectory.set(layout.buildDirectory.dir("dist"))
    from(layout.projectDirectory.dir("bSrcFiles"))
}

publishing {
    publications {
        create<MavenPublication>("mavenJava") {
            groupId = "org.demo"
            artifactId = "demo"
            artifact(assembleASrcFiles)
            artifact(assembleBSrcFiles)
        }
    }
}

Running maven-publish task publishToMavenLocal with this setup fails to publish to repository 'mavenLocal'. This issue only happens when I introduce another artifact with the same extension (e.g. another ZIP file).

Am I doing anything wrong? Regards.

CodePudding user response:

You need to set the archiveClassifier for each ZIP task:

val assembleASrcFiles = tasks.register<Zip>("assembleASrcFiles") {
    archiveClassifier.set("aSrcFiles")
    // ...
}

val assembleBSrcFiles = tasks.register<Zip>("assembleBSrcFiles") {
    archiveClassifier.set("bSrcFiles")
    // ...
}

From Maven POM Reference:

The classifier distinguishes artifacts that were built from the same POM but differ in content. It is some optional and arbitrary string that - if present - is appended to the artifact name just after the version number.

As a motivation for this element, consider for example a project that offers an artifact targeting Java 11 but at the same time also an artifact that still supports Java 1.8. The first artifact could be equipped with the classifier jdk11 and the second one with jdk8 such that clients can choose which one to use.

Another common use case for classifiers is to attach secondary artifacts to the project's main artifact. If you browse the Maven central repository, you will notice that the classifiers sources and javadoc are used to deploy the project source code and API docs along with the packaged class files.

Now in your other project dependencies, for example, you use them as follows:

dependencies {
    runtimeOnly("org.demo:demo:${version}:aSrcFiles@zip")
    runtimeOnly("org.demo:demo:${version}:bSrcFiles@zip")
}
  • Related