Explain Codes LogoExplain Codes Logo

What is the purpose of mvnw and mvnw.cmd files?

java
build-tool
maven-wrapper
continuous-integration
Anton ShumikhinbyAnton Shumikhin·Nov 27, 2024
TLDR

mvnw and mvnw.cmd act as magic wands that provide a consistent Maven build environment across all machines, all without having to manually install Maven.

// Ready, set, build! ./mvnw package # Unix mvnw.cmd package # Windows

They’re your answer to escaping the notorious "Oops! This does not build as it does on my machine" chaos.

Consistent builds with Maven wrapper

The Maven Wrapper scripts (mvnw for Unix-like systems and mvnw.cmd for Windows) ensure every developer makes use of the same Maven version specified by the project, irrespective of their local Maven setup. Added perks include:

  • Automatic Maven installation: The wrapper scripts download Maven automatically if the correct version isn't found.
  • Version control at project level: The maven-wrapper.properties file can be configured to need a specific Maven version.

This way, you eliminate the hassle of adding Maven's "bin" directory to your system "Path".

Wrapping your Maven commands

Whenever you come across a Maven project equipped with the Maven Wrapper, remember to use the wrapper scripts instead of your own installed 'mvn' command for all Maven related tasks:

  • Running Tests: Execute ./mvnw test or mvnw.cmd test.
  • Cleaning Project: Roll in ./mvnw clean or mvnw.cmd clean to clear the 'target' directory.
  • Installation and Deployment: Unleash ./mvnw install or mvnw.cmd install to install the package into the local repository.

In doing so, you align the execution of Maven commands with the stipulated project version, ensuring build consistency.

Tackling version management

When you want to specifically choose or update the Maven version that is tied to the Maven Wrapper, utilize:

// Simplicity, thy name is Maven Wrapper. mvn -N io.takari:maven:wrapper -Dmaven=3.3.3

As an aside, you can manually tweak the maven-wrapper.properties located in the .mvn/wrapper directory. This file contains the URL for downloading Maven, which can be customized to suit your needs.

Sprucing up the project setup

Kick off a new project with the Maven Wrapper in one easy step:

// Keep calm and initialize Maven Wrapper mvn io.takari:maven:wrapper

If you're dealing with a multi-module project, make sure to add the --non-recursive flag to prevent creating a wrapper for each sub-module.

Cross-platform compatibility

Due to the availability of both mvnw and mvnw.cmd, your project builds are platform-agnostic. So, whether you're working on a Mac in London or a PC in New York, the Maven Wrapper ensures that you have access to the correct Maven version.

Capable of advanced configurations

The .mvn directory can contain additional configuration files. These configuration files extend the capabilities of the Maven Wrapper, opening up a world of possibilities like configuring JVM options, Maven options, and more, thus enhancing consistency and automation within diverse development teams.

CI best practices with Maven Wrapper

Continuous Integration (CI) servers benefit from the Maven Wrapper, thanks to:

  • No Need for Pre-installed Maven: CI servers that use mvnw don't require Maven to be pre-installed.
  • Controlled Upgrades: Maven upgrades can be made with a simple commit.
  • Reproducible Builds: Identical Maven versions on all systems ensure fewer surprises.

Build tool compatibility

Similar to the Maven Wrapper, Gradle provides a mirror tool known as the Gradle Wrapper. Such wrappers are part of a larger trend towards making project builds reproducible and reducing setup times.