美文网首页
项目循环依赖导致Maven无法编译

项目循环依赖导致Maven无法编译

作者: 沉静说 | 来源:发表于2017-08-28 23:32 被阅读340次

    今天使用Maven编译项目的时候,遇到了一个异常:

    [ERROR] Internal error: java.lang.NullPointerException -> [Help 1]
    org.apache.maven.InternalErrorException: Internal error: java.lang.NullPointerException
        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:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.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 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/InternalErrorException
    

    参考StackOverFlow的资料,导致这个问题的其中一个原因是项目循环引用,原回答为:

    You have a cyclic reference of dependencies.
    It looks like your crm-core project has crm-data in its dependencies and the crm-data project has crm-core in its dependencies.
    Maven can't compile such a case

    翻译:

    你的依赖循环引用了,工程A里面有工程B的依赖,工程B有工程A的依赖。Maven在这种情况下是无法编译的。

    突然想到我的工程中确实存在循环依赖的问题,在Eclipse的Markers里面有一堆警告提示信息,精简之后如下:

    A cycle was detected in the build path of project 'prjoectA'. 
    The cycle consists of projects {projectA, projectB, projectC...}
    

    之前我看到这个问题的时候,网上提供了两种方案:

    1. 在Eclipse的首选项里面关闭这个错误提示,修改为警告;
    2. 解决项目之间循环引用的问题

    当时因为偷懒,选择了Plan 1,但是现在问题来了,Maven项目编译不通过,而我们公司的整个工程都是建立在Maven上的。

    经过仔细排查,最终在Maven dependencies里找到了common工程错误地依赖了某个facade工程,而facade工程又引用了common工程。
    按照工程的设计,common应该作为所有的工程的依赖工程,所以,只能将facade中被引用的部分挪入common中。

    代码修改之后,在项目上选择Maven-Update Project,勾选Force Update。完成后,对Project选择Clean,错误提示消失。
    重新进行Maven编译,提示Success,问题解决!


    总结:
    这个问题告诉我们,进行工程开发之前,需要先理清楚工程结构。了解各个项目之间的依赖关系,杜绝循环引用。


    参考资料:
    https://stackoverflow.com/questions/23078500/maven-throws-nullpointer-exception

    相关文章

      网友评论

          本文标题:项目循环依赖导致Maven无法编译

          本文链接:https://www.haomeiwen.com/subject/xfendxtx.html