NoClassDefFoundError: org/apache/commons/cli/ParseException - Maven Shade Plugin - Fat Jar

88 Views Asked by At

I am attempting to make a fat jar using Maven Shade Plugin, but I am having trouble. Every time I run the shaded jar file, I get this error:

Error: Unable to initialize main class nz.ac.wgtn.swen301.assignment1.cli.StudentManagerUI
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException

I have never used Maven Shade before, so I don't understand why this doesn't include all of the dependencies in the jar.

Here is my pom.xml file for reference:

<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>nz.ac.wgtn.swen301</groupId>
    <artifactId>assignment1</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <name>swen301-assignment1</name>
    <url>http://maven.apache.org</url>

    <dependencies>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.9.2</version>
            <scope>test</scope>
        </dependency>

      <!-- careful upgrading: newer versions seem to have been compiled with Java 17 !! -->
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.14.2.0</version>
      </dependency>

        <!-- to be used for the CLI -->
        <dependency>
            <groupId>commons-cli</groupId>
            <artifactId>commons-cli</artifactId>
            <version>1.5.0</version>
        </dependency>

        <!-- useful for several utilities, including data structures -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>32.1.1-jre</version>
        </dependency>

        <!-- local dependency to setup database -->
        <dependency>
            <groupId>nz.ac.wgtn.swen301</groupId>
            <artifactId>studentdb</artifactId>
            <version>1.3.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/lib/studentdb-1.3.1.jar</systemPath>
        </dependency>
    </dependencies>

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


            <!-- Maven Shade Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.4</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <artifactSet>
                                <includes>
                                    <include>org.apache.commons:commons-lang3</include>
                                    <include>org.apache.commons:commons-cli</include>
                                </includes>
                            </artifactSet>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>nz.ac.wgtn.swen301.assignment1.cli.StudentManagerUI</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project> 

I have looked at multiple iterations of solutions to the java.lang.NoClassDefFoundError: But nothing has helped thus far. I wouldd appreciate any help!

Thanks very much in advance.

1

There are 1 best solutions below

0
g00se On

Remove your artifactSet node and replace it with the following. It's always worked for me:

<filters>
    <filter>
        <artifact>*:*</artifact>
        <excludes>
            <exclude>META-INF/*.SF</exclude>
            <exclude>META-INF/*.DSA</exclude>
            <exclude>META-INF/*.RSA</exclude>
        </excludes>
    </filter>
</filters>