How to create Executable jar using Maven

Introduction

Here I am going to show you how to create executable jar using maven build tool. Sometimes you need to create executable jar file from a standalone application using maven because you want to put the executable jar file anywhere and just want to run the file to do some operations from command line.

Related Posts:

Prerequisites

Apache Maven 3.6.3, Java at least 1.8

Executable JAR

You need to put your below configurations into the maven build file – pom.xml.

You need maven compiler plugin to compile the java files. This section should be put under <build> section inside <plugins>.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
    </configuration>
</plugin>

You need maven jar plugin to create a jar file from the application. This section should be put under <build> section inside <plugins>.

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass> main class name with full package </mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <finalName> final output name which will be the jar name </finalName>
    <appendAssemblyId>false</appendAssemblyId> <!-- this indicates whether you want to append descriptorRef after the finalName in the output jar name -->
  </configuration>
</plugin>

Putting all together, the entire pom.xml file is given below. This is an example pom.xml file. You need to adjust the below pom.xml file according to your project name and group name.

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.roytuts</groupId>
    <artifactId>executablejar</artifactId>
    <packaging>jar</packaging>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.13</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
              <artifactId>maven-assembly-plugin</artifactId>
              <configuration>
                <archive>
                  <manifest>
                    <mainClass> main class name with full package </mainClass>
                  </manifest>
                </archive>
                <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName> final output name which will be the jar name </finalName>
                <appendAssemblyId>false</appendAssemblyId> <!-- this indicates whether you want to append descriptorRef after the finalName in the output jar name -->
              </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Now maven generates the executable jar for the application but there is another jar created in the application’s target directory and this jar is created by default by maven build plugin maven-jar-plugin.

If you do not want maven to generate this jar file then you can remove it by overriding maven-jar-plugin.

That’s all about how to create executable jar using maven build tool.

Leave a Reply

Your email address will not be published. Required fields are marked *