【问题标题】:Maven property substitution for repository url not happening if parent pom is defined如果定义了父 pom,则不会发生存储库 url 的 Maven 属性替换
【发布时间】:2020-01-08 17:19:08
【问题描述】:

我遇到了一个问题,当且仅当定义了父 pom 时,maven 无法将属性正确替换到我的存储库 URL 中。这尤其是一个问题,因为父 pom 在远程存储库中,所以我需要定义父 pom。

最小可重现示例:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.group</groupId>
    <artifactId>parent-artifact</artifactId>
    <version>1.0.0</version>
    <relativePath/>
  </parent>

  <groupId>com.group</groupId>
  <artifactId>project-artifact</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>project</name>
  <description>test project</description>

  <properties>
    <nexus.url>https://nexus.myorganization.net</nexus.url>
  </properties>

  <repositories>
    <repository>
      <id>nexus-server</id>
      <url>${nexus.url}/repository/maven-releases/</url>
    </repository>
  </repositories>

</project>

使用这个 pom,我收到错误消息Cannot access ${nexus.url}/repository/maven-snapshots/...,很明显它没有用实际值替换属性。

如果我删除 POM 的 &lt;parent&gt; 部分,那么突然属性替换就可以正常工作了:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.group</groupId>
  <artifactId>project-artifact</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>project</name>
  <description>test project</description>

  <properties>
    <nexus.url>https://nexus.myorganization.net</nexus.url>
  </properties>

  <repositories>
    <repository>
      <id>nexus-server</id>
      <url>${nexus.url}/repository/maven-releases/</url>
    </repository>
  </repositories>

  <!-- adding this dependency so that Maven is forced to download from the repository -->
  <dependencies>
    <!-- some dependency here -->
  </dependencies>

</project>

我知道它工作正常,因为我可以在 maven 的输出中看到 Downloading from nexus-server: https://nexus.myorganization.net/repository/maven-releases/... 这一行

有什么想法吗?

【问题讨论】:

  • 那里不提供变量替换。这是为了使您的构建更加健壮而设计的。
  • 为什么不呢?为什么只有在没有定义父 pom 时才可用?
  • 从唯一位置使用的存储库应该是您的 settings.xml 文件,不应更改,也不应放入 pom 文件中...
  • 我假设从 repo 下载父 pom 时会发生错误?这是有道理的,因为要构建整个属性列表,您需要下载父级并构建有效的 POM。因此,您无法应用属性,因为尚未下载父级,并且您无法下载父级,因为尚未应用属性。这就是我对它为什么不起作用的看法,但我没有进行任何实验来证实这个假设。
  • 它在父 pom 坐标被解析后 AFTER 可用。你不能这样做。

标签: java maven


【解决方案1】:

感谢评论者,原因是直到父pom解决后才发生属性解析,因此当前pom和父pom之间没有属性冲突。

更多详情请见票MNG-2569MNG-624 (source)。

【讨论】:

    【解决方案2】:

    我认为您在父项的定义中有错误的标签。 为什么会有 relativePath 标签?

    只需删除标签: 再试一次

    【讨论】:

    • 老实说,我不确定为什么会有相对路径标签,但删除标签并不会改变行为。
    • relativePath 告诉 Maven 在磁盘上相对于父 pom 的 pom.xml 查找的位置。通常该值是“此目录上方的目录”
    猜你喜欢
    • 2012-02-03
    • 1970-01-01
    • 1970-01-01
    • 2017-03-11
    • 2012-12-22
    • 2012-03-31
    • 2012-07-15
    • 2013-11-01
    • 2015-08-22
    相关资源
    最近更新 更多