【问题标题】:Intellij can not parse pom.xml with $revision. Maven worksIntellij 无法使用 $revision 解析 pom.xml。 Maven 作品
【发布时间】:2020-05-14 06:10:27
【问题描述】:

${revision} 标签可以在 pom.xml 中使用,如 here 所述。

有这个目录结构:

fix
├── pom.xml
├── parent
│   └── pom.xml
└── example
    └── pom.xml

pom.xml 有:

<project>
    <groupId>intellij</groupId>
    <artifactId>fix</artifactId>
    <version>${revision}</version>

    <properties>
        <project.parent.basedir>parent</project.parent.basedir>
    </properties>

    <packaging>pom</packaging>
    <modules>
        <module>parent</module>
        <module>example</module>
    </modules>
    <modelVersion>4.0.0</modelVersion>
</project>

parent/pom.xml 有:

<project>
    <groupId>intellij</groupId>
    <artifactId>parent</artifactId>

    <properties>
        <revision>1.0.0</revision>
    </properties>

    <parent>
        <groupId>intellij</groupId>
        <artifactId>fix</artifactId>
        <version>${revision}</version>
    </parent>

    <packaging>pom</packaging>
    <modelVersion>4.0.0</modelVersion>
</project>

还有example/pom.xml

<project>
    <groupId>example</groupId>
    <artifactId>example</artifactId>

    <parent>
        <groupId>intellij</groupId>
        <artifactId>parent</artifactId>
        <version>${revision}</version>
        <relativePath>../parent/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>
</project>

IntelliJ 报告:

而终端上的mvn 可以很好地解析它并且可以正常工作。

这会破坏 IntelliJ 中的很多内容。
我用 IntelliJ 2019.2 试过这个

【问题讨论】:

  • 您应该将版本属性的属性定义从您的父级移动到项目的根目录(修复工件)。此外,我强烈建议不要使用既是模块又是父级的父级...

标签: maven intellij-idea pom.xml parent-pom


【解决方案1】:

只是一个提示。如果您对结构执行简单的mvn clean on 命令,您将看到:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] parent 1.0.0 ....................................... SUCCESS [  0.146 s]
[INFO] example 1.0.0 ...................................... SUCCESS [  0.003 s]
[INFO] fix ${revision} .................................... SUCCESS [  0.002 s]
[INFO] ------------------------------------------------------------------------

这表明这里有问题......因为版本属性无法在根 pom 中解析......这就是 IDEA IntelliJ 告诉你有问题的原因......因为您在最后一行看到根项目,这表明存在问题。

如果您将修订属性正确移动到根项目(修复)中,您将看到:

[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ example ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for fix 1.0.0:
[INFO] 
[INFO] fix ................................................ SUCCESS [  0.106 s]
[INFO] parent ............................................. SUCCESS [  0.003 s]
[INFO] example ............................................ SUCCESS [  0.002 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.255 s
[INFO] Finished at: 2020-01-28T20:56:22+01:00
[INFO] ------------------------------------------------------------------------

在此输出中,您会在第一行看到fix,这是正确的。

我强烈建议正确更改您的结构,不要在您的 example 项目中使用 &lt;relativePath&gt;../parent/pom.xml&lt;/relativePath&gt;,这表明您的目录结构不代表您在 pom 文件中创建的真实结构。

这应该会产生我在 github 上创建的示例项目中给出的结构:

https://github.com/khmarbaise/so-question-1

除此之外,请按照文档中的说明将flatten-maven-plugin 添加到您的项目中,否则如果您使用mvn clean installmvn clean deploy,您将失败

【讨论】:

  • 首先,感谢您抽出宝贵时间查看此问题。这解决了问题。这对您来说似乎很明显,但我继承的项目有 187 个 pom.xml 文件,为了修复它,我费了点力才提出这个简化的示例。我会尝试改变结构,但这取决于这是否符合我的发展议程。我们已经启用了 flatten-maven-plugin 和许多其他...再次欢呼并感谢!
  • 我知道现实生活是怎样的,因为我已经完成了几个项目,其中包含非常大的 pom(800 多个模块等)......所以不足为奇。
  • 哇,800+!那是很多bigger ball of mud... ;-)
猜你喜欢
  • 1970-01-01
  • 2013-03-17
  • 2021-02-02
  • 2016-06-12
  • 2014-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
相关资源
最近更新 更多