在 Maven 项目中,如何自动更新所有子模块以及父模块的版本?

发布于 2024-08-23 19:34:02 字数 281 浏览 9 评论 0原文

我有一个多模块项目。

parent POM (1.0-SNAPSHOT)
|-- module1 (1.0-SNAPSHOT)
|-- module2 (1.0-SNAPSHOT)
`-- module3 (1.0-SNAPSHOT)

当我执行 mvn release:prepare 时,它​​会验证父 POM 是否具有 SNAPSHOT 版本,并且所有依赖模块都没有 SNAPSHOT 版本。如何自动将所有子模块从 SNAPSHOT 更新到下一个发布版本?

我想自动增加所有模块的版本。

I have a multi-module project.

parent POM (1.0-SNAPSHOT)
|-- module1 (1.0-SNAPSHOT)
|-- module2 (1.0-SNAPSHOT)
`-- module3 (1.0-SNAPSHOT)

When I execute mvn release:prepare it verify that parent POM has a SNAPSHOT version and all dependent modules don't have a SNAPSHOT version. How automatically update all child modules from SNAPSHOT to the next release version?

I would like automatically increment version for all modules.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦亦如幻 2024-08-30 19:34:02

设置 poms 版本(包括多模块项目)的灵活方法是 Versions Maven Plugin

mvn versions:set -DnewVersion=your_new_version

它将调整多模块项目中的所有 pom 版本、父版本和子版本。

那么

mvn versions:commit

或者

mvn versions:revert

A flexible way to set poms versions, multi-modules projects included, is Versions Maven Plugin.

mvn versions:set -DnewVersion=your_new_version

It will adjust all pom versions, parent versions and children versions in a multi-module project.

then

mvn versions:commit

or

mvn versions:revert
拥抱我好吗 2024-08-30 19:34:02

发布插件可以处理这个问题。您是否检查过更新 POM 版本?但...
我什么也没得到。 将 POM 中的版本从 x-SNAPSHOT 更改为新版本以及将 POM 中的版本更改为新值 y-SNAPSHOT 应通过 release 来完成:prepare准备发布<中所述/a>.使用这个目标时出了什么问题?

更新: autoVersionSubmodules 参数可能就是您正在寻找的。从准备发布示例:

多模块项目

系统将提示您输入版本
项目每个模块的编号。
如果您希望每个模块都获得
与父 POM 版本相同,
你可以设置选项
autoVersionSubmodules 更改为 true。现在轮到你
只会被询问一次
发布版本和下一个版本
开发版本。

父 pom.xml 的片段

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>x.y.z</version>
            <configuration>
                <goals>deploy</goals>
                <autoversionsubmodules>true</autoversionsubmodules>
            </configuration>
        </plugin>
    </plugins>
</build>

The release plugin can handle that. Did you check Updating POM Versions? But...
I don't get something. Changing the version in the POMs from x-SNAPSHOT to a new version and bumping the version in the POMs to a new value y-SNAPSHOT should be done by release:prepare as explained in Prepare a Release. What is going wrong when using this goal?

Update: The autoVersionSubmodules parameter might be what you're looking for. From the Prepare a Release example:

Multi-module projects

You will be prompted for the version
number for each module of the project.
If you prefer that every module gets
the same version as the parent POM,
you can set the option
autoVersionSubmodules to true. Now you
will be asked only once for the
release version and the next
development version.

Snippet of parent pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-release-plugin</artifactId>
            <version>x.y.z</version>
            <configuration>
                <goals>deploy</goals>
                <autoversionsubmodules>true</autoversionsubmodules>
            </configuration>
        </plugin>
    </plugins>
