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?
answer
- publishing { repositories { maven { } } }
- url required, name optional
- uri("…") wrapper
- RepositoryHandler = upload targets
- not the dependency repositories block
basics
~10 sInside 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 sThe `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 linespublishing {
repositories {
maven {
name = "myCompany"
url = uri("https://repo.example.com/releases")
}
}
}go deeper
Know it goes in publishing { repositories { maven { url = ... } } } and that url is required.
Explain name vs url, the uri() wrapper, and that this handler is upload targets, not dependency sources.
Articulate how name flows into generated task names and credential lookup, and the MavenArtifactRepository type behind maven { }.
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).