解决 Maven 报错 Non-resolvable parent POM ... was cached in the local repository, resolution will not be

解决 Maven 报错 Non-resolvable parent POM ... was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM

  笔者使用 IntelliJ IDEA 对以前的项目作重构时,发现一个匪夷所思的问题:明明使用 IntelliJ IDEA 运行项目时,项目正常运行,但使用 Maven 命令进行 clean、compile、install 时,对项目中的每一个模块,均发生如下的报错。

笔者报错时的运行环境:

JDK 13.0.2

Maven 3.6.3

IntelliJ IDEA 2020.1.2 (Ultimate Edition)

  这个报错的信息如下:

"C:\Program Files\Java\jdk-13.0.2\bin\java.exe" -Dmaven.multiModuleProjectDirectory=XXX -Xms128m -Xmx512m "-Dmaven.home=XXX\plugins\maven\lib\maven3" "-Dclassworlds.conf=XXX\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=XXX\plugins\maven\lib\maven-event-listener.jar" "-javaagent:XXX\lib\idea_rt.jar=25353:XXX\bin" -Dfile.encoding=UTF-8 -classpath "XXX\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;XXX\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.versionXXXorg.apache.maven.plugins:maven-clean-plugin:2.5:clean
[INFO] Scanning for projects...
[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[WARNING] 'artifactId' contains an expression but should be a constant. @ line 17, column 17
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.junit.jupiter:junit-jupiter:jar -> duplicate declaration of version (?) @ line 35, column 21
[WARNING] 'artifactId' contains an expression but should be a constant. @ XXX:${project_artifactId}:${project_version}, XXX\pom.xml, line 13, column 17
[WARNING] 'version' contains an expression but should be a constant. @ XXX:${project_artifactId}:${project_version}, XXX\pom.xml, line 14, column 14
[WARNING] 'parent.relativePath' of POM XXX:${project_artifactId}:XXX (XXX\XXX\pom.xml) points at XXX:${project_artifactId} instead of XXX:XXX-project, please verify your project structure @ line 9, column 13
[FATAL] Non-resolvable parent POM for XXX:${project_artifactId}:XXX: Failure to find XXX:XXX-project:pom:XXX in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ line 9, column 13
[WARNING] 'artifactId' contains an expression but should be a constant. @ line 17, column 17
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.junit.jupiter:junit-jupiter:jar -> duplicate declaration of version (?) @ line 29, column 21
[WARNING] 'artifactId' contains an expression but should be a constant. @ XXX:${project_artifactId}:${project_version}, XXX\pom.xml, line 13, column 17
[WARNING] 'version' contains an expression but should be a constant. @ XXX:${project_artifactId}:${project_version}, XXX\pom.xml, line 14, column 14
[WARNING] 'parent.relativePath' of POM XXX:${project_artifactId}:XXX (XXX\XXX-exception\pom.xml) points at XXX:${project_artifactId} instead of XXX:XXX-project, please verify your project structure @ line 9, column 13
[FATAL] Non-resolvable parent POM for XXX:${project_artifactId}:XXX: Failure to find XXX:XXX-project:pom:XXX in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ line 9, column 13
[WARNING] 'artifactId' contains an expression but should be a constant. @ XXX:${project_artifactId}:${project_version}, XXX\pom.xml, line 13, column 17
[WARNING] 'version' contains an expression but should be a constant. @ XXX:${project_artifactId}:${project_version}, XXX\pom.xml, line 14, column 14
 @ 
[ERROR] The build could not read 2 projects -> [Help 1]
[ERROR]   
[ERROR]   The project XXX:${project_artifactId}:XXX (XXX\XXX\pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for XXX:${project_artifactId}:XXX: Failure to find XXX:XXX-project:pom:XXX in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ line 9, column 13 -> [Help 2]
[ERROR]   
[ERROR]   The project XXX:${project_artifactId}:XXX (XXX\XXX-exception\pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM for XXX:${project_artifactId}:XXX: Failure to find XXX:XXX-project:pom:XXX in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced and 'parent.relativePath' points at wrong local POM @ line 9, column 13 -> [Help 2]
[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/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

  在笔者不断地新建工程,不断地隔离模块的努力下,终于发现了原因所在。(当然,每个遇到这个问题的读者可能原因不同,这里仅供参考。)

  为了偷懒,笔者编写了一个愚蠢的代码。这段代码类似如下:

    <properties>
        <project_artifactId>demo-project</project_artifactId>
        <project_version>1.2</project_version>
    </properties>
    
    <groupId>org.demo</groupId>
    <artifactId>${project_artifactId}</artifactId>
    <version>${project_version}</version>
    <packaging>pom</packaging>
    <name>{project_artifactId}</name>

  上述代码的问题在于,对 Maven 项目的坐标使用了 <properties/...> 中的元素来代替。但实际上,<properties/...> 应该在依赖(<dependencies/...>)中使用。Maven 并未在坐标中识别出 <properties/...>,因为坐标解析发生在解析 <properties/...> 之前。同样地,对于子 pom,虽然父 pom 中的 <properties/...> 可以被子 pom 继承,不过在子 pom 中的 <parent/...> 坐标里同样不能解析父 pom 中的 <properties/...>,因为必须要先明确地指定它的父 pom 坐标,然后继承才会发生。终于知道真相的笔者眼泪掉下来。希望读者引以为戒。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值