【问题标题】:${project.artifactId} in parent pom.xml resolves odd父 pom.xml 中的 ${project.artifactId} 解决了奇怪的问题
【发布时间】:2013-12-29 02:35:02
【问题描述】:

我有很多项目在 pom.xml 中有相同的 URL:

<url>https://github.com/malkusch/${project.artifactId}</url>

<scm>
    <connection>scm:git:git://github.com/malkusch/${project.artifactId}.git</connection>
    <developerConnection>scm:git:git@github.com:malkusch/${project.artifactId}.git</developerConnection>
    <url>https://github.com/malkusch/${project.artifactId}</url>
</scm>

<issueManagement>
    <system>github</system>
    <url>https://github.com/malkusch/${project.artifactId}/issues</url>
</issueManagement>

所以我认为将其放入parent pom.xml 是个好主意。但是有效的 pom 会产生奇怪的 ${project.artifactId}:

<parent>
  <groupId>de.malkusch.parent</groupId>
  <artifactId>oss-parent</artifactId>
  <version>1.1-SNAPSHOT</version>
</parent>
<groupId>de.malkusch.localized</groupId>
<artifactId>localized</artifactId>
<url>https://github.com/malkusch/localized/localized</url>
<scm>
  <connection>scm:git:git://github.com/malkusch/localized.git/localized</connection>
  <developerConnection>scm:git:git@github.com:malkusch/localized.git/localized</developerConnection>
  <url>https://github.com/malkusch/localized/localized</url>
</scm>
<issueManagement>
  <system>github</system>
  <url>https://github.com/malkusch/localized/issues</url>
</issueManagement>

您注意到只有 issueManagement.url 被正确解析。其他的完全奇怪,尤其是 ${project.artifactId}.git -> localized.git/localized。我正在使用 Maven 3.0.4。我是否使用了一些未定义的功能?是bug吗?

【问题讨论】:

    标签: maven


    【解决方案1】:

    是的,这种行为令人困惑。

    也许理解这一点的最简单方法是考虑 Maven 本身是如何构建的。它在 Subversion 中,reactor pom(带有&lt;modules&gt; 部分的 pom)往往也是模块本身的父 pom。

    project/pom.xml (artifactId: parent)
    |-+ module1/pom.xml (artifactId: module1, inherits parent)
    |-+ module2/pom.xml (artifactId: module2, inherits parent)
    

    这里,父pom(project/pom.xml)包含一个&lt;modules&gt;部分,也是被module1和module2继承的。

    现在假设 parent 的 SCM URL 是 svn://host/path/project/:maven 应该怎么做才能不必在两个模块中再次指定 SCM URL?

    嗯,module1 的 SCM URL 是 svn://host/path/project/module1,Maven 可以通过将 artifactId 添加到它从父 pom 继承的 SCM URL 来计算它。它只需要将 artifactId 附加到 SCM URL。所以这正是它的作用。

    这就是您看到的行为:

    ${project.artifactId}.git 变成 localized.git/localized 如下:

    localized  -> from ${project.artifactId} in the inherited SCM URL
    .git       -> from the the inherited SCM URL
    /localized -> from adding the artifactId to the inherited SCM URL
    

    您将在 SCM URL 中看到这种行为,(我认为)project.urldistributionMangement.site.url 中的 URL。但是,Maven 并不假定 issueManagement URL 结构遵循您的目录结构,这就是您看到它正确继承的原因。

    【讨论】:

    • 哇!这种行为是否记录在案? ${project.artifactId} 的上下文敏感性给我留下了深刻的印象。
    • Maven JIRA 中似乎有很多问题,这表明对这种行为有很多困惑。 Jörg Schaible 的评论提供了我能找到的行为的最佳解释:jira.codehaus.org/browse/…
    • 是的,一切都记录在案:这是模型构建的结果,请参阅maven.apache.org/ref/current/maven-model-builder
    【解决方案2】:

    从 Maven 3.6.1 开始,继承可以通过将每个 url 的模型属性值设置为 false 来避免将任何路径附加到父值

    源代码:maven-model-builder

    您需要在 POM 中将以下属性附加到 SCM 标记: &lt;scm child.scm.connection.inherit.append.path="false" child.scm.developerConnection.inherit.append.path="false" child.scm.url.inherit.append.path="false"&gt;...&lt;/scm&gt;

    似乎 IntelliJ 显示错误,但最后,它完美地完成了这项工作。

    【讨论】:

      【解决方案3】:

      除了已经给出的很好的背景信息之外,为了仍然使用有效的 scm urlsince Maven 3.5 进行部署,您可以更正每个 project.directory 属性附加的“模块”名称:

      <properties>
          <project.directory>${project.artifactId}</project.directory>
      </properties>
      

      然后您只需简化您的developerConnection 以不再包含artifactId,因为它将作为project.directory 附加:

      <scm>
          <developerConnection>scm:git:git@server:</developerConnection>
      </scm>
      

      假设这与任何其他 goals 不冲突,那应该允许 deploy 目标使用正确的 git url 为您定义了 @987654331 的非多模块 Maven 项目完成其工作@ 在父 pom 中。

      当我运行deploy 时,我看到 maven 进行了正确的推送,例如:

      [INFO] Executing: /bin/sh -c cd /projectDir/proj && git push git@server:proj master:master
      

      【讨论】:

        猜你喜欢
        • 2021-07-24
        • 1970-01-01
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多