</build>
鹿港小镇 2024-08-30 19:34:02
  1. 使所有子项目的版本都定义在其父pom.xml中。
  2. 确保所有子项目版本与其父项目版本相同。

    com.xyz;
    module-1;
    <包装>罐子
    
    <父>
      com.xyz;
      xyz-parent
      <版本>1.0.123-SNAPSHOT
    
    

     

    
    <项目 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
      <模型版本>4.0.0
      xyz-parent
      com.xyz;
      <版本>1.0.123-SNAPSHOT
      <包装>pom
      <名称>xyz-父级
    
      <依赖管理>
        <依赖关系>
    
        
        <依赖关系>
          com.xyz;
          module-1;
          <版本>${project.version}
        
    
        <依赖关系>
          com.xyz;
          module-2
          <版本>${project.version}
        
    
      
    
    
  3. 创建另一个 pom.xml 将这些项目组合在一起。

    <前><代码><模块>;
    <模块>../xyz-parent
    <模块>../module-1
    <模块>../module-2

  4. 然后更新父项目的版本,然后通过以下命令构建它。

    mvn 版本:set -DnewVersion=1.0.1004-SNAPSHOT
    mvn 干净安装
    
  5. 然后将这些子项目中定义的父版本更新为 latset 版本

    mvn -N 版本:更新子模块
    
  6. 然后将它们一起构建。

    <前><代码>@echo 开启
    cd .\xyz-父级
    调用 mvn 版本:set -DnewVersion=1.0.1004-SNAPSHOT -o
    调用mvn版本:commit -o
    调用 mvn clean install -o
    :::http://www.mojohaus.org/versions-maven-plugin/examples/update-child-modules.html
    cd ..\xyz-buildaggregate-ide
    调用 mvn -N versions:update-child-modules -o
    调用 mvn clean install -o

  1. Make the version of all sub-project be defined in its parent pom.xml.
  2. Make sure all sub-projects versions are the same to their parent's version.

    <groupId>com.xyz</groupId>
    <artifactId>module-1</artifactId>
    <packaging>jar</packaging>
    
    <parent>
      <groupId>com.xyz</groupId>
      <artifactId>xyz-parent</artifactId>
      <version>1.0.123-SNAPSHOT</version>
    </parent>
    

     

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    
      <modelVersion>4.0.0</modelVersion>
      <artifactId>xyz-parent</artifactId>
      <groupId>com.xyz</groupId>
      <version>1.0.123-SNAPSHOT</version>
      <packaging>pom</packaging>
      <name>xyz-parent</name>
    
      <dependencyManagement>
        <dependencies>
    
        <!-- Message -->
        <dependency>
          <groupId>com.xyz</groupId>
          <artifactId>module-1</artifactId>
          <version>${project.version}</version>
        </dependency>
    
        <dependency>
          <groupId>com.xyz</groupId>
          <artifactId>module-2</artifactId>
          <version>${project.version}</version>
        </dependency>
    
      </dependencyManagement>
    </project>
    
  3. Create another pom.xml to group those project together.

    <modules>   
      <module>../xyz-parent</module>
      <module>../module-1</module>
      <module>../module-2</module>      
    </modules>
    
  4. Then update the parent project's version and then build it by below command.

    mvn versions:set -DnewVersion=1.0.1004-SNAPSHOT
    mvn clean install
    
  5. Then update the parent's version which is defined in those sub-project to the latset one

    mvn -N versions:update-child-modules
    
  6. Then build them together.

    @echo on
    cd   .\xyz-parent
    call mvn versions:set -DnewVersion=1.0.1004-SNAPSHOT -o
    call mvn versions:commit -o
    call mvn clean install -o
    :::http://www.mojohaus.org/versions-maven-plugin/examples/update-child-modules.html
    cd ..\xyz-buildaggregate-ide
    call mvn -N versions:update-child-modules -o
    call mvn clean install -o
    
沫离伤花 2024-08-30 19:34:02

有一个可能更好的选择 https://issues.apache.org/jira/browse/MNG-624?focusedCommentId=14415968&page=com.atlassian.jira.plugin.system.issuetabpanels: comment-tabpanel#comment-14415968

这是一种解决方法,用于解决以下问题:如果没有列出显式版本,则无法引用子模块 poms 中的顶级 pom。 (这就是错误 MNG-624 的含义)
它解释了如何为版本提供单一位置(在顶级profiles.xml 文件中),并在所有 pom.xml 文件中简单地拥有属性引用(即 ${currentVersion})。

但是,在此方案版本中:prepare 可能不会为您更新profiles.xml。

There is a potentially better option over at https://issues.apache.org/jira/browse/MNG-624?focusedCommentId=14415968&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-14415968

That is a workaround for the fact that you can't refer to the top level pom in the sub-module poms without having an explicit version listed. (which is what bug MNG-624 is about)
It explains how you can have a single location for the version (in a top-level profiles.xml file), and simply have a property references in all the pom.xml files (i.e. ${currentVersion})

However, in this scheme release:prepare probably won't update profiles.xml for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文