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.
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
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:
Consequently, the version 3.5.2 will be downloaded and used.
3. Use Cases
The wrapper should work with different operating systems such as:
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:
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.
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.