Skip to content
On this page

Using mvnw

When using Maven, we typically only use the mvn command. Some of you might have heard of mvnw. What is it?

mvnw is short for Maven Wrapper. By default, when we install Maven, all projects on the system use the globally installed Maven version. However, some projects may require a specific Maven version. In such cases, the Maven Wrapper can be used to install the specified Maven version for that particular project without affecting other projects.

Simply put, the Maven Wrapper provides an independent, specified version of Maven for a project to use.

Installing Maven Wrapper

The easiest way to install the Maven Wrapper is to run the installation command in the root directory of the project (where the pom.xml is located):

bash
$ mvn wrapper:wrapper

This will automatically use the latest version of Maven. If you want to specify the Maven version to use, run the installation command with the version specified, for example, 3.9.0:

bash
$ mvn wrapper:wrapper -Dmaven=3.9.0

After installation, check the project structure:

my-project
├── .mvn
│   └── wrapper
│       └── maven-wrapper.properties
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   └── resources
    └── test
        ├── java
        └── resources

You will notice the addition of mvnw, mvnw.cmd, and the .mvn directory. We only need to replace the mvn command with mvnw to use the Maven version associated with the project. For example:

bash
mvnw clean package

When running on Linux or macOS, you need to prefix it with ./:

bash
$ ./mvnw clean package

Another purpose of the Maven Wrapper is to commit the mvnw, mvnw.cmd, and .mvn directory to the version control repository, ensuring that all developers use the same Maven version.

Exercise

Use mvnw to compile the hello project.

Summary

Using the Maven Wrapper allows you to specify a particular Maven version for a project.

Using mvnw has loaded