Search code examples
javascalamaven

Creating a fat jar that is also executable with Maven


I would like to create a fat jar that is also executable using maven (pom.xml). I am aware of the plugins that allow the creation of a jar with dependencies and of a jar that contains a manifest but whenever I try to run my jar with dependencies using java -jar target/MyApp-jar-with-dependencies.jar I get the "Manifest not found" error, which I don't get if I try to run the "light" jar.

Here is the pom.xml of my application.

<?xml version="1.0" encoding="UTF-8"?>
<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>my.package</groupId>
    <artifactId>MyApp</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <scala.version.major>2.12</scala.version.major>
        <scala.version.minor>17</scala.version.minor>
        <scala.version>${scala.version.major}.${scala.version.minor}</scala.version>
    </properties>
        
    <dependencies>
        <!-- OpenCV dependencies -->
        <dependency>
            <groupId>org.openpnp</groupId>
            <artifactId>opencv</artifactId>
            <version>4.9.0-0</version>
        </dependency>
    
        <!-- JavaCPP dependencies -->
        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacpp</artifactId>
            <version>1.5.9</version>
        </dependency>

        <!-- Scala library -->
        <!-- https://mvnrepository.com/artifact/org.scala-lang/scala-library -->
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>2.12.17</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <scalaVersion>${scala.version}</scalaVersion>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Fat JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- Build an executable JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>my.package.MyApp</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

EDIT: I already checked this question which helped me creating a jar with dependencies but I am not able to execute it.

EDIT 2: I added the full pom.xml as requested by @khmarbaise


Solution

  • Thanks to @khmarbaise 's comment I was able to solve my issue. I found this answer that explains the difference between Maven plugins used to create jars.

    The plugin I was using to create an executable jar previously, i.e. maven-jar-plugin, only makes the "simple" jar executable and not all the jars produced after compiling my project. To solve my issue:

    1. I removed the maven-jar-plugin;
    2. I configured the main class in the maven-assembly-plugin so that the fat jar created was also executable;
    3. I compiled again the project using mvn clean package.

    The plugin in the pom.xml looks like this:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>3.1.1</version>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>my.package.MyApp</mainClass>
                </manifest>
            </archive>
            <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
            </descriptorRefs>
        </configuration>
        <executions>
            <execution>
                <id>make-assembly</id>
                <phase>package</phase>
                <goals>
                    <goal>single</goal>
                </goals>
            </execution>
        </executions>
    </plugin>