skip to content

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

level: middleimportance: must knowfreq 60%

answer

  1. publish<Pub>PublicationTo<Repo>Repository
  2. publishAllPublicationsTo<Repo>Repository
  3. global publish task aggregates all
  4. name capitalised into task id
  5. tasks --group publishing

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.

solid answer

~40 s

When you declare a publish repository with a `name`, the `maven-publish` plugin reacts by generating upload tasks named from both the publication and the repository. For a repository `myCompany` and a publication `lib` you get `publishLibPublicationToMyCompanyRepository`, and an aggregate `publishAllPublicationsToMyCompanyRepository`. There is also the global `publish` task that depends on every such upload task. The naming pattern is `publish<PublicationName>PublicationTo<RepositoryName>Repository`, with each component capitalised. This combinatorial generation (publications × repositories) is why a clear `name` matters: it makes the right task discoverable in `./gradlew tasks` and unambiguous in CI scripts. ```bash ./gradlew publishLibPublicationToMyCompanyRepository ``` These tasks are lazily registered, so they appear without forcing eager evaluation of the publishing model.

code

bash · 6 lines
bash
# Repo named "myCompany", publication "lib":
./gradlew publishLibPublicationToMyCompanyRepository
# Or push all publications to that repo:
./gradlew publishAllPublicationsToMyCompanyRepository
# Discover the exact names:
./gradlew tasks --group publishing

go deeper

for a junior

Know that running publish uploads, and that there is a task per repository.

for a middle

Reproduce the publish<Pub>PublicationTo<Repo>Repository naming pattern and the aggregate tasks from a name.

for a senior

Explain the publications × repositories cross-product, lazy registration, and why naming repos keeps CI invocations stable.

for a principal

Standardise repository names across the org so a single CI command works uniformly and pipelines aren't coupled to per-project task name drift.

## The generation rule The `maven-publish` plugin walks the cross-product of every declared `MavenPublication` and every declared `MavenArtifactRepository` and **registers** one upload task for each pair. The naming template is: ``` publish<PublicationName>PublicationTo<RepositoryName>Repository ``` Each name segment is capitalised. So publication `lib` + repository `myCompany` → `publishLibPublicationToMyCompanyRepository`. ## Aggregate tasks Gradle also creates conveniences: - `publishAllPublicationsTo<RepositoryName>Repository` — pushes every publication to one repository. - `publish` — the umbrella task that depends on all `publish…To…Repository` tasks (all publications to all repositories). - `publishToMavenLocal` — separate path (sibling topic), targets the local `~/.m2` and is not generated from your declared repositories. ## Why the repository name is load-bearing Because the repository `name` literally becomes part of the task identifier, choosing `name = "myCompany"` versus leaving it default (`maven`) changes whether your CI invokes `publishAllPublicationsToMyCompanyRepository` or `publishAllPublicationsToMavenRepository`. Multiple unnamed repositories collide on the default and Gradle disambiguates with `maven`, `maven2`, … which is brittle; always name them. ## Lazy registration These tasks are created with `tasks.register`-style lazy registration via the plugin, so listing tasks does not force the whole publishing model to realise eagerly. You only pay configuration cost for the task you actually run. ## Discovering them ```bash ./gradlew tasks --group publishing ``` lists every generated task, which is the quickest way to confirm the exact name for a given repository.

  • What does the bare `publish` task do?
    It is the umbrella task depending on every generated `publish…To…Repository` task, so it publishes all publications to all declared repositories at once.
  • If you declare two repositories without names, what happens to the task names?
    Gradle assigns default names `maven`, `maven2`, … so the tasks become `...ToMavenRepository`, `...ToMaven2Repository` — order-dependent and brittle, which is why naming them explicitly is recommended.

saying these in an interview costs you the question

  • Claiming the tasks are eagerly created and slow down configuration — they are lazily registered.
  • Confusing `publishToMavenLocal` (local ~/.m2) with the per-repository generated tasks.

context