无效发布 'mavenJava':具有相同扩展名和分类器的多个工件

Invalid publication 'mavenJava': multiple artifacts with the identical extension and classifier

我有多个 ZIP 工件,我想使用 maven-publish Gradle (kts) 插件以相同的工件 ID 发布这些工件,但我不断收到错误消息:

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').

背景:

考虑以下项目结构:

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

这是以下 build.gradle.kts 文件:

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)
        }
    }
}
使用此设置的

运行 maven-publish 任务 publishToMavenLocal 无法发布到存储库 'mavenLocal'。只有当我引入另一个具有相同扩展名的工件(例如另一个 ZIP 文件)时才会发生此问题。

我做错了什么吗?问候。

您需要为每个 ZIP 任务设置 archiveClassifier:

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

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

来自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.

现在在你的其他项目依赖中,例如,你使用它们如下:

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