NoClassDefFoundError:Maven模块间依赖性

时间:2019-02-04 13:07:53

标签: java maven dependencies apache-storm

我的Storm应用程序(Maven)项目的结构如下:

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

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

我的目标是在utils中包含兄弟模块storm-application

我需要将模块utils直接包含在jar模块的storm-application包中。

实际上,如果我直接从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

我什至尝试使用maven-assembly-plugin将所有依赖项包含在软件包中,但仍然出现相同的错误。

更新

所以我所缺少的是程序集在jar表单上创建了第二个name-version-jar-with-dependencies

此外,风暴依赖项应为<scope>provided</scope>,否则会抱怨。

还是,我想知道是否会有更简单的方法来实现模块间的依赖性。

1 个答案:

答案 0 :(得分:0)

我找到了一种更清洁的方法。我没有使用Assembly插件,而是使用shade插件,以便胖罐保留标准名称。

最后,对我来说,这是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>