java - 更新依赖项中的版本时更新模块版本(多模块 maven )

标签 java maven versions-maven-plugin

我的问题:versions-maven-plugin 帮助我升级多模块 maven 项目中某些模块(我们称之为 A)的版本。

此项目中的一些模块(我们称之为BC)具有依赖模块A。我还需要升级该模块的版本(BC)。有时,我还需要升级其他模块(B-parent)中的版本,其中依赖项(A 版本升级 -> B 版本升级 -> B-parent 版本升级)。另一个问题是模块可以处于不同的嵌套级别。

示例:

root:
  ---B-parent: 
       ---B (A in dependencies)
  ---C-parent
       ---C (A in dependencies)
  ---A-parent: 
       ---A

流程:A版本升级->A-parent版本升级,C版本升级->C-parent 版本升级,B 版本升级 -> B-parent 版本升级。

这个插件无法做到这一点。

有什么想法可以做到这一点吗? 还是我的版本更新策略不够好?

最佳答案

我制作了一个脚本,用于使用 versions-maven-plugin 递归地增加所有依赖模块中的版本号。

算法如下:

  1. 运行版本:在目标模块中设置
  2. 在上一步中已被 versions:set 更新的所有模块中运行 versions:set。如果模块已被处理 - 跳过它。
  3. 重复步骤 2

Python 2.7 代码

#!/usr/bin/env python
# -*- coding: utf-8 -*- #

# How To
#
# Run script and pass module path as a first argument.
# Or run it without arguments in module dir.
#
# Script will request the new version number for each module.
# If no version provided - last digit will be incremented (1.0.0 -> 1.0.1).
# cd <module-path>
# <project-dir>/increment-version.py
# ...
# review changes and commit

from subprocess import call, Popen, PIPE, check_output
import os
import re
import sys

getVersionCommand = "mvn org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate " \
                    "-Dexpression=project.version 2>/dev/null | grep -v '\['"


def getCurrentModuleVersion():
    return check_output(getVersionCommand, shell=True).decode("utf-8").split("\n")[0]


def incrementLastDigit(version):
    digits = version.split(".")
    lastDigit = int(digits[-1])
    digits[-1] = str(lastDigit+1)
    return ".".join(digits)


def isUpdatedVersionInFile(version, file):
    return "<version>" + version + "</version>" in \
           check_output("git diff HEAD --no-ext-diff --unified=0 --exit-code -a --no-prefix {} "
                        "| egrep \"^\\+\"".format(file), shell=True).decode("utf-8")


def runVersionSet(version):
    process = Popen(["mvn", "versions:set", "-DnewVersion="+version, "-DgenerateBackupPoms=false"], stdout=PIPE)
    (output, err) = process.communicate()
    exitCode = process.wait()
    if exitCode is not 0:
        print "Error setting the version"
        exit(1)
    return output, err, exitCode


def addChangedPoms(version, dirsToVisit, visitedDirs):
    changedFiles = check_output(["git", "ls-files", "-m"]) \
        .decode("utf-8").split("\n")
    changedPoms = [f for f in changedFiles if f.endswith("pom.xml")]
    changedDirs = [os.path.dirname(os.path.abspath(f)) for f in changedPoms if isUpdatedVersionInFile(version, f)]
    changedDirs = [d for d in changedDirs if d not in visitedDirs and d not in dirsToVisit]
    print "New dirs to visit:", changedDirs
    return changedDirs


if __name__ == "__main__":
    visitedDirs = []
    dirsToVisit = []

    if len(sys.argv) > 1:
        if os.path.exists(os.path.join(sys.argv[1], "pom.xml")):
            dirsToVisit.append(os.path.abspath(sys.argv[1]))
        else:
            print "Error. No pom.xml file in dir", sys.argv[1]
            exit(1)
    else:
        dirsToVisit.append(os.path.abspath(os.getcwd()))

    pattern = re.compile("aggregation root: (.*)")
    while len(dirsToVisit) > 0:
        dirToVisit = dirsToVisit.pop()
        print "Visiting dir", dirToVisit
        os.chdir(dirToVisit)
        currentVersion = getCurrentModuleVersion()
        defaultVersion = incrementLastDigit(currentVersion)
        version = raw_input("New version for {}:{} ({}):".format(dirToVisit, currentVersion, defaultVersion))
        if not version.strip():
            version = defaultVersion
        print "New version:", version
        output, err, exitcode = runVersionSet(version)
        rootDir = pattern.search(output).group(1)
        visitedDirs = visitedDirs + [dirToVisit]
        os.chdir(rootDir)
        print "Adding new dirs to visit"
        dirsToVisit = dirsToVisit + addChangedPoms(version, dirsToVisit, visitedDirs)

关于java - 更新依赖项中的版本时更新模块版本(多模块 maven ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48128234/

相关文章:

maven - 从命令行设置依赖项的版本

Maven 版本-maven-plugin 版本插件2.2 -- Maven 大叔情况

java - 本地时间有问题

java - Eclipse 内容支持不适用于 Groovy 文件中的 Java 对象

maven - 如何在 Gradle 中排除与分类器(平台版本)的依赖关系?

maven - 使用 Maven 时我的项目是否上传到存储库?

maven - 如何在 Maven 清理阶段删除文件夹?

maven-3 - mvn 版本 :set not updating child poms

java - Android - 具有形状可绘制对象和以编程方式渐变的自定义按钮

Java,如何重置 JTextField