Wrapper

Executing Maven Goals without Installing Maven — ‘Maven Wrapper’

--

Introduction

Maven is a popular build automation tool used primarily for Java projects. One of the challenges of using Maven is that it requires users to have a specific version of Maven installed on their system in order to execute build goals. However, this can be overcome using the Maven Wrapper, which is a script that allows you to run Maven builds without installing Maven on your system.

In this article, we’ll take a look at how to use the Maven Wrapper (https://maven.apache.org/wrapper/), similar to gradle wrapper to generate a script that can be used to run Maven builds. We’ll also discuss some of the benefits of using the Maven Wrapper, and how to execute Maven goals using the generated script.

Generating the Maven Wrapper

To generate the Maven Wrapper script, you can use one of two commands:

mvn -N io.takari:maven:wrapper

or

mvn wrapper:wrapper

The first command uses the io.takari:maven plugin to generate the Maven Wrapper, while the second command uses the wrapper:wrapper goal provided by the Maven Wrapper Plugin.

Note that if you’re using Maven version 3.5.0 or later, you should use the wrapper:wrapper command. If you're using an older version of Maven, you should use the io.takari:maven command.

After running one of these commands, Maven will generate a script called mvnw (or mvnw.cmd on Windows) in the root directory of your project. This script can be used to run Maven builds without installing Maven on your system.

Benefits of using the Maven Wrapper

There are several benefits to using the Maven Wrapper:

  • Consistent builds: Since the Maven Wrapper includes a specific version of Maven, you can ensure that every developer on your team is using the same version of Maven when building the project. This can help avoid issues related to version incompatibility.
  • Ease of use: With the Maven Wrapper, you don’t need to worry about installing or configuring Maven on your system. You can simply run the generated script to execute Maven builds.
  • Portability: Since the Maven Wrapper includes a specific version of Maven, you can easily move your project to a different system without worrying about whether Maven is installed on that system.
  • Version Control: Maven Wrapper is included in the project, which means that it is versioned along with the project. This makes it easier to track changes to the Maven version used in the project and ensures that all developers are using the same version of Maven.

Executing Maven goals with the Maven Wrapper

Once you’ve generated the Maven Wrapper script, you can use it to execute all other Maven goals just like you would with the mvn command. Here are a few examples:

  • To run unit tests: ./mvnw test
  • To package the project as a JAR file: ./mvnw package
  • To install the project into the local repository: ./mvnw install
  • To deploy the project to a remote repository: ./mvnw deploy
  • To clean the project: ./mvnw clean

In general, any Maven goal that you can execute with the mvn the command can also be executed using the Maven Wrapper script with the ./mvnw command. The only difference is that you'll be using the Maven Wrapper script instead of the system-wide installation of Maven.

Conclusion

In this article, we’ve looked at how to use the Maven Wrapper to generate a script that can be used to run Maven builds. We’ve also discussed some of the benefits of using the Maven Wrapper, and how to execute Maven goals using the generated script. By using the Maven Wrapper, you can ensure that every developer on your team is using the same version of Maven, and make it easier to build your projects on different systems.

--

--

Raja Nagendra Kumar, Code Doctor/Innovator

Since 2 decades Nagendra Practices #cleancode as fCTO copilot to boost CTO vision and to handle EM & VPs engineering execution pains/stress due to #techdebt