【问题标题】:Can parent poms receive properties from children?parent poms 可以从孩子那里接收属性吗?
【发布时间】:2014-04-18 18:17:07
【问题描述】:

父 pom 中的某些内容可以采用子 pom 中声明的值吗?

具体来说,我想做这样的事情。

家长:

<scm>
    <connection>scm:git:git@github.corp.com:team/${git.repo}.git</connection>
    <developerConnection>scm:git:git@github.corp.com:team/${git.repo}.git</developerConnection>
    <url>http://github.corp.com/team/${git.repo}</url>
</scm>

孩子:

<properties>
    <git.repo>Foo</git.repo>
</properties>

理想情况下,git.repo 属性未在父级中明确设置,因此子级必须覆盖它,或完全覆盖 scm 部分。

【问题讨论】:

    标签: maven pom.xml parent-pom


    【解决方案1】:

    我在此处添加此内容,因为一般行为如 Marcel 答案中所述,但特别是对于与 scm 一起使用的示例,这可能无法正常工作。

    在 maven 3.3.1(和 3.3.3)中无法按预期工作。

    家长:

    <properties>
        <git.project.name>uumds-commons</git.project.name>
        <git.scmUrl>https://github.com/raisercostin/${git.project.name}.git</git.scmUrl>
    </properties>
    
    <scm>
          <connection>scm:git:${git.scmUrl}</connection>
          <developerConnection>scm:git:${git.scmUrl}</developerConnection>
          <tag>HEAD</tag>
    </scm>
    

    孩子:

    <properties>
        <git.project.name>uumds-pdp</git.project.name>
    </properties>
    

    在 child 的有效 pom (mvn help:effective-pom) 中变为:

    <properties>
      <git.project.name>uumds-pdp</git.project.name>
      <git.scmUrl>https://github.com/raisercostin/uumds-pdp.git</git.scmUrl>
    </properties>
    
    <scm>
      <connection>scm:git:https://github.com/raisercostin/uumds-pdp.git/uumds-pdp</connection>
      <developerConnection>scm:git:https://github.com/raisercostin/uumds-pdp.git/uumds-pdp</developerConnection>
      <tag>uumds-parent-0.9</tag>
    </scm>
    

    所以看起来如果 scm 没有在 child 中定义,它会自动将项目名称添加到连接中。

    更新:maven似乎有一些自动更改url:${project.artifactId} in parent pom.xml resolves oddhttps://issues.apache.org/jira/browse/MNG-2290

    jira 状态

    Maven 有很多元素会修改 URL 或路径 自动为子 POM(我目前知道的那些):

    url
    scm/connection
    scm/developerConnection
    scm/url
    distributionManagement/site/url
    

    【讨论】:

    【解决方案2】:

    父pom中的某些东西可以取子pom中声明的值吗?

    是的。但是,为了有一个有效的父 POM,我建议在父中为 ${git.repo} 提供一个默认/虚拟值。

    要处理子 POM 不覆盖(即重新定义)${git.repo} 的情况,您可以像这样在父级中引入配置文件:

    <profiles>
      <profile>
        <id>property-not-redefined</id>
        <activation>
          <activeByDefault>false</activeByDefault>
          <property>
            <name>git.repo</name>
            <value>your-default-value</value>
          </property>
        </activation>
        <build>
          <plugins>
            <!-- do something here -->
          </plugins>
        </build>
      </profile>
    </profiles>
    

    git.repo 属性仍具有默认值时,此配置文件将变为活动状态。

    【讨论】:

    • “当 git.repo 属性仍然具有默认值时,此配置文件将变为活动状态。” - 你能再解释一下吗?我可以在子 POM 中做一些事情来为这个孩子而不是为其他人激活它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-17
    • 2015-07-18
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多