I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

The Maven Wrapper is an excellent choice for projects that need a specific version of Maven (or for users that don’t want to install Maven at all). Instead of installing many versions of it in the operating system, we can just use the project-specific wrapper script.

In this quick article, we’ll show how to set up a Maven Wrapper for an existing Maven project.

2. Setting Up the Maven Wrapper

There’re two ways to configure it in a project, where the simplest one is to use an appropriate plugin to automate it or by applying the manual installation.

2.1. Plugin

Let’s use this Maven Wrapper plugin to make auto installation in a simple Spring Boot project.

First, we need to go in the main folder of the project and run this command:

mvn -N io.takari:maven:wrapper

We can also specify the version of Maven:

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

The option -N means –non-recursive so that the wrapper will only be applied to the main project of the current directory, not in any submodules.

After executing the goal, we’ll have more files and directories in the project:

  • mvnw: it’s an executable Unix shell script used in place of a fully installed Maven
  • mvnw.cmd: it’s the Batch version of the above script
  • mvn: the hidden folder that holds the Maven Wrapper Java library and its properties file

2.2. Manual

With a manual approach, we can copy files and folders seen above from another project to the main folder of the current project.

Afterwards, we need to specify the version of Maven to use in the wrapper properties file located in .mvn/wrapper/maven-wrapper.properties file.

For instance, our properties file has the following line:

distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip

Consequently, the version 3.5.2 will be downloaded and used.

3. Use Cases

The wrapper should work with different operating systems such as:

  • Linux
  • OSX
  • Windows
  • Solaris

After that, we can run our goals like this for the Unix system:

./mvnw clean install

And the following command for Batch:

./mvnw.cmd clean install

If we don’t have the specified Maven in the wrapper properties, it’ll be downloaded and installed in the folder $USER_HOME/.m2/wrapper/dists of the system. 

Let’s run our Spring-Boot project:

./mvnw spring-boot:run

The output is the same as for a fully installed Maven:

Note: we use the executable mvnw in place of mvn, which stands now as the Maven command line program.

4. Conclusion

In this tutorial, we’ve seen how to set up and use Maven Wrapper in a Maven project.

As always, the source code for this article can be found over on GitHub.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS

Leave a Reply

2 Comments on "A Quick Guide to Maven Wrapper"

Notify of
avatar
Sort by:   newest | oldest | most voted
Dusan Odalovic
Guest

Hi, thanks for sharing

What does Batch mean in:

“…And the following command for Batch:…”

Grzegorz Piwowarek
Editor

I’m not sure but it sounds like it’s about https://en.wikibooks.org/wiki/Windows_Batch_Scripting