skip to content

Distribution Plugin

The distribution plugin's distributions container and the archive and install tasks it generates for each one. Interviewers ask when a project has to ship more than one bundle.

on this pageshow

questions

5

What does applying the Gradle `distribution` plugin give you, and what is the `main` distribution?

level: juniorimportance: must knowfreq 55%

answer

  1. distributions {} NamedDomainObjectContainer
  2. main is the default distribution
  3. distributionBaseName defaults to project name
  4. distZip / distTar / installDist / assembleDist
  5. src/main/dist auto-included

basics

~10 s

Applying the distribution plugin adds a distributions {} container with one default distribution named main. It auto-creates distZip, distTar, and installDist tasks that bundle whatever you put under src/main/dist into an archive or folder.

solid answer

~30 s

The `distribution` plugin (`plugins { id 'distribution' }`) registers a `distributions {}` `NamedDomainObjectContainer` and seeds it with a single distribution called `main`. Every distribution in the container is backed by a `CopySpec` (its `contents`) plus a `distributionBaseName`. For the `main` distribution Gradle generates `distZip`, `distTar`, `installDist`, and an `assembleDist` aggregate. By convention, `src/main/dist` is included automatically in `main`'s contents. The archives land in `build/distributions/` and the exploded install in `build/install/<baseName>`. The plugin is the foundation the `application` plugin builds on — application just configures `main` to add launcher scripts and the runtime classpath.

code

kotlin · 12 lines
kotlin
plugins { id("distribution") }

distributions {
    main {
        distributionBaseName.set("my-app")
        contents {
            from("docs")
            into("docs")
        }
    }
}
// run: ./gradlew distZip   -> build/distributions/my-app-<version>.zip

go deeper

for a junior

Know that the plugin gives you distributions {} with a default main and generates distZip/distTar/installDist.

for a middle

Explain the container is a NamedDomainObjectContainer, that each distribution has a baseName + CopySpec contents, and where outputs land.

for a senior

Articulate how application reuses main, the src/main/dist convention, and the per-distribution task naming scheme.

for a principal

Frame the plugin as a generic packaging primitive and discuss when teams should depend on it directly vs. through application for reproducible release artifacts.

## What the distribution plugin is The `distribution` plugin is a small, focused Gradle core plugin whose job is to package arbitrary files into named, releasable bundles — ZIP archives, TAR archives, or an exploded directory layout. It is deliberately generic: it knows nothing about JVMs or main classes. Higher-level plugins (notably `application`) apply it and configure it. ```kotlin plugins { id("distribution") } ``` ## The `distributions {}` container Applying the plugin adds a project extension: a `NamedDomainObjectContainer<Distribution>` exposed as `distributions {}`. A container is Gradle's pattern for a collection of named, lazily-configured domain objects. The plugin pre-populates this container with exactly one element named **`main`**. Each `Distribution` has two configurable pieces: - **`distributionBaseName`** — a `Property<String>` used as the prefix of generated archive and install names. It defaults to the **project name** for `main`. - **`contents`** — a `CopySpec` describing which files go into the bundle. You configure it like any `CopySpec` (`from`, `into`, `include`, `rename`, ...). ## Tasks generated per distribution For **each** distribution in the container the plugin registers a parallel set of tasks, named after the distribution. For `main` (whose name is elided in task names) you get: - `distZip` — produces `build/distributions/<baseName>[-version].zip` - `distTar` — produces the `.tar` equivalent - `installDist` — produces an exploded copy under `build/install/<baseName>/` - `assembleDist` — a lifecycle task that depends on `distZip` + `distTar` For a non-`main` distribution named e.g. `custom`, the task names are capitalized and infixed: `customDistZip`, `customDistTar`, `installCustomDist`. ## The `src/<name>/dist` convention Gradle automatically wires `src/main/dist` into the `main` distribution's `contents`. So files dropped there appear at the root of the archive with no configuration. ## Where `main` comes from in the application plugin The `application` plugin applies `distribution` and then configures the existing `main` distribution — adding the `bin/` start scripts and `lib/` runtime jars to its `contents`. That is why an application project produces `distZip`/`installDist` for free: it is reusing this plugin's `main` distribution.

  • Where do the `distZip` output and the `installDist` output land by default?
    `distZip` writes `build/distributions/<baseName>[-version].zip`; `installDist` writes an exploded tree under `build/install/<baseName>/`.
  • What is `distributionBaseName` defaulted to for the `main` distribution?
    The project name. You override it with `distributionBaseName.set("...")` inside the `main {}` block.

saying these in an interview costs you the question

  • Claiming the distribution plugin knows about main classes or the runtime classpath — that is the application plugin's job.
  • Saying you must declare `main` yourself; it is pre-created by the plugin.

context

open as a page

How does the distribution plugin name the tasks it generates per distribution, and how does that differ between `main` and a non-`main` distribution?

level: middleimportance: must knowfreq 45%

basics

~10 s

For main the tasks are distZip, distTar, installDist, assembleDist. For any other distribution named <x> Gradle capitalizes and infixes the name: <x>DistZip, <x>DistTar, install<X>Dist. The main name is elided.

open as a page

How does `distributionBaseName` interact with the project version to determine archive file names, and how do you control versionless archive names?

level: middleimportance: should knowfreq 35%

basics

~10 s

The archive name is <distributionBaseName>-<version>.zip. distributionBaseName defaults to the project name and version comes from project.version. If the version is unset (unspecified), the archive is just <baseName>.zip.

open as a page

When would you declare multiple entries in the `distributions {}` container, and what does each extra distribution produce?

level: seniorimportance: should knowfreq 28%

basics

~10 s

Add more entries when one project needs several differently-packaged bundles (e.g. a server and a docs bundle). Each entry gets its own distributionBaseName, its own contents CopySpec, and its own <name>DistZip/<name>DistTar/install<Name>Dist tasks.

open as a page

How does the standalone `distribution` plugin relate to the `application` plugin, and when would you apply `distribution` on its own?

level: seniorimportance: should knowfreq 30%

basics

~20 s

The application plugin applies distribution under the hood and configures the main distribution to add launcher scripts and the runtime classpath. You apply distribution alone when you need to package arbitrary files into ZIP/TAR/install bundles without a runnable Java app.

open as a page