父母可以从儿童那里获得房产吗?

时间:2014-04-18 18:17:07

标签: maven pom.xml parent-pom

父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区域。

2 个答案:

答案 0 :(得分:0)

  

父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属性仍具有默认值时,此配置文件将变为活动状态。

答案 1 :(得分:0)

我在这里添加这个,因为一般行为如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>

在孩子的有效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,它会自动将项目名称添加到连接中。

更新:maven似乎有一些自动更改网址:${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