Gradle:仅在发布到本地时更改工件名称

Gradle: Change artifact names only on publishing to local

我只想在使用插件 maven-publish 发布到 mavenLocal() 时将后缀 -LOCAL 附加到我的工件版本,这样当我想将它添加到我的依赖项时我的另一个项目, 我可以这样添加依赖项:

dependencies {
    implementation("my.group", "my-jar", "1.0.0-LOCAL")
}

我尝试的解决方案是在 `publishing

处连接字符串
publishing {
    publications {
        create<MavenPublication>("maven") {
            artifacts {
                artifact(tasks["sourcesJar"]) {
                    builtBy(tasks["remapSourcesJar"])
                    version += "LOCAL"
                }

                artifact(tasks["javadocJar"]) {
                    version += "LOCAL"
                }
                artifact(tasks["remapJar"]) {
                    version += "LOCAL"
                }
            }
        }

        repositories {
            mavenLocal()
            maven(url = "some url") { name = "E" }
        }
    }
}

但是,更改不是动态的,这将在我发布时更改版本名称 也到其他存储库(可能我还没有尝试过)。我只想改变 这仅在本地发布时。

我该如何解决这个问题?

您应该有 2 个发布工件,然后有条件发布。请参阅 https://docs.gradle.org/current/userguide/publishing_customization.html

publishing {
    publications {
        mavenLocal(MavenPublication) {
            version += "-LOCAL"
            from components.java
        }
        maven(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            url = uri("$buildDir/repos/releases")
        }
        maven {
            url = uri("$buildDir/repos/snaps")
        }
    }
}

tasks.withType(PublishToMavenRepository) {
    onlyIf {
        publication == publishing.publications.maven
    }
}
tasks.withType(PublishToMavenLocal) {
    onlyIf {
        publication == publishing.publications.mavenLocal
    }
}