Getting Started with Maven | Java Application Development | Creating an Executable Jar with maven-jar-plugin

In the previous chapter, running the program became simple, but it is still not complete.

A packaged Jar file was created in the target folder, but it does not work if you try to run it as-is. For example, run the following command.

java -jar target/SampleMavenApp-1.0-SNAPSHOT.jar

Then the following error message is printed.

no main manifest attribute, in target/SampleMavenApp-1.0-SNAPSHOT.jar

The Jar file created with mvn package is not an executable Jar. Because the manifest file is not created properly, it cannot be used as-is.

About maven-jar-plugin Settings

Jar files are created by a plugin called maven-jar-plugin. If you add configuration information for creating the manifest file to this plugin, it takes the following form.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>Specify main class</mainClass>
                <addClasspath>true</addClasspath>
                <addExtensions>true</addExtensions>
                <packageName>Package</packageName>
            </manifest>
        </archive>
    </configuration>
</plugin>

Inside <configuration>, where the settings are written, there is an <archive> tag for archive-related settings. Inside it is a <manifest> tag for the manifest file, and the manifest information is written there. The contents are as follows.

Tag Description
<mainClass> Specifies the main class.
<addClasspath> Specifies whether to add the classpath (Class-Path value). Set this to true.
<addExtensions> Includes extension information. This is used to output the library information written in <dependencies>. Set this to true as well.
<packageName> Outputs the Jar package.

For a simple program like SampleMavenApp that does not use any particular libraries, specifying only <mainClass> is enough.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.devkuma.App</mainClass>
                <addClasspath>true</addClasspath>
                <addExtensions>true</addExtensions>
                <packageName>com.devkuma</packageName>
            </manifest>
        </archive>
    </configuration>
</plugin>

Write this in pom.xml, create the Jar file with mvn package, and run SampleMavenApp-1.0-SNAPSHOT.jar with java -jar. This time, it should run without problems.

$ java -jar target/SampleMavenApp-1.0-SNAPSHOT.jar 
Hello World!

Complete pom.xml

The complete pom.xml with the plugin information explained so far is shown below.

<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>com.devkuma</groupId>
  <artifactId>SampleMavenApp</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SampleMavenApp</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>1.6.0</version>
        <configuration>
          <mainClass>com.devkuma.App</mainClass>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <archive>
            <manifest>
                <mainClass>com.devkuma.App</mainClass>
                <addClasspath>true</addClasspath>
                <addExtensions>true</addExtensions>
                <packageName>com.devkuma</packageName>
            </manifest>
          </archive>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

This is the basic code for creating a Java application, running it immediately, or creating an executable Jar file.

If a plugin is no longer needed, you can remove it. For example, when creating an executable Jar, if you do not need to run it with exec:java, you can remove the <plugin> section for exec-maven-plugin. Modify it as needed.