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:

gradle
repositories {
    mavenLocal()
    mavenCentral()
}

Custom or mirror URL:

gradle
repositories {
    maven {
        url = 'https://maven.aliyun.com/repository/public'
    }
    mavenCentral()
}

Centralized in settings.gradle (Gradle 7+):

gradle
dependencyResolutionManagement {
    repositories {
        mavenCentral()
    }
}

Google Repository (Android / some JVM libs)

gradle
repositories {
    google()
    mavenCentral()
}

Refresh and Cache

bash
# Re-resolve changing versions
./gradlew build --refresh-dependencies
 
# Gradle dependency cache location
# ~/.gradle/caches/modules-2/

Build cache (optional speedup):

properties
# gradle.properties
org.gradle.caching=true

Credentials for Private Repositories

build.gradle:

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):

properties
nexusUser=ci-bot
nexusPassword=secret

Publish with maven-publish

build.gradle on a library module:

bash
./gradlew publishToMavenLocal

Artifact appears under:

text
~/.m2/repository/com/example/<artifactId>/1.0.0-SNAPSHOT/

Maven and Gradle consumers can depend on the same coordinates.

Publish to Remote Nexus

bash
./gradlew publishMavenJavaPublicationToCompanyNexusRepository

Task 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

ActionMavenGradle
Local cache~/.m2/repository~/.gradle/caches + optional mavenLocal()
Deploy commandmvn deploypublish tasks
Settings file~/.m2/settings.xml mirrorssettings.gradle / gradle.properties

Proxy Configuration

gradle.properties:

properties
systemProp.http.proxyHost=proxy.corp.example.com
systemProp.http.proxyPort=8080
systemProp.https.proxyHost=proxy.corp.example.com
systemProp.https.proxyPort=8080

Mini Example: Consume Your Own Published Module

  1. publishToMavenLocal from library project
  2. In another project:
gradle
repositories {
    mavenLocal()
    mavenCentral()
}
 
dependencies {
    implementation 'com.example:my-lib:1.0.0-SNAPSHOT'
}
  1. ./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.

What comes next?

Advanced topics.