How to generate Maven Wrapper files (mvnw and mvnw.cmd)
This article shows how to generate the Maven Wrapper files in a Maven project:
mvnw
(For Linux or macOS)mvnw.cmd
(For Windows).mvn/wrapper/maven-wrapper.jar
.mvn/wrapper/maven-wrapper.properties
Table of contents
- 1. What is Maven Wrapper
- 2. Generate the mvnw and mvnw.cmd
- 3. Maven Wrapper in Version Control
- 4. Maven Wrapper with specified Maven version
- 5. References
1. What is Maven Wrapper
The Maven Wrapper ensures our project has everything necessary to run the Maven build by automatically installing and managing a specified version of Maven.
For example, if we push a Maven project to GitHub that we developed using Maven version 3.8.6 and someone else tries to build it locally using an older version like 3.3, the build might fail.
mvn
And we can use Maven Wrapper to fix it; if our project includes the Maven Wrapper (mvnw
for Linux or macOS or mvnw.cmd
for Windows), then the user could use the Maven Wrapper to build the project; The Maven Wrapper automatically downloads and installs the correct Maven version if required, ensuring that the project uses the correct Maven version, regardless of the local Maven installation.
2. Generate the mvnw and mvnw.cmd
Below are the steps to generate the Maven Wrapper files, mvnw
and mvnw.cmd
files.
2.1 Ensure Maven installed
Ensure our system installed the Maven. We can verify this by running:
mvn --version
2.2 Navigate to the Maven project
Navigate to the root directory of the Maven project.
cd maven-project
2.3 Generate Maven Wrapper files
Run the following command to generate Maven Wrapper files.
mvn wrapper:wrapper
The above command will generate the mvnw
, mvnw.cmd
, and .mvn/wrapper/maven-wrapper.jar
and .mvn/wrapper/maven-wrapper.properties
files in the project directory.
2.4 Build with mvnw or mvnw.cmd files
Now, instead of using mvn
to build the project, we can use ./mvnw
(on Linux or macOS) or mvnw.cmd
(on Windows) to build the project.
For Linux or macOS
./mvnw
For Windows
mvnw.cmd
3. Maven Wrapper in Version Control
It’s good practice to commit the following Maven Wrapper files and scripts to the version control (for example, Git); it ensures other developers or CI/CD systems can use the wrapper to build the project without needing a local Maven installation.
Maven Wrapper files to commit to version control:
mvnw
mvnw.cmd
.mvn/wrapper/maven-wrapper.jar
.mvn/wrapper/maven-wrapper.properties
4. Maven Wrapper with specified Maven version
If not specified, The Maven Wrapper default uses a Maven version defined in the project’s pom.xml
or the latest version. We can use the following command to specify a particular version for the wrapper files.
mvn wrapper:wrapper -Dmaven=MAVEN_VERSION
Replace MAVEN_VERSION
with a particular Maven version, like 3.8.6.
Thanks
Thank you