What we will have in this post ?
- We will create a maven project and add apache shade plug-in.
- Create runnable JAR using shade plug-in.
- Create a class having main method.
- Log information using Log4J logging framework.
Program – create runnable jar using apache shade plugin
- Create a class containing main method.
- We will register main method in shade plugin configuration (refer pom file)
- Log execution information using logger framework.
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
- Added log4j dependencies.
- Added class having main method in mainClass tag (Refer following pom file).
- <mainClass>org.learn.application.Application</mainClass>
<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
- We will execute runnable jar in windows os.
- Build project using maven clean install command.
- Go to the target directory, where we will get output mavenShadeRunnableJar-1.0-jar-with-dependencies.jar
- Execute command java -jar mavenShadeRunnableJar-1.0-jar-with-dependencies.jar
- Go to target directory and execute above command.
- Once we successfully executed the jar, we will get following output
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)