How do you create or upgrade the Maven Wrapper in a project, and what does the wrapper:wrapper goal do?
answer
- mvn wrapper:wrapper
- -Dmaven=<version> pins it
- -Dtype=only-script avoids jar
- needs global mvn once to bootstrap
- re-run to upgrade, commit diff
basics
~10 sRun mvn wrapper:wrapper. It generates mvnw, mvnw.cmd, and .mvn/wrapper files. Add -Dmaven=<version> to pin a specific Maven version. Re-running it with a new version upgrades the wrapper.
solid answer
~40 sThe Maven Wrapper Plugin's `wrapper:wrapper` goal scaffolds and updates the wrapper. `mvn wrapper:wrapper` creates `mvnw`, `mvnw.cmd`, `.mvn/wrapper/maven-wrapper.properties` (and the jar for the jar type). Use `-Dmaven=3.9.6` to pin the Maven distribution version, and `-Dtype=only-script|bin|source` to choose the distribution type — `only-script` avoids committing a binary jar, which many teams prefer. To upgrade, re-run with a new `-Dmaven` value; the goal rewrites `distributionUrl` consistently across the files. Commit the result. Because it's a plugin goal, you don't need the wrapper to already exist — you bootstrap it with a globally installed `mvn` once, then everyone uses `./mvnw` thereafter. Pinning the version makes the upgrade a reviewable one-commit change.
code
bash · 6 lines# Create or upgrade the wrapper, pinned to 3.9.6, no committed jar
mvn wrapper:wrapper -Dmaven=3.9.6 -Dtype=only-script
# Commit so the team and CI pick it up
git add mvnw mvnw.cmd .mvn/
git commit -m "Pin Maven Wrapper to 3.9.6"go deeper
Knows mvn wrapper:wrapper creates the files.
Uses -Dmaven and -Dtype, knows it bootstraps from a global mvn and how to upgrade.
Prefers regenerating over hand-editing, chooses only-script for repo hygiene, reviews the diff.
Standardizes the goal invocation (version + type) in repo templates and upgrade automation across teams.
## The goal The **Maven Wrapper Plugin** (`org.apache.maven.plugins:maven-wrapper-plugin`) exposes the goal **`wrapper:wrapper`**. Running it generates or refreshes all wrapper artifacts in the current project. ## Creating the wrapper ```bash mvn wrapper:wrapper ``` This produces: - `mvnw` and `mvnw.cmd` (the launchers) - `.mvn/wrapper/maven-wrapper.properties` - `.mvn/wrapper/maven-wrapper.jar` (only for the jar/`bin` type) You need a globally installed `mvn` for this *first* invocation; afterwards everyone uses `./mvnw`. ## Pinning a version ```bash mvn wrapper:wrapper -Dmaven=3.9.6 ``` `-Dmaven` sets the Maven version written into `distributionUrl`. Without it, the goal uses a default (often the version running the goal). ## Distribution type ```bash mvn wrapper:wrapper -Dmaven=3.9.6 -Dtype=only-script ``` - **`only-script`** (3.2.0+ wrapper) — no `maven-wrapper.jar` committed; the scripts bootstrap everything. Avoids committing a binary, which is friendly to security scanners and reviewers. - **`bin`** — classic; downloads the full Maven distribution via the jar bootstrap. - **`source`** — builds Maven from source (rare). ## Upgrading Upgrading Maven is just re-running the goal with a new version and committing the diff: ```bash mvn wrapper:wrapper -Dmaven=3.9.9 git add mvnw mvnw.cmd .mvn/ git commit -m "Bump Maven Wrapper to 3.9.9" ``` The goal keeps `distributionUrl` and the scripts consistent — safer than hand-editing the properties file. ## Why use the goal vs. hand-editing Hand-editing only `distributionUrl` works but can leave stale scripts or a mismatched wrapper jar. The goal regenerates everything coherently.
- Do you need an existing wrapper to run wrapper:wrapper?No. It's a plugin goal, so a globally installed mvn bootstraps it once. After that everyone uses ./mvnw.
- What does -Dtype=only-script change?It generates a script-only wrapper with no maven-wrapper.jar committed, so no binary lives in the repo; the scripts handle the bootstrap themselves.
saying these in an interview costs you the question
- Believing you must manually download and place mvnw files instead of using the goal.
- Hand-editing distributionUrl and leaving the scripts/jar stale and mismatched.
- Thinking the wrapper version and the Maven version are the same thing — wrapperVersion is the tooling, -Dmaven is the engine.