maven复制了groupId,artifactId和子模块中的版本

时间:2010-08-07 00:39:01

标签: maven-2

我们正在优化我们的Maven配置(以前使用过ant),我刚刚阅读了Sonatype的Maven by Example书。复制配置让我们在过去遇到了麻烦,所以我绝对想避免这一点。

上面的书提到在将其他兄弟子模块称为依赖项时,使用父模块中的内置project.groupId和project.version属性:

<dependency>
  <groupId>${project.groupId}</groupId>
  <artifactId>model</artifactId>
  <version>${project.version}</version>
</dependency>

这很有效,我喜欢它。但这在子模块pom.xml的标签中不起作用:

<parent>
    <groupId>${project.groupId}</groupId>
    <artifactId>${project.artifactId}</artifactId>
    <version>${project.version}</version>
    <relativePath>../pom.xml</relativePath>
</parent>

我想这不是什么大问题,似乎我可以为这些创建属性,但是有很多模块我真的很想完全理解这些问题的最佳实践..

更新 到目前为止,似乎这样做的最佳方法如下。有点难看,但消除了重复的硬编码值。 parent pom.xml:

<groupId>${groupId}</groupId>
<artifactId>${artifactId}</artifactId>
<packaging>pom</packaging>
<version>${version}</version>
<properties>
    <groupId>com.mycompany</groupId>
    <artifactId>mycompany</artifactId>
    <version>1.0</version>
</properties>

child pom.xml:

<parent>
    <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>
    <relativePath>../pom.xml</relativePath>
</parent>
<artifactId>child</artifactId>

1 个答案:

答案 0 :(得分:3)

  

这很有效,我喜欢它。但这不适用于子模块pom.xml(...)

的标记

这不起作用,鸡和蛋问题:你无法从父母那里得到父母的坐标。

  

我想这不是什么大问题,似乎我可以为这些创建属性,但是有很多模块我真的很想完全理解这些问题的最佳实践

好吧,假设它会起作用(据我所知,属性不会在父元素中扩展,所以它不会),在哪里放置这些属性?在父母?与上述问题相同。在孩子?没有意义。简而言之,您需要在父元素

中对这些值进行硬编码

请注意,这不是groupIdartifactId的问题,因为它们不会改变;但是,version更令人烦恼,我建议使用Maven Release PluginVersions Maven Plugin(及其versions:update-child-modules目标。)

PS:Maven 3.1将支持无版本的父元素(参见MNG-624)。

相关答案


  

为什么我不能从父母那里得到父母的坐标? relativePath应该允许我访问父pom。因此,如果您将parentId,artifactId和version作为属性添加到父级中,然后引用子级中父级的那些属性,它实际上是有效的。因此,没有重复的值,父级中相应的硬编码元素也被替换为属性的引用。这似乎是一种丑陋的方式,但它有效......

不,它确实 NOT 工作,在父元素中允许属性替换 (以便以后再现)。这就是它在Maven 2.x中的设计方式。请参阅用户邮件列表中的MNG-624the millions of threads about this,例如:

你问过最佳做法,我回答说:硬编码父元素中的所有内容。周期。

相关问题