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

Introduction to KDoc

In this article, we’ll discuss all that entails KDoc in Kotlin. KDoc is simply a language used to document code written in Kotlin specifically.

Read more

How to Implement API Rate Limiting in a Node.js Express Application

Have you ever wondered how public API platforms, payment services, or popular websites such as Medium, Twitter, and others manage that their APIs are not overloaded?

Read more

Code Formatting with Ktlint

In this tutorial, we are going to learn about Ktlint, a linting tool for Kotlin. Ktlint checks the style of our code and also helps us to format it against some guidelines.

Read more