Repositories and Publishing in Gradle
Introduction
Gradle downloads dependencies from repositories and can publish your libraries to mavenLocal, Maven Central, or a company Nexus. This chapter configures mirrors and caches, publishes a JAR with maven-publish, and contrasts the flow with mvn deploy.
Prerequisites
Repository Declarations
Per-project build.gradle:
repositories {
mavenLocal()
mavenCentral()
}Custom or mirror URL:
repositories {
maven {
url = 'https://maven.aliyun.com/repository/public'
}
mavenCentral()
}Centralized in settings.gradle (Gradle 7+):
dependencyResolutionManagement {
repositories {
mavenCentral()
}
}Google Repository (Android / some JVM libs)
repositories {
google()
mavenCentral()
}Refresh and Cache
# Re-resolve changing versions
./gradlew build --refresh-dependencies
# Gradle dependency cache location
# ~/.gradle/caches/modules-2/Build cache (optional speedup):
# gradle.properties
org.gradle.caching=trueCredentials for Private Repositories
build.gradle:
repositories {
maven {
url = 'https://nexus.example.com/repository/maven-releases'
credentials {
username = findProperty('nexusUser') ?: ''
password = findProperty('nexusPassword') ?: ''
}
}
}Pass secrets via ~/.gradle/gradle.properties (not committed):
nexusUser=ci-bot
nexusPassword=secretPublish with maven-publish
build.gradle on a library module:
./gradlew publishToMavenLocalArtifact appears under:
~/.m2/repository/com/example/<artifactId>/1.0.0-SNAPSHOT/Maven and Gradle consumers can depend on the same coordinates.
Publish to Remote Nexus
./gradlew publishMavenJavaPublicationToCompanyNexusRepositoryTask names vary—run ./gradlew tasks --group=publishing to list them.
Signing (Brief)
Maven Central often requires signed artifacts. Use signing plugin with GPG keys—follow repository-specific docs when you publish publicly.
Compare with Maven repositories
| Action | Maven | Gradle |
|---|---|---|
| Local cache | ~/.m2/repository | ~/.gradle/caches + optional mavenLocal() |
| Deploy command | mvn deploy | publish tasks |
| Settings file | ~/.m2/settings.xml mirrors | settings.gradle / gradle.properties |
Proxy Configuration
gradle.properties:
systemProp.http.proxyHost=proxy.corp.example.com
systemProp.http.proxyPort=8080
systemProp.https.proxyHost=proxy.corp.example.com
systemProp.https.proxyPort=8080Mini Example: Consume Your Own Published Module
publishToMavenLocalfrom library project- In another project:
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation 'com.example:my-lib:1.0.0-SNAPSHOT'
}./gradlew build
FAQ
mavenLocal() first or last?
Usually mavenLocal() then mavenCentral() during development; be aware stale snapshots in local repo can confuse resolution.
Same coordinates as Maven project?
Yes—GAV is ecosystem-wide.
Authentication leaks?
Never commit passwords; use CI secret stores and gradle.properties ignored by git.