Maven抛出空指针异常。

10

您好,

我使用Maven构建了一个项目。在这个项目中,我得出结论,特定模块需要添加一个依赖项。我已经在它的pom文件中添加了这个依赖项,但是当我尝试使用Maven重新构建项目时,抛出了空指针异常。

之前的Pom文件:

(子pom:)

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
    </dependency>

</dependencies>

(主要的pom:)

    <dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>


        <dependency>
            <groupId>com.solveit.crm.core</groupId>
            <artifactId>crm-core</artifactId>
            <version>${project.version}</version>
        </dependency> //this dependency is for another module, it was
                        declared at the start of the project


    </dependencies>
</dependencyManagement>

现在的Pom文件(导致空指针异常):

(子Pom文件:)

<dependencies>

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.0-api</artifactId>
    </dependency>

    <dependency>
        <groupId>com.solveit.crm.data</groupId>
        <artifactId>crm-data</artifactId>
    </dependency>

</dependencies>

(主POM:)

<dependencyManagement>
    <dependencies>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>


        <dependency>
            <groupId>com.solveit.crm.core</groupId>
            <artifactId>crm-core</artifactId>
            <version>${project.version}</version>
        </dependency>

        <dependency>
            <groupId>com.solveit.crm.data</groupId>
            <artifactId>crm-data</artifactId>
            <version>${project.version}</version>
        </dependency>

    </dependencies>
</dependencyManagement>

为什么 Maven 会抛出空指针异常?我现在很困惑。

错误信息:

    [INFO] Scanning for projects...
[ERROR] Internal error: java.lang.NullPointerException -> [Help 1]
org.apache.maven.InternalErrorException: Internal error: java.lang.NullPointerEx
ception
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:167)
        at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
        at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:213)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:157)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:483)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Laun
cher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.jav
a:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(La
uncher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:
356)
Caused by: java.lang.NullPointerException
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:270)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
        ... 11 more
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/InternalErrorE
xception

在 Eclipse 中,我得到:

    [INFO] Scanning for projects...
[ERROR] The projects in the reactor contain a cyclic reference: Edge between 'Vertex{label='com.solveit.crm.data:crm-data:1'}' and 'Vertex{label='com.solveit.crm.core:crm-core:1'}' introduces to cycle in the graph com.solveit.crm.core:crm-core:1 --> com.solveit.crm.data:crm-data:1 --> com.solveit.crm.core:crm-core:1 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectCycleException

谢谢


尝试在同一项目目录下使用命令窗口而不是Eclipse运行maven clean install命令。 - Jay
杰伊,我用了 cmd 而不是 Eclipse 来完成这个。 - Bravo
我现在尝试在Eclipse中构建这个项目,但是我得到了一个不同的错误。我已经编辑并添加了Eclipse的错误信息。 - Bravo
1个回答

14

您有一组循环依赖关系。

看起来您的crm-core项目在其依赖项中引用了crm-data,而crm-data项目在其依赖项中引用了crm-core。

Maven无法编译这种情况。


我该如何处理这种情况?我如何使两个模块相互依赖? - Bravo
3
你可以将常用的类/资源移动到第三个独立模块中,并使现有的两个模块依赖于这个公共模块。 - ssssteffff
在我看来,你不能这样做。也许你可以为你的项目考虑另一个概念。你可以像ssssteeefff建议的那样使用一个常见的模块。 - Fabien Thouraud
嗯,好的...谢谢大家,我会记住的。干杯 - Bravo
17
我认为Maven崩溃并出现NullPointerException并不是解释循环依赖关系的最佳方式。 - djjeck
如果您正在使用Eclipse M2E插件,它将为您提供更好的错误消息提示,指出存在循环依赖关系。 - yellavon

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接