Skip to content

Organizing different repositories and environmental variables

Organizing different publishing libraries should be straightforward in the build.gradle in situations where you might pubish to a snapshot repository vs a release repository.

In the following build.gradle, note that I can simply change the publishRepository to releasesRepository and the artifact will be published accordingly. I don't yet have a sexy solution for having the "SNAPSHOT" at the end of the version, but I will report back if I streamline that further.

Version Name

If publishing to the releasesRepository, remove -SNAPSHOT from the version.

plugins {
    id 'java-library'
    id 'maven-publish'
}

ext {
    nexusUrl = System.getenv('NEXUS_URL')
    snapshotsRepository = "$nexusUrl/maven-snapshots/"
    releasesRepository = "$nexusUrl/maven-releases/"

    // Change the publish repository as needed
    publishRepository = snapshotsRepository
}

version "1.0.0-SNAPSHOT"

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifactId = 'My awesome artifact name'
            from components.java
            artifact sourcesJar
            artifact javadocJar
        }
    }
    repositories {
        maven {
            url publishRepository
            credentials {
                username = System.getenv('NEXUS_USER')
                password = System.getenv('NEXUS_PASSWORD')
            }
        }
    }
}

Comments