Quicktip: Use a Maven Wrapper

2 min read

I'm using a Gradle wrapper every time I work with Gradle. That's more or less the de facto standard for me when using Gradle as build automation tool. But when using Maven it's not that common to use a wrapper. Maybe some people never used Gradle and they don't know what a Wrapper is and which problems are solved by using one. That's the reason why I'm writing this blog post. In this blogpost I will describe why and how to use a Maven wrapper.

What is a Maven wrapper and why should I use one?

A Maven wrapper can be used if a Maven build depends on a specific version of Maven or if there are different environments where Maven is used or even when the developers don't want to install Maven on their local machine ;-) By using a Maven wrapper on top of your Maven project your code doesn't depend on a specific Maven version or installation anymore. This means you don't need to download a specific version of Maven and you also don't need to add an environment variable for this Maven version to your classpath. That's nice isn't it? :-)

How do I get started?

The easiest way to add a Maven wrapper to your project is to use the Maven Wrapper plugin. Just cd into your projects folder and call mvn -N io.takari:maven:wrapper. This call adds a .mvn folder containing a wrapper jar, some properties and so on. It also adds two scripts which will replace the mvn ... call via console. You just have to use these generated scripts instead of the mvn calls on your terminal. The scripts can be used the same way maven is used via command line. A clean and install call via Maven wrapper for example would look like ./mvnw clean install. Note: you don't need to add the maven-wrapper.jar to your SCM. It will be downloaded automatically if you don't add it to your SCM. Thanks to Fred Bricon who commented my tweet about it. Forgot to mention it in this blogpost.

Conclusion

By using a Maven wrapper instead of plain mvn command line calls your builds get independent from a specific maven version and specific installations which everyone needs to have on the local machine because everything needed for the Maven build is part of your project.

Hope you learned something new. Have fun with it! :-)

Bye,
Bennet


Be Social, Share!