带有Eclipse Project作为依赖项的Maven包导致NoClassDefFoundError

时间:2018-11-02 09:50:29

标签: java eclipse maven dependencies

这是情况。 我正在为基于历史的CLI进程开发GUI客户端。这些过程均在Eclipse中的Maven项目下。我的GUI是一个单独的Eclipse Maven项目。 GUI项目在其pom.xml中引用了其他项目,mvn package不会失败,但是当我运行项目的...-jar-with-dependencies.jar时,我得到了一个孩子项目类的NoClassDefFoundError

这是我的pom.xml

<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>
  <artifactId>proto-xml-validator</artifactId>
  <organization>
    <name>SGcib</name>
    <url>https://cib.societegenerale.com/en/</url>
  </organization>
  <build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
       <executions>
    <execution>
      <id>make-assembly</id> <!-- this is used for inheritance merges -->
      <phase>package</phase> <!-- bind to the packaging phase -->
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
    </plugin>
  </plugins>
</build>
  <dependencies>
  <!-- https://mvnrepository.com/artifact/com.miglayout/miglayout-swing -->
<dependency>
    <groupId>com.miglayout</groupId>
    <artifactId>miglayout-swing</artifactId>
    <version>5.0</version>
</dependency>

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.11.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.8</version>
  </dependency>
  <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>1.1</version>
    </dependency>
    <dependency>
      <groupId>idea</groupId>
      <artifactId>ideaToolsUtils</artifactId>
      <version>1.2-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>bacardi</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <artifactId>pnl-loader</artifactId>
    </dependency>
</dependencies>
  <groupId>bacardi</groupId>
  <version>0.0.1-SNAPSHOT</version>
</project>

这是堆栈跟踪

$ java -jar target/proto-xml-validator-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: idea/file/loader/runner/ValuationBVLoaderRunner
        at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui.initialize(ProtoXMLValidatorGui.java:55)
        at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui.<init>(ProtoXMLValidatorGui.java:33)
        at com.sgcib.bacardi.tools.gui.ProtoXMLValidatorGui$1.run(ProtoXMLValidatorGui.java:127)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: idea.file.loader.runner.ValuationBVLoaderRunner
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)

触发NoClassDefFoundError的类位于pnl-loader groupId中的bacardi artifactId下。

我已经将Project依赖项的范围设置为compile

有人知道我在做什么错吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

我的问题的解决方案:

问题是,尽管我的Eclipse Maven项目具有正确的依赖项引用,但它无法将其正确打包在JAR中。确实,项目依赖对Maven没有用处(Maven不知道Eclipse项目是什么)。 Maven在他的存储库中寻找编译的JAR。这就是@ aka-one在评论中指出的内容。为了最终打包我的项目,我不得不mvn clean install构建一些必要的项目。