Showing posts with label maven. Show all posts
Showing posts with label maven. Show all posts

Saturday, 1 October 2022

Create a fat jar using maven assembly plugin

Maven assembly plugin enable developers to generate a fat jar that can contains dependencies, modules, site documentation, and other files.

 

At the time of writing this post, Maven assembly plugin can create distributions in the following formats:

 

a.   zip

b.   tar

c.    tar.gz (or tgz)

d.   tar.bz2 (or tbz2)

e.   tar.snappy

f.     tar.xz (or txz)

g.   jar

h.   dir

i.     war

 

How to configure assembly plugin in pom.xml?

Maven assembly plugin block looks like below.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.6</version>
	<configuration>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<mainClass>com.sample.app.App</mainClass>
			</manifest>
		</archive>
	</configuration>
	<executions>
		<execution>
			<id>assemble-all</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

 

Follow below step-by-step procedure to build a fat jar using assembly plugin.

 

Step 1: Create new maven project ‘assembly-plugin-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

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>assembly-plugin-demo</artifactId>
	<version>1</version>

	<properties>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.9.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>assemble-all</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 

Step 3: Define main application class.

 

App.java

package com.sample.app;

import com.google.gson.Gson;

public class App {
	static class Employee {
		private int id;
		private String name;

		public Employee(int id, String name) {
			this.id = id;
			this.name = name;
		}

	}

	public static void main(String[] args) {
		Employee emp = new Employee(1, "Krishna");
		Gson gson = new Gson();
		System.out.println(gson.toJson(emp));
		
	}
}

 

Total project structure looks like below.



How to get a fat jar from maven project?

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

It generates jar file (assembly-plugin-demo-1-jar-with-dependencies.jar ) in the folder target.

 

How to run the jar file?

java -jar {jar_file}

$java -jar ./target/assembly-plugin-demo-1-jar-with-dependencies.jar 
{"id":1,"name":"Krishna"}

You can download the application from this link.




Previous                                                 Next                                                 Home

Thursday, 29 September 2022

How to execute tests in a maven project?

In this post, I am going to explain various scenarios to execute tests in a maven project.

 

Demo application has three test classes, where each class has one or more test cases.

 


 

Scenario 1: Execute all the tests.

mvn test

Scenario 2: Execute single test class.

mvn -Dtest=TestClass1 test

Above command execute all the tests in TestClass1.

Scenario 3: Execute tests in multiple test classes.

mvn -Dtest=TestClass1,TestClass3 test

Above command execute all the tests in TestClass1, TestClass3.

 

Scenario 4: Run single test case from a test class.

mvn -Dtest=TestClass1#testCase1 test

Scenario 5: Run all the test cases that match to a pattern in a test class.

mvn -Dtest=TestClass1#*2 test

Above snippet run all the test cases where the test case name ends with 2 in TestClass1.

 

You can download this application from this link.



Previous                                                 Next                                                 Home

Sunday, 31 July 2022

How to find the maven installed location in my system?

Approach 1: Execute the command ‘mvn -version’ to find out the location, where Maven is installed in your system.

$mvn -version
Apache Maven 3.8.4 (9b656c72d54e5bacbed989b64718c159fe39b537)
Maven home: /Users/krishna/Documents/softwares/apache-maven-3.8.4
Java version: 1.8.0_311, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home/jre
Default locale: en_GB, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"

 

Property ‘Maven home:’ points to the maven location in your system.

 

Approach 2: If you add the maven bin directory path to your system path, then execute the command ‘which mvn’, it gives you the location of maven bin directory path. This command works in Linux and Mac systems.

$which mvn
/Users/krishna/Documents/softwares/apache-maven-3.8.4/bin/mvn

 

 

Previous                                                 Next                                                 Home

Quick guide to maven local repository

 

Maven support three kinds of repositories.

a.   Local repository: It is a is a directory on the computer where Maven runs. It caches remote downloads and contains temporary build artifacts that you have not yet released.

b.   Remote repository: Custom repository provided by your organization. In general, every organization configures a remote repository, where maven download the dependencies that are not exist in the local repository.

c.    Central repository: Repository provided by Maven community (https://repo1.maven.org/maven2/).

 

 


Typical flow looks like below.

a.   When you build a project that require dependency ‘X’.

b.   Maven check for the dependency ‘X’ in the local repository. If it exist, it build the artefact with given dependency. If the dependency is not exist in the local repository, maven contact the remote repository, download the dependency to local repository, and build the artifact.

 

Location of maven local repository

Default local repository location varies based on the operating system, find the following details.

 

Windows: 

C:\Users\<user_name>\.m2


Linux: 

/home/<user_name>/.m2 or ~/.m2


Mac: 

/Users/<user_name>/.m2 or ~/.m2


Customize maven local repository

You can customize the local repository location by updating the ‘localRepository’ property in the file '{MAVEN_HOME}\conf\settings.xml'.

<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ${user.home}/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->

  <localRepository>/Users/krishna/Documents/my_m2</localRepository>

</settings>

Can I pass my custom local repository location to the maven command?

Yes, you can specify the custom local repository location to the maven command using the argument -Dmaven.repo.local.

mvn -Dmaven.repo.local=/Users/krishna/Documents/my_m2 clean install

References

https://maven.apache.org/guides/introduction/introduction-to-repositories.html


Previous                                                 Next                                                 Home

Friday, 29 July 2022

How to deploy a war file to tomcat?

In this post, I am going to explain how to deploy a war file to Apache tomcat.

 

Prerequisite

Setup maven

Setup Java

 

This tutorial is devided into two sections.

a.   Create a war file

b.   Deploy the war file to tomcat.

 

Step 1: Create a war file.

 

Open terminal and execute below command to generate a sample Maven webapp project.

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4

 

Above command generate the project in interactive mode. Provide below details.

a.   Version as 1

b.   Package as com.sample.app

c.    groupId as com.sample.app

d.   artifactId as helloworld

 

Upon successful completion of the above command, a webapp project template is created.

 

Project structure looks like below.

$tree helloworld/
|____
| |____pom.xml
| |____src
| | |____main
| | | |____webapp
| | | | |____index.jsp
| | | | |____WEB-INF
| | | | | |____web.xml

 

Navigate to the folder helloworld and execute the command ‘mvn clean install’ to generate a war file. Generated war file location is helloworld/target/helloworld.war.

 

Step 2: Deploy the war file to tomcat. You can find the instructions to setup tomcat here.

 

Start the tomcat server. Open the url ‘http://localhost:8080/’ in browser.

 


Click on Manager App button. It prompts you for the login details.

 

 


Enter username and password that you configured, click on Sign In button. You will be taken to ‘Tomcat Web Application Manager’ page.

 

Navigate to 'WAR file to deploy' section in Tomcat Web Application Manager page.




Browse the war file, and click on Deploy button.

 

Once deployment is successful, you can see helloworld application is available in Applications section.

 


 

Click on the hyperlink helloworld, to open the helloworld application.

 


 

That’s it, you are done…


  

Previous                                                 Next                                                 Home