Search code examples
kotlingradlegradle-kotlin-dsl

Gradle: Change artifact names only on publishing to local


I want to append the suffix -LOCAL to my artifact version only when I publish to mavenLocal() using the plugin maven-publish so that when I want to add it to my dependencies of my other project, I can add the dependency like this:

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

The solution that I tried is by concatenating the string at the `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" }
        }
    }
}

But, the change is not dynamic, this will change the version name when I publish to other repository too (probably, I have not tried it yet). I only want to change this only when publishing locally.

How do I go about solving this problem?


Solution

  • you should have 2 publishing artefacts and then conditional publish .. see 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
        }
    }