简体   繁体   中英

Why Executable jar created using maven-assembly-plugin for a multi-module maven project showing classnotfound error

I am trying to create an executable jar for my multi-module maven project. I used the maven-assembly-plugin to generate the jar. Even though am getting the jar created, am getting the ClassNotFound exception while trying to run the jar file using java -jar command.

There is some mistake in you descriptor or how you are using the assembly plugin.

A jar file is like a zip, just open your jar and find your class, if you don't find it then check your pom file or descriptor to understand why.

Use the shade plugin , much easier than assembly.

Parent pom to hold it all together:

<?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>com.greg</groupId>
  <artifactId>fat-jar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>library-jar</module>
    <module>final-jar</module>
  </modules>

</project>

Final build jar pom, with dependency to library:

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

    <parent>
        <artifactId>fat-jar</artifactId>
        <groupId>com.greg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>final-jar</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>library-jar</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.greg.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM