Tuesday 15 March 2022

quick guide to Maven Wrapper plugin

In this post, I am going to demonstrate the use of Maven wrapper plugin. Maven wrapper plugin is used to distribute Maven software with your project and makes your project more portable.

 

Let me explain with an example to understand this wrapper plugin better.

 

Step 1: Create new maven project ‘wrapper-plugin-demo’. Refer below post for more details

https://self-learning-java-tutorial.blogspot.com/2022/03/create-new-maven-project-in-eclipse.html

 

 

Step 2: Update pom.xml with below content.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.app</groupId>
  <artifactId>wrapper-plugin-demo</artifactId>
  <version>1</version>
  
  <properties>
		<java.version>1.8</java.version>
		<maven.compiler.source>${java.version}</maven.compiler.source>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.report.outputEncoding>UTF-8</project.report.outputEncoding>
	</properties>


	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>2.4.3</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<transformers>
								<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<manifestEntries>
										<Main-Class>com.sample.app.HelloWorld</Main-Class>
										<Build-Number>1.0</Build-Number>
									</manifestEntries>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>


</project>

 

Step 3: Create a package com.sample.app and define HelloWorld class.

 

HelloWorld.java

package com.sample.app;

public class HelloWorld {
	
	public static void main(String[] args) {
		System.out.println("Hello World");
	}

}

 

Total project structure looks like below.

 


 

Let’s add maven wrapper to the project ‘wrapper-plugin-demo’.

 

Navigate to the project directory ‘wrapper-plugin-demo’ and execute below command.

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

 

$mvn -N io.takari:maven:wrapper -Dmaven=3.6.0
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< com.sample.app:wrapper-plugin-demo >-----------------
[INFO] Building wrapper-plugin-demo 1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven:0.7.7:wrapper (default-cli) @ wrapper-plugin-demo ---
[INFO] 
[INFO] Maven Wrapper version 0.5.6 has been successfully set up for your project.
[INFO] Using Apache Maven: 3.6.0
[INFO] Repo URL in properties file: https://repo.maven.apache.org/maven2
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.354 s
[INFO] Finished at: 2022-03-15T15:03:49+05:30
[INFO] ------------------------------------------------------------------------

Once the command executed successfully, you can observe two files (mvnw, mvnw.cmd) and one hidden directory (.mvn) is created in the project.

$ls -lart
total 64
drwxr-xr-x   4 krishna  staff    128 Mar 15 14:55 src
-rw-r--r--   1 krishna  staff   1428 Mar 15 14:57 pom.xml
drwxr-xr-x  24 krishna  staff    768 Mar 15 15:01 ..
-rwxrwxr-x   1 krishna  staff  10069 Mar 15 15:03 mvnw
-rw-rw-r--   1 krishna  staff   6607 Mar 15 15:03 mvnw.cmd
drwxr-xr-x  11 krishna  staff    352 Mar 15 15:03 .
drwxr-xr-x   3 krishna  staff     96 Mar 15 15:03 .mvn

 

Contents of .mvn folder is given below.

$ls .mvn/
wrapper
$
$ls .mvn/wrapper/
MavenWrapperDownloader.java	maven-wrapper.jar		maven-wrapper.properties

 

‘maven-wrapper.properties’ file contain the information about the maven distribution to be used to build this project.

$cat .mvn/wrapper/maven-wrapper.properties 
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar

 

How to build the project?

Navigate to the project directory and execute the command ‘mvnw clean package’.

$./mvnw clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------------< com.sample.app:wrapper-plugin-demo >-----------------
[INFO] Building wrapper-plugin-demo 1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ wrapper-plugin-demo ---
[INFO] Deleting /Users/krishna/eclipse-workspace/wrapper-plugin-demo/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ wrapper-plugin-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ wrapper-plugin-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/krishna/eclipse-workspace/wrapper-plugin-demo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ wrapper-plugin-demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ wrapper-plugin-demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ wrapper-plugin-demo ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ wrapper-plugin-demo ---
[INFO] Building jar: /Users/krishna/eclipse-workspace/wrapper-plugin-demo/target/wrapper-plugin-demo-1.jar
[INFO] 
[INFO] --- maven-shade-plugin:2.4.3:shade (default) @ wrapper-plugin-demo ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing /Users/krishna/eclipse-workspace/wrapper-plugin-demo/target/wrapper-plugin-demo-1.jar with /Users/krishna/eclipse-workspace/wrapper-plugin-demo/target/wrapper-plugin-demo-1-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.589 s
[INFO] Finished at: 2022-03-15T15:09:41+05:30
[INFO] ------------------------------------------------------------------------

You can download the complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/maven-projects/wrapper-plugin-demo

 

References

https://maven.apache.org/wrapper/maven-wrapper-plugin/index.html

 

  

Previous                                                 Next                                                 Home

No comments:

Post a Comment