代码之家  ›  专栏  ›  技术社区  ›  at.

子模块中的maven重复的groupId、artifactId和versions

  •  15
  • at.  · 技术社区  · 14 年前

    我们正在优化Maven配置(从以前使用ant开始),我刚刚阅读了 Maven by Example 从Sonatype预订。重复配置在过去给我们带来了麻烦,所以我绝对想避免哪怕是最微小的一点。

    上面的书提到在将其他同级子模块作为依赖项引用时使用父模块中的内置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>
    

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

    更新 到目前为止,最好的办法是这样做。有点难看,但消除了重复的硬编码值。

    <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>
    

    子pom.xml:

    <parent>
        <groupId>${groupId}</groupId>
        <artifactId>${artifactId}</artifactId>
        <version>${version}</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <artifactId>child</artifactId>
    
    1 回复  |  直到 12 年前
        1
  •  3
  •   Community CDub    7 年前

    很好,我喜欢。但这在pom.xml的子模块(…)的标记中不起作用

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

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

    好吧,假设它可以工作(据我所知,属性在父元素中没有展开,所以不会),那么这些属性应该放在哪里呢?在父母身上?和上面一样的问题。在孩子身上?没有意义。简而言之,您需要在父元素中硬编码这些值

    请注意,这对 groupId artifactId 因为它们不会改变;但是,对于 version 我建议要么用 Maven Release Plugin 或者 Versions Maven Plugin (及其 versions:update-child-modules 目标)。

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

    相关答案


    为什么我不能从父对象中获取父对象的坐标?相对路径应该能让我访问父pom。因此,如果将groupId、artifactId和version作为属性添加到父级中,然后在子级中为父级引用这些属性,那么它实际上是有效的。为了不存在重复的值,父元素中相应的硬编码元素也被替换为对属性的引用。这似乎是一个丑陋的方式去做这件事,但它的工作。。。

    不,是的 不是 工作,财产替代是 在母元素中允许(用于以后的再现性)。这就是它在Maven 2.x中的设计方法 the millions of threads about this

    你问到了最佳实践,我回答道:在父元素中硬编码所有内容。周期。