What does applying the Gradle `distribution` plugin give you, and what is the `main` distribution?
answer
- distributions {} NamedDomainObjectContainer
- main is the default distribution
- distributionBaseName defaults to project name
- distZip / distTar / installDist / assembleDist
- src/main/dist auto-included
basics
~10 sApplying 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 sThe `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 linesplugins { id("distribution") }
distributions {
main {
distributionBaseName.set("my-app")
contents {
from("docs")
into("docs")
}
}
}
// run: ./gradlew distZip -> build/distributions/my-app-<version>.zipgo deeper
Know that the plugin gives you distributions {} with a default main and generates distZip/distTar/installDist.
Explain the container is a NamedDomainObjectContainer, that each distribution has a baseName + CopySpec contents, and where outputs land.
Articulate how application reuses main, the src/main/dist convention, and the per-distribution task naming scheme.
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.