Site icon

Create runnable jar in java using apache shade plugin (maven/example)

What we will have in this post ?

Program – create runnable jar using apache shade plugin

package org.learn.application;

import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {
	private static final Logger logger = LoggerFactory.getLogger(Application.class);

	public static void main(String[] args) {
		logger.info("Starting execution of main method!");
		logger.info("Printing sports list:");
		List<String> listOfSports = Arrays.asList("Soccer", "Badminton", "Tennis", "Rugby", "BaseBall");
		listOfSports.stream().forEach((sport) -> {
			logger.info(sport);

		});
		logger.info("Completed the execution of main method");
	}
}

Maven dependencies (POM) of shade plugin & logger framework

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.learn</groupId>
	<artifactId>mavenShadeRunnableJar</artifactId>
	<version>1.0</version>
	<packaging>jar</packaging>
	<name>mavenShadeRunnableJar</name>
	<url>http://maven.apache.org</url>
	<properties>
		<java_compiler_version>1.8</java_compiler_version>
		<maven_compiler_version>3.5.1</maven_compiler_version>
		<shade_plugin_version>2.4.3</shade_plugin_version>
		<slf_version>1.7.2</slf_version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf_version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf_version}</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<build>		
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>${maven_compiler_version}</version>
				<configuration>
					<source>${java_compiler_version}</source>
					<target>${java_compiler_version}</target>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>${shade_plugin_version}</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<shadedArtifactAttached>true</shadedArtifactAttached>
							<shadedClassifierName>jar-with-dependencies</shadedClassifierName>

							<transformers>
								<transformer
									implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass>org.learn.application.Application</mainClass>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

Example of Log4J.properties is:

log4j.rootLogger=INFO,stdout
log4j.logger.org.learn=INFO

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601}\t[%p] %c [%t] %m%n

Execute runnable jar containing all dependencies

2016-07-03 21:48:22,130	[INFO] org.learn.application.Application [main] Starting execution of main method!
2016-07-03 21:48:22,131	[INFO] org.learn.application.Application [main] Printing sports list:
2016-07-03 21:48:22,217	[INFO] org.learn.application.Application [main] Soccer
2016-07-03 21:48:22,218	[INFO] org.learn.application.Application [main] Badminton
2016-07-03 21:48:22,218	[INFO] org.learn.application.Application [main] Tennis
2016-07-03 21:48:22,218	[INFO] org.learn.application.Application [main] Rugby
2016-07-03 21:48:22,218	[INFO] org.learn.application.Application [main] BaseBall
2016-07-03 21:48:22,218	[INFO] org.learn.application.Application [main] Completed the execution of main method

Download code – create runnable Jar (apache shade Plugin)

 

Exit mobile version