Run Your Maven Build Anywhere with the Maven Wrapper

Run Your Maven Build Anywhere with the Maven Wrapper

  • July 29, 2020
Table Of Contents

In this article, we’ll learn about the Maven Wrapper - what problem it solves, how to set it up, and how it works.

Why Do We Need the Maven Wrapper?

Years ago, I was on a team developing a desktop-based Java application. We wanted to share our artifact with a couple of business users in the field to get some feedback. It was unlikely they had Java installed. Asking them to download, install, and configure version 1.2 of Java (yes, this was that long ago!) to run our application would have been a hassle for them.

Looking around trying to find how others had solved this problem, I came across this idea of “bundling the JRE”. The idea was to include within the artifact itself the Java Runtime Environment that our application depended on. Then users don’t need to have a particular version or even any version of Java pre-installed - a neat solution to a specific problem.

Over the years I came across this idea in many places. Today when we containerize our application for cloud deployment, it’s the same general idea: encapsulate the dependent and its dependency into a single unit to hide some complexity.

What’s this got to do with the Maven Wrapper? Replace “business users” with “other developers” and “Java” with “Maven” in my story and it’s the same problem that the Maven Wrapper solves - we use it to encapsulate our source code and Maven build system. This lets other developers build our code without having Maven pre-installed.

The Maven Wrapper makes it easy to build our code on any machine, including CI/CD servers. We don’t have to worry about installing the right version of Maven on the CI servers anymore!

Setting Up the Maven Wrapper

From the project’s root directory (where pom.xml is located), we run this Maven command:

mvn -N io.takari:maven:0.7.7:wrapper

If we wanted to use a particular Maven version, we can specify it like this:

mvn -N io.takari:maven:wrapper -Dmaven=3.6.3

This creates two files (mvnw, mvnw.cmd) and a hidden directory (.mvn). mvnw can be used in Unix-like environments and mvnw.cmd can be used in Windows.

Along with our code, we check in the two files and the .mvn directory and its contents into our source control system like Git. Here’s how other developers can now build the code:

./mvnw clean install

Instead of the usual mvn command, they would use mvnw.

Alternatively, we can set up the wrapper by copying over the mvn, mvnw.cmd files and .mvn directory from an existing project.

Starting from 3.7.0 version of Maven, the Wrapper will be included as a feature within core Maven itself making it even more convenient.

How Does the Maven Wrapper Work?

The .mvn/wrapper directory has a jar file maven-wrapper.jar that downloads the required version of Maven if it’s not already present. It installs it in the ./m2/wrapper/dists directory under the user’s home directory.

Where does it download Maven from? This information is present in the mvn/wrapper/ file:



In this article, we learned what problem the Maven Wrapper solves, how to use it, and how it works. You can read a similar article on this blog on Gradle Wrapper.

Written By:

Saajan Nagendra

Written By:

Saajan Nagendra

Saajan is an architect with deep experience building systems in several business domains. With a clean and minimalist approach to design, he is passionate about code - the aesthetics of it and creating maintainable and flexible solutions. He enjoys both sharing with and learning from others.

Recent Posts

Organizing Code in Node.js Application

Node.js is a popular server-side runtime engine based on JavaScript to build and run web applications. Organizing our source code right from the start is a crucial initial step for building large applications.

Read more

How to Build Responsive Web Apps with Spring Boot and React: A Step-by-Step Guide

Choosing a backend and frontend stack for web apps can be a daunting task, as there are numerous options available for backend (Node.

Read more

Structured Logging with Spring Boot and Amazon CloudWatch

The primary purpose of logging in applications is to debug and trace one or more root causes of an unexpected behavior.

Read more