【问题标题】:Best practice: Versioning and releases in multiprojects最佳实践:多项目中的版本控制和发布
【发布时间】:2012-08-15 20:40:26
【问题描述】:

在以下多项目情况下,版本控制和发布管理的最佳做法是什么?

项目结构

  • 全局父级
    • 父项目(版本:1.0-SNAPSHOT)
      • 子项目 1(与父项目相同)
      • 子项目 2(与父项目相同)
      • 子项目 3(与父项目相同)
      • 子项目 4(与父项目相同)

我只想为父项目和所有子项目设置一次版本,因为项目的每个部分都必须具有相同的版本。

我还想要的是,用 continuum/maven 发布项目。

当前的“坏”解决方案:

通常一个简单的方法应该是在父 pom 中设置版本并在每个孩子中说“来自父母的最后一个版本”,但这不适用于 maven

例子:

家长

<groupId>com.test</groupId>
<artifactId>com.test.buildDefinition</artifactId>
<version>1.0-SNAPSHOT</version>

儿童

<parent>
    <groupId>com.test</groupId>
    <artifactId>com.test.buildDefinition</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<groupId>com.test</groupId>
<artifactId>com.test.project</artifactId>
<version>${parent.version}</version>

如果我想现在使用 Continuum 发布我的项目,我使用以下顺序来发布它:

  1. 父项目
  2. 子项目1
  3. 子项目 2

但这不起作用,因为在更改父级版本后,子级在父级中不再有 SNAPSHOT 版本,我认为必须有更好的方法来发布具有连续性的多项目。

【问题讨论】:

  • 是多模块项目吗?
  • 否,目前我不使用多模块。但我想看看它。

标签: java maven dependencies release continuum


【解决方案1】:

如果您在&lt;dependencyManagement/&gt; 标签中添加您的子模块,我很确定您不会遇到这个问题。

家长

<groupId>com.test</groupId>
<artifactId>com.test.buildDefinition</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<modules>
    <module>child1</module>
    <module>child2</module>
</modules>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>com.test.child1</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>com.test</groupId>
            <artifactId>com.test.child2</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

孩子1

<parent>
    <groupId>com.test</groupId>
    <artifactId>com.test.buildDefinition</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<!-- groupId and version can be skipped since it will be inherited from parent -->
<artifactId>com.test.child1</artifactId>

Child2(取决于 Child1)

<parent>
    <groupId>com.test</groupId>
    <artifactId>com.test.buildDefinition</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>

<!-- groupId and version can be skipped since it will be inherited from parent -->
<artifactId>com.test.child2</artifactId>

<dependencies>
    <dependency>
        <groupId>com.test</groupId>
        <artifactId>com.test.child1</artifactId>
    </dependency>
</dependencies>

如果您尝试在使用dependencyManagement 时,模块之间的依赖项将永远不必定义任何版本,因为它们是在父pom 中定义的。

通过这种方式发布多模块项目我从来没有遇到过任何问题。

编辑

要明确:dependencyManagement 与父子之间的继承无关。它解决了子模块之间依赖版本的任何问题。它在发布期间有效。

【讨论】:

    【解决方案2】:

    如果您希望所有版本号保持同步,您可以使用 Maven 发布插件的autoVersionSubmodules 标志。将此值设置为 true 将允许您在顶级项目级别执行发布,并以与父模块相同的版本发布所有子模块。

    这可以在您运行mvn -DautoVersionSubmodules release:prepare 时在命令行中指定,或者在POM 文件中指定,如下所示:

    <plugin>
      <artifactId>maven-release-plugin</artifactId>        
      <version>2.3.2</version>
      <configuration>
        <autoVersionSubmodules>true</autoVersionSubmodules>
      </configuration>
    </plugin>
    

    我没有使用过 Continuum,但我相信它在幕后使用了 Maven 发布插件?一些谷歌搜索表明这可能有效(基于一些关于 Continuum 界面如何处理这种情况的“很好”的错误)。

    【讨论】:

    • 没错,Continuum 的发布功能使用 Maven 发布插件,包括尊重您提到的标志。
    【解决方案3】:

    您应该在单个版本控制层次结构下为同时具有相同版本的模块创建一个多模块结构。 Continuum 允许您将它们作为单个作业或每个模块的多个作业添加到组中,无论哪种方式,发布机制都会从父级触发并自动为您解析版本。

    顺便说一句,您可以删除 &lt;version&gt;${parent.version}&lt;/version&gt; 和相同的 groupId,因为它们将从父级继承。

    如果全局父级单独发布,您应该将其拆分为单独的模块,而不是使其成为同一个多模块结构的一部分。您可以在此处找到此布局中的示例项目:https://github.com/brettporter/centrepoint

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-19
      • 2013-03-25
      • 2010-09-24
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      • 2012-03-22
      相关资源
      最近更新 更多