Sunday 13 March 2022

Maven: How to include dependencies in the pom.xml file

We can specify the project dependencies in pom.xml using <dependencies> element.

<dependencies>
	.......
	.......
</dependencies>

You must explicitly specify the groupId, artifactId, version of the dependency jar file. For example, below snippet added gson dependency to my project.

 

<dependencies>

	<dependency>
		<groupId>com.google.code.gson</groupId>
		<artifactId>gson</artifactId>
		<version>${gson.version}</version>
	</dependency>

</dependencies>

Follow below step-by-step procedure to build complete working application.

 

Step 1: Create new maven project ‘maven-add-dependency-demo’ in Eclipse.

 

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 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>maven-add-dependency-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>

		<gson.version>2.9.0</gson.version>
	</properties>

	<dependencies>

		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>${gson.version}</version>
		</dependency>

	</dependencies>

	<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.model and define AppDetails class.

 

AppDetails.java

package com.sample.app.model;

public class AppDetails {
	private String appName;
	private String version;

	public AppDetails() {
	}

	public AppDetails(String appName, String version) {
		this.appName = appName;
		this.version = version;
	}

	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

}

Step 4: Define HelloWorld class in com.sample.app package.

 

HelloWorld.java

package com.sample.app;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.sample.app.model.AppDetails;

public class HelloWorld {

	public static void main(String[] args) {
		AppDetails appDetails = new AppDetails("Chat Server", "1.23.4");

		Gson gson = new GsonBuilder().setPrettyPrinting().create();
		String json = gson.toJson(appDetails);

		System.out.println(json);

	}

}

Total project structure looks like below.




Step 5: Generate the artifact.

 

Go to the directory where pom.xml file is located and execute the command ‘mvn clean package’. It generates maven-add-dependency-demo-1.jar file in target folder.

 

Execute the command ‘java -jar ./target/maven-add-dependency-demo-1.jar’ to run the application.

$ java -jar ./target/maven-add-dependency-demo-1.jar 
{
  "appName": "Chat Server",
  "version": "1.23.4"
}

You can download complete working application from below link.

https://github.com/harikrishna553/java-libs/tree/master/maven-projects/maven-add-dependency-demo



Previous                                                 Next                                                 Home

No comments:

Post a Comment