skip to content

Declaring Maven Repositories

Declaring named publish targets in the publishing block and the per-repository publish tasks Gradle generates from them. Asked because the generated task name is how CI targets exactly one repository.

on this pageshow

questions

5

How do you declare a target Maven repository to publish your artifacts to in the maven-publish plugin, and what is the minimum you must specify?

level: juniorimportance: must knowfreq 70%

answer

  1. publishing { repositories { maven { } } }
  2. url required, name optional
  3. uri("…") wrapper
  4. RepositoryHandler = upload targets
  5. not the dependency repositories block

basics

~10 s

Inside the publishing { repositories { } } block add a maven { } repository and set its url. Optionally give it a name. That tells Gradle where to upload the published artifacts.

solid answer

~40 s

The `maven-publish` plugin adds a `publishing` extension. Inside it, `repositories { }` is a `RepositoryHandler` where you declare publish *targets* with `maven { }`. The only required property is `url` — where artifacts get uploaded. You can also set `name`, which has two effects: it labels the repository and it determines the name of the generated upload task (`publishTo<Name>Repository`). If you omit `name`, Gradle assigns `maven`/`maven2`/… and credentials lookup falls back to those defaults. ```kotlin publishing { repositories { maven { name = "myCompany" url = uri("https://repo.example.com/releases") } } } ``` Note this `repositories` block is *publish destinations*, distinct from the top-level `repositories { }` used for dependency resolution.

code

kotlin · 8 lines
kotlin
publishing {
  repositories {
    maven {
      name = "myCompany"
      url = uri("https://repo.example.com/releases")
    }
  }
}

go deeper

for a junior

Know it goes in publishing { repositories { maven { url = ... } } } and that url is required.

for a middle

Explain name vs url, the uri() wrapper, and that this handler is upload targets, not dependency sources.

for a senior

Articulate how name flows into generated task names and credential lookup, and the MavenArtifactRepository type behind maven { }.

for a principal

Frame repository declaration as part of a publishing convention plugin shared org-wide so every project targets the same canonical endpoints consistently.

## Where the block lives Applying the `maven-publish` plugin adds a project extension called `publishing` (type `PublishingExtension`). It has two nested containers: - `publications { }` — *what* you publish (e.g. a `MavenPublication` with your jar + POM). - `repositories { }` — *where* you publish it to. This is a `RepositoryHandler`, the same type used by the project-level `repositories { }`, but here the entries are interpreted as **upload destinations**, not dependency sources. ## Declaring a Maven repository Inside `publishing.repositories` you call `maven { }`, which configures a `MavenArtifactRepository`. The two properties that matter most: - **`url`** (required) — the location artifacts are uploaded to. Use `uri("…")` to convert a String/File to a `URI`. It can be an HTTP(S) endpoint or a local/file path (`uri(layout.buildDirectory.dir("repo"))`). - **`name`** (optional but recommended) — a human label that *also* drives the generated task name and the credentials property prefix. ## Why `name` matters For every declared repository Gradle generates a task `publishAllPublicationsTo<Name>Repository` and, per publication, `publish<Pub>PublicationTo<Name>Repository`. So `name = "myCompany"` yields `publishToMyCompanyRepository`-style tasks. A clear name makes those tasks discoverable in `./gradlew tasks` and readable in CI logs. ## Distinct from dependency repositories A frequent confusion: the top-level `repositories { mavenCentral() }` configures where Gradle *downloads* dependencies; `publishing.repositories { maven { } }` configures where Gradle *uploads* your build's outputs. They are separate handlers with separate purposes. ```kotlin plugins { `maven-publish` } publishing { publications { create<MavenPublication>("lib") { from(components["java"]) } } repositories { maven { name = "myCompany" url = uri("https://repo.example.com/releases") } } } ```

  • What is the difference between the top-level `repositories { }` and the one inside `publishing { }`?
    The top-level handler declares sources Gradle resolves/downloads dependencies from; the `publishing` one declares destinations Gradle uploads published artifacts to. Same handler type, opposite direction of data flow.
  • What does setting `name` on the repository affect besides labelling?
    It becomes part of the generated upload task names (`publishTo<Name>Repository`) and the prefix Gradle uses when looking up credentials by convention.

saying these in an interview costs you the question

  • Putting the publish target in the project-level `repositories { }` block instead of inside `publishing { }`.
  • Assigning url a raw String in Kotlin DSL without `uri(...)` (the property expects a URI).

context

open as a page

What publish tasks does Gradle auto-generate per declared Maven repository, and how does the repository name shape them?

level: middleimportance: must knowfreq 60%

basics

~10 s

For each repository named e.g. myCompany, Gradle generates publishAllPublicationsToMyCompanyRepository plus a publish<Pub>PublicationToMyCompanyRepository task per publication. The repository name is capitalised into the task name.

open as a page

How would you route a publish to a release repository for a release version and a snapshot repository for a -SNAPSHOT version using the repository url?

level: middleimportance: should knowfreq 45%

basics

~10 s

Set the repository url conditionally on the version: if version ends with -SNAPSHOT use the snapshot URL, otherwise the releases URL. Often two layout paths under one base host.

open as a page

How can you declare a local file-system Maven repository as a publish target, and when is that useful?

level: middleimportance: should knowfreq 35%

basics

~10 s

Point the repository url at a local directory via uri(layout.buildDirectory.dir("repo")). Gradle writes the full Maven layout there. Useful for testing publication output without hitting a real server.

open as a page

You need internal libraries published to a private repository but releases also mirrored to a public one. How do you model multiple publish repositories and control which build targets which?

level: seniorimportance: should knowfreq 30%

basics

~10 s

Declare multiple named maven { } repositories in publishing.repositories. Each gets its own publishAllPublicationsTo<Name>Repository task, so CI invokes the one(s) appropriate for that build instead of the catch-all publish.

open as a page