On Maven Wrapper

Let’s say you have built a project using maven.

You used a specific version of maven to build it.

Now you want to send this project to your friend.

To build your project , your friend first needs to install maven in his machine.

And also he might need to use the specific version you used.

What if there is a way to avoid this?

Imagine your friend just downloads your project and start building it right away without installing maven at all.

That is what maven wrapper does!

All you need to do is install maven wrapper in your project and use it to build your project.

Then when you ship your project maven wrapper goes along with it.

Your friend just needs to build the project using maven wrapper instead of maven.

The wrapper will download the version you specified , to your friend’s machine automatically.

Let’s see a practical example.

I created a simple maven project which displays the message “Hello World” to the console.

HelloWorld.java

public class HelloWorld {


    public static void main(String a[]){


        System.out.println("Hello World");
    }
}

To use the wrapper first install it using maven from the project root folder.I ran the below command :

mvn -N io.takari:maven:wrapper

If you want your friend to use a particular version of maven you can specify it to the above command:

mvn -N io.takari:maven:wrapper -Dmaven=3.6.0

That’s it.

Once installed you can see maven wrapper files included in the project:

Now you can use mvnw command instead of mvn command to run all maven commands.

Here is the command to clean and install the project :

mvnw clean install

The project has been built!

Since the maven wrapper gets installed inside the project , you just need to ship your project to your friend and your friend just needs to run maven commands using mvnw keyword instead of mvn. There is no need to install maven!

This idea was borrowed from gradle.

One widely used practical example of maven wrapper:

If you create Spring Boot projects using Spring Initializr , you might have noticed they ship maven wrapper along with the project .So you never need to install maven in your machine.You can start building the project right away!

More on it here:

https://github.com/takari/maven-wrapper


Posted

in

by

Tags:

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading