Gradle Publishing to Nexus¶
TL;DR / when to use this: Publishing libraries to a Sonatype Nexus instance should be a one-line switch between snapshot and release repositories. Drive everything from environment variables and flip publishRepository as needed.
build.gradle¶
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')
}
}
}
}
Version name
If publishing to the releasesRepository, remove -SNAPSHOT from the version — Nexus rejects snapshot-named artifacts in the release repo.
Credentials come from NEXUS_URL, NEXUS_USER, NEXUS_PASSWORD env vars — no secrets in the build file.
See also¶
- Sonatype Nexus — the Nexus instance itself
- Blog post: Organizing repositories and env vars