'Gradle: Conditional publish to remote repo or local maven repo

With Gradle 7.1, I am able to publish to a remote repo:

...
publishing {
    publications {
        maven(MavenPublication) {
            from components.java
        }
    }
    repositories {
        maven {
            url = "https://remote-repo.co.uk/nexus/repository/${version.endsWith('-SNAPSHOT') ? "project-snapshots" : "project-releases"}/"
            credentials {
                username "$NEXUS_USERNAME"
                password "$NEXUS_PASSWORD"
            }
        }
    }
}
...

However, for a normal local build

gradle clean build publishToMavenLocal

It complains about the NEXUS credentials. (ie unknown property) Is there anyway I can say build local or build and publish to remote repo and then only care about the credentials? For local build just ignore the credentials section

I suppose I can put a conditional around publishing block, ie:

if (hasProperty('remotePublish') && remotePublish == 'true') {
...
} else {
...
}
  • gradle clean build -PremotePublish=true // remote
  • gradle clean build publishToMavenLocal


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source