【问题标题】:Is there a way to cascade the version of a parent pom?有没有办法级联父 pom 的版本?
【发布时间】:2013-04-22 14:32:09
【问题描述】:

我有一个根 pom.xml,它充当多个子 pom.xml 文件的父级,如果您有一个或两个子 pom.xml 文件,那就太好了。我正在开发一个包含 50 多个子 pom.xml 文件的项目,并且同时发布多个版本。一切都很好,直到我需要更新父 pom.xml 文件的版本,这可能很乏味。有没有办法将父 pom.xml 的版本级联到所有子 pom.xml 文件?

我的想法是在当前版本的父 pom.xml 中创建一个属性,但这不起作用。

编辑

这是我的子 pom.xml 文件中的一个示例:

<parent>
    <groupId>myGroup</groupId>
    <artifactId>myArtifactId</artifactId>
    <version>1.0</version>  <!-- This is the value that I would like to parameterize in my child pom.xmls -->
    <relativePath>../../pom.xml</relativePath>
</parent>

顺便说一句,我使用的是 Maven 3.0.3

【问题讨论】:

  • 之前好像有人问过这个问题...stackoverflow.com/questions/1981151/…
  • @DaveRlz 差不多,但不完全
  • 响应中有一点说“要在子模块中使用父 pom 版本,只需从子 pom 中删除 标签,它们将从父 pom 继承版本。”这不是你想要做的吗?
  • @DaveRlz 我尝试只使用 groupId、artifactId 和 relativePath,但 maven 不喜欢它
  • 只需使用我在回答中描述的versions-maven-plugin。

标签: maven maven-3


【解决方案1】:

你可以这样做:

有一个聚合器 POM 项目,您可以在其中指定所有子模块,然后在此聚合器项目上运行此目标:

mvn versions:update-parent

这将使用新的父版本更新所有子模块。

或者,您可以在每次发布之前使用相同的:prepare 子模块的目标,从而避免创建聚合器模块。

所以如果你想发布一个子模块,你只需运行这个目标:

mvn versions:update-parent release:prepare

这将确保父级在准备发布之前使用新版本进行更新。

我在您的评论中看到您缺少 scm 标签。颠覆情况下的使用示例可能是:

<scm>
    <developerConnection>scm:svn:https://your.domain/scm/svn/yourModule/trunk/</developerConnection>
</scm>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <configuration>
                <tagBase>https://your.domain/scm/svn/yourModule/tags</tagBase>
            </configuration>
        </plugin> 
    </plugins>
</build>

如果没有正确配置的 scm,Release 插件将无法工作。

如果您在 svn 存储库中遵循“主干、标签、分支”最佳实践,则无需在发布插件中配置 tagbase 标签,see this answer.

【讨论】:

    【解决方案2】:

    我假设你有这样的事情:

    +--root (pom.xml)
         +--- module1 (pom.xml)
         +---
    

    在根 pom.xml 中:

    <project ..>
    
      <groupId>the.company.project</groupId>
      <artifactId>the-artifact</artifactId>
      <version>1.0-SNAPSHOT</version>
      ...
    </project>
    

    而你应该拥有的孩子:

    <project ..>
    
      <parent>         
        <groupId>the.company.project</groupId>
        <artifactId>the-artifact</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>the-artifact-child</artifactId>
    
    </project>
    

    为此,您应该使用maven-release-plugin 来处理根目录和子目录中的版本更新。如果你不使用 maven-release-plugin,你可以使用versions-maven-plugin,它可以处理同样的事情。

    【讨论】:

    • 我正在试用 maven-release-plugin,但由于缺少 scm 属性,我遇到了一些错误。你知道最小的配置是什么吗?
    【解决方案3】:

    我遇到了同样的问题,我所做的与目前所建议的略有不同。假设您的项目具有以下文件夹结构:

    parent
    +-- child1
         +-- child2_of_parent
         +-- child3_of_parent
    +-- child4
         +-- child5_of_child4
    

    请注意,除了child5_of_child4所有其余项目都以顶级项目parent 作为父级。这与文件夹结构无关。 家长pom.xml

    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>1.1-SNAPSHOT</version>
    

    所有子 pom.xml 除了 child5_of_child4 作为父 child4

     <parent>
      <groupId>...</groupId>
      <artifactId>parent</artifactId>
      <version>1.1-SNAPSHOT</version>
      <relativePath>../parent</relativePath>
     </parent>
    

    您转到父目录并将其版本从1.1-SNAPSHOT 更改为1.2-SNAPSHOT。然后你做(更多细节here):

    mvn versions:update-child-modules
    

    由于这不是递归的,它只适用于您父项目的直接子项目。换句话说,除了child5_of_child4之外,所有子模块都将更新为指向父1.2-SNAPSHOT

    【讨论】:

      【解决方案4】:

      在您的孩子 pom.xml 中:

      <parent>
          <groupId>groupId</groupId>
          <artifactId>parent.artifactId</artifactId>
          <version>version</version>
          <relativePath>../parent-pom</relativePath>
       </parent>
       <artifactId>child.artifactId</artifactId>
      

      【讨论】:

      • 这种情况下不需要指定relativePath,因为默认是parent。
      • 这取决于您的目录组织。在某些情况下,您可以让子目录不包含在父目录中,因此您需要指定相对路径;)
      【解决方案5】:

      只是不要将版本放在子 poms 中。它是自动继承的。如果您在那里编辑,它将在 Eclipse 插件中显示为警告。

      【讨论】:

      • 抱歉,我在尝试您的回答时收到此错误:[ERROR] The project myGroup:acceptance-tests:[unknown-version] (C:\git\my_app\1.0\my_app\acceptance- tests\pom.xml) 有 1 个错误 [ERROR] 'parent.version' is missing。 @ 第 8 行,第 13 列
      • @bakoyaro 否。在 中,您需要指定父版本。您没有指定子版本。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 2017-02-22
      • 2012-04-29
      • 2018-08-10
      • 1970-01-01
      • 2014-08-16
      • 2012-09-03
      相关资源
      最近更新 更多