Introduction

Sometimes we 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.

You may like to read Creating Multiple Executable Jars using Maven

Prerequisites

Apache Maven 3.6.0, Java 1.8

Example

We will put our below configurations into the maven build file – pom.xml.

We 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>

We 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 we 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>
    <name>executable jar</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.12</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 we want to append descriptorRef after the finalName in the output jar name -->
              </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Now maven generated executable jar from 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. Thanks for reading.

Tags:

Leave a Reply

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