简体   繁体   English

NoClassDefFoundError:Maven模块间依赖

[英]NoClassDefFoundError: Maven intermodule dependency

I have my Storm application (maven) project structured as follow: 我的Storm应用程序(Maven)项目的结构如下:

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

I structured my pom.xml files as follow: 我的pom.xml文件结构如下:

  • 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 . 我的目标是将同级模块utils包含在storm-application

I need to include the module utils directly in the jar package of the storm-application module. 我需要将模块utils直接包含在storm-application模块的jar包中。

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: 实际上,如果我直接从IntelliJ运行主要功能,则它可以工作,因为它可以识别项目结构,但是如果我尝试使用Storm部署应用程序,则会出现此错误:

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. 我什至尝试使用maven-assembly-plugin将所有依赖项包含在包中,但仍然出现相同的错误。

Update 更新资料

So what I was missing was that the assembly creates a second jar on the form name-version-jar-with-dependencies . 所以我所缺少的是该程序集以name-version-jar-with-dependencies的形式创建了另一个jar

Plus, the storm dependency should be of <scope>provided</scope> otherwise it complains. 另外,风暴依赖项应为<scope>provided</scope>否则会抱怨。

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. 我没有使用Assembly插件,而是使用shade插件,以便胖子罐保留标准名称。

Finally, this is the storm-application/pom.xml that, for me, works: 最后,对我来说,这是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-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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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