Maven - Create Executable Jar
In this article we focus on how executable jar file is created in maven project. Usually we want to create java package and run it without specifying classpath of the main class that we want to execute. For this purpose we want to specify application entry point in the Jar manifest as follows.
Manifest-Version: 1.0 Created-By: 1.7.0_06 (Oracle Corporation) Main-Class: MyPackage.MyClassSo that we can execute our jar file in the terminal as follows.
java -jar MyJar.jar
Configuration
We have to configure our pom.xml file and set packaging property as jar.
<groupId>com.udara</groupId> <artifactId>tcp-client</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging>In maven we can configure jar manifest using plugins. Here we talk about few plugins that we can use in this purpose.
Dependency plugin and jar plugin
The dependency plugin provides the capability to manipulate artifacts. It can copy and/or unpack artifacts from local or remote repositories to a specified location. Here I have used dependency plugin to copy artifacts to the libs folder. We can copy specified dependencies by calling,
mvn dependency:copyWe can define goals to execute in lifecycle event inside executions as in the code. In the code I have defined execution phase to execute during package lifecycle event. Any dependencies that are need to include in the jar file should be copied to libs folder. So that output directory is set to ${project.build.directory}/libs. Maven jar plugin is used to build jars. This plugin can be used to customize manifest.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.1.1</version> <configuration> <artifactItems> <artifactItem> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <type>jar</type> </artifactItem> <artifactItem> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <type>jar</type> </artifactItem> </artifactItems> </configuration> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/libs</outputDirectory> <overWriteReleases>false</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <excludeTransitive>true</excludeTransitive> </configuration> </execution> </executions> </plugin>
Apache Maven Assembly Plugin
The maven assembly plugin allow users to aggregate the project output along with its dependencies, modules, site documentation, and other files into a single distributable archive. To assemble modules in to jar file call
mvn assembly:singleAssembly plugin can be bind to maven lifecycle callback. Simply call,
mvn packageto build and assembly the jar file with dependencies.
<plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>3.1.1</version> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>com.udara.App</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <!-- this is used for inheritance merges --> <phase>package</phase> <!-- bind to the packaging phase --> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin>
Apache Maven Shade Plugin
This plugin provides the capability to package the artifact in an uber-jar, including its dependencies and to shade - i.e. rename - the packages of some of the dependencies.
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <filters> <filter> <artifact>org.apache.logging.log4j:log4j-api</artifact> <includes> <include>**</include> </includes> </filter> <filter> <artifact>org.apache.logging.log4j:log4j-core</artifact> <includes> <include>**</include> </includes> </filter> </filters> <minimizeJar>true</minimizeJar> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>com.udara.App</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin>
Comments
Post a Comment