Sunday, March 21, 2021

Maven Wrapper - What, Why & How

 Maven Wrapper- Why ?

This feature of Maven is borrowed from Gradle

We want to use different version of Maven in my Project than what is installed at my Computer, So we can Use Maven Wrapper
We use it to encapsulate our source code and Maven build system. 
This lets other developers build our code without having Maven pre-installed.
We don’t have to worry about installing the right version of Maven on the CI servers anymore!

2.1. Plugin

mvn -N io.takari:maven:wrapper

We can also specify the version of Maven:

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

The option -N means –non-recursive so that the wrapper will only be applied to the main project of the current directory, not in any submodules.

After executing the goal, we'll have more files and directories in the project:

  • mvnw: it's an executable Unix shell script used in place of a fully installed Maven
  • mvnw.cmd: it's the Batch version of the above script
  • mvn: the hidden folder that holds the Maven Wrapper Java library and its properties file

2.2. Manual

With a manual approach, we can copy files and folders seen above from another project to the main folder of the current project.

Afterwards, we need to specify the version of Maven to use in the wrapper properties file located in .mvn/wrapper/maven-wrapper.properties file


Usage without Binary JAR

By default, the Maven Wrapper JAR archive is added to the using project as small binary file .mvn/wrapper/maven-wrapper.jar. It is used to bootstrap the download and invocation of Maven from the wrapper shell scripts.

If your project is not allowed to contain binary files like this, you can configure your version control system to exclude checkin/commit of the wrapper jar.

If the JAR is not found to be available by the scripts they will attempt to download the file from the URL specified in .mvn/wrapper/maven-wrapper.properties under wrapperUrl and put it in place. The download is attempted via curl, wget and, as last resort, by compiling the ./mvn/wrapper/MavenWrapperDownloader.java file and executing the resulting class.

distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.2/apache-maven-3.5.2-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

No comments:

Post a Comment

Azure - Pipeline - Add Approver for Stage

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/approvals?view=azure-devops&tabs=check-pass