简体   繁体   English

Intellij - maven模块依赖。 NoClassDefFound错误

[英]Intellij - maven module dependecy. NoClassDefFound Error

I created a maven base project and it has two modules, A and B . 我创建了一个maven基础项目,它有两个模块, AB In A i added B as a dependency and also added JSoup from maven in B via Project Structure . A i中,我添加了B作为依赖关系,并通过Project StructureB添加了来自maven的JSoup。

Now i want to run these following codes A: 现在我想运行以下代码A:

public class A {
    public static void main(String args[]){
        new B().foo();
    }
}

B: B:

public class B {
    public void foo() {
        System.out.println("B....");//prints B...
        new Document("S");// NoClassDefFoundError exception
    }
}

When i run the following code it says: 当我运行以下代码时,它说:

Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/nodes/Document

Jsoup only added into B.iml not B.pom file: Jsoup只添加到B.iml而不是B.pom文件中:

<orderEntry type="library" exported="" name="org.jsoup:jsoup:1.8.3" level="project" />

I can solve this problem by adding jsoup dependency to A but i think it's not a good idea. 我可以通过向A添加jsoup依赖来解决这个问题,但我认为这不是一个好主意。

Is there any way that A recognizes all B 's dependencies? A有什么方法可以识别所有B的依赖关系?

Root pom: 根pom:

<?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>xxx</groupId>
    <artifactId>xxx</artifactId>
    <packaging>pom</packaging>
    <version>0.5-SNAPSHOT</version>
    <modules>
        <module>A</module>
        <module>B</module>
    </modules>


</project>

A's pom 一个人的pom

<?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">
    <parent>
        <artifactId>xxx</artifactId>
        <groupId>xxx</groupId>
        <version>0.5-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>A</artifactId>


</project>

B's pom: B的pom:

<?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">
    <parent>
        <artifactId>xxx</artifactId>
        <groupId>xxx</groupId>
        <version>0.5-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>B</artifactId>


</project>

In A i added B as a dependency 在A中我添加了B作为依赖

So needs to add this dependency to A 's pom file too 因此需要将此依赖项添加到A的pom文件中

 <dependencies>
        <dependency>
            <groupId>xxx</groupId>
            <artifactId>B</artifactId>
            <version>0.5-SNAPSHOT</version>
        </dependency>

    </dependencies>

and also added JSoup from maven in B 并在B中添加了来自maven的JSoup

Also you should add this dependency to B 's pom file 您还应该将此依赖项添加到B的pom文件中

 <dependencies>
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.8.3</version>
        </dependency>
    </dependencies>

via Project Structure 通过项目结构

IntelliJ and Maven dependecy management don't related to each other. IntelliJ和Maven依赖管理彼此无关。

You did not add B as a dependency to A. In order to do that you must add A's pom.xml this lines. 你没有将B作为依赖项添加到A.为了做到这一点,你必须在这行添加A的pom.xml。

<dependencies>
    <dependency>
        <groupId>xxx</groupId>
        <artifactId>B</artifactId>
        <version>0.5-SNAPSHOT</version>
     </dependency>
</dependencies>

Also jsoup is not defined as a dependency in any projects. 此外,jsoup未在任何项目中定义为依赖项。 You should read more about maven and its dependency management . 您应该阅读有关maven及其依赖关系管理的更多信息。

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

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