简体   繁体   中英

NoClassDefFoundError: Maven intermodule dependency

I have my Storm application (maven) project structured as follow:

parent-project/
├── pom.xml
├── storm-application/
    └── pom.xml
├── utils/
    └── pom.xml

I structured my pom.xml files as follow:

  • parent-project:pom.xml
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.project</groupId>
    <artifactId>parent-project</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>utils</module>
        <module>storm-application</module>
    </modules>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
  • storm-application:pom.xml
    <parent>
        <groupId>my.project</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>my.project.parent-project</groupId>
    <artifactId>storm-application</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>my.project.parent-project</groupId>
            <artifactId>utils</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency> ...storm dependency... </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>

        <plugins>
            <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>
        </plugins>

    </build>
  • utils:pom.xml
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>my.project</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>my.project.parent-project</groupId>
    <artifactId>utils</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        ... some project related dependencies ...
    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
    </build>

My objective would be to include the sibling module utils in storm-application .

I need to include the module utils directly in the jar package of the storm-application module.

In fact, if I run the main function directly from IntelliJ it works, because it recognize the project structure, but if I try to deploy the application with Storm I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: my/project/parent-project/utils/MyClass
        at my.project.parent-project.storm-application.MainClass.main(MainClass.java:116)
Caused by: java.lang.ClassNotFoundException: my.project.parent-project.utils.MyClass
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

I even tried to use the maven-assembly-plugin to include all the dependency in the package but I still get the same error.

Update

So what I was missing was that the assembly creates a second jar on the form name-version-jar-with-dependencies .

Plus, the storm dependency should be of <scope>provided</scope> otherwise it complains.

Still, I'm wondering if there would be an easier way to achieve this inter-module dependency.

I found a cleaner way to do so. Instead of using the assembly plugin, I use the shade plugin, so that the fat jar keeps the standard name.

Finally, this is the storm-application/pom.xml that, for me, works:

    <parent>
        <groupId>my.project</groupId>
        <artifactId>parent-project</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <groupId>my.project.parent-project</groupId>
    <artifactId>storm-application</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>my.project.parent-project</groupId>
            <artifactId>utils</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <dependency> ...storm dependency... </dependency>
    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>

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

                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>

    </build>

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