maven mvn版本:显示插件更新结构化输出

tsm1rwdh  于 2023-01-04  发布在  Maven
关注(0)|答案(3)|浏览(158)

有没有什么方法可以使“mvn版本:显示插件更新”输出依赖关系以结构化格式(例如CSV,XML或JSON),以便它可以很容易地被解析?

kzipqqlq

kzipqqlq1#

因此,没有任何担保,这里有一段bash脚本,这要感谢我的一个同事,它从display-plugin-updates中获取输出|display-dependency-更新并转换为逗号分隔的列表;支持多行输出,为我们工作

#!/bin/bash

source_file=/opt/report.txt
target_file=/opt/comma_report.txt

#Clean source file from "[INFO]"
sed 's/\[INFO\] //g' $source_file > /tmp/clean_report.txt

#Join next line to the previous one if it starts with more than 3 spaces
sed -i -E ':a ; $!N ; s/\n^   / / ; ta ; P ; D' /tmp/clean_report.txt

#Clean file from more than 2 spaces
sed -i 's/  *//g' /tmp/clean_report.txt

#Grep for arrows, sort and send result to grepped_report.txt
grep "\->" /tmp/clean_report.txt | awk '!x[$0]++' > /tmp/grepped_report.txt

#Clean file from ".RELEASE"
sed -i 's/.RELEASE//g' /tmp/grepped_report.txt

#Replace arrows with commas
sed -i 's/->/,/g' /tmp/grepped_report.txt

#Replace more than 2 dots with comma and send the output to target file
sed -r -e 's/\.{3,}/,/g' /tmp/grepped_report.txt > $target_file

#Delete tmp files
rm /tmp/clean_report.txt
rm /tmp/grepped_report.txt

#The end
5ktev3wc

5ktev3wc2#

我有同样的问题,现在有一个简单的解决办法。
首先,您必须使用versions:dependency-updates-report而不是versions:display-dependency-updates

./mvnw versions:dependency-updates-report

默认情况下,它在target/site/dependency-updates-report.html中生成一个HTML文件。更改pom.xml中的配置以获得XML格式的结果。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>versions-maven-plugin</artifactId>
    <version>2.11.0</version>
    <configuration>
        <formats>xml</formats>
    </configuration>
</plugin>

现在再次运行目标,您应该在target/dependency-updates-report.xml处看到XML文件。
我在CI/CD管道中使用dasel来处理这个文件。当我有一个过时的依赖项时,管道就会中断。

#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

# run maven to generate dependency report
./mvnw versions:dependency-updates-report

# use default report location
REPORT="target/dependency-updates-report.xml"

# get the number of dependencies using the latest version
NEXT_VERSION_AVAILABLE=$(cat ${REPORT} | dasel --read xml ".DependencyUpdatesReport.summary.nextVersionAvailable")
NEXT_INCREMENTAL_AVAILABLE=$(cat ${REPORT} | dasel --read xml ".DependencyUpdatesReport.summary.nextIncremetalAvailable")
NEXT_MINOR_AVAILABLE=$(cat ${REPORT} | dasel --read xml ".DependencyUpdatesReport.summary.nextMinorAvailable")
NEXT_MAJOR_AVAILABLE=$(cat ${REPORT} | dasel --read xml ".DependencyUpdatesReport.summary.nextMajorAvailable")

# compare number of outdated dependencies with our threshold
if [ "${NEXT_VERSION_AVAILABLE}" -ne 0 ]; then
    echo "Error: ${NEXT_VERSION_AVAILABLE} next versions are available"
    exit 1
elif [ "${NEXT_INCREMENTAL_AVAILABLE}" -ne 0 ]; then
    echo "Error: ${NEXT_INCREMENTAL_AVAILABLE} next incremental versions are available"
    exit 1
elif [ "${NEXT_MINOR_AVAILABLE}" -ne 0 ]; then
    echo "Error: ${NEXT_MINOR_AVAILABLE} next minor versions are available"
    exit 1
elif [ "${NEXT_MAJOR_AVAILABLE}" -ne 0 ]; then
    echo "Error: ${NEXT_MAJOR_AVAILABLE} next major versions are available"
    exit 1
fi

我希望这对您和任何在线搜索后可能访问这里的人有所帮助。让我们确保在Maven项目中没有使用过时的依赖项。

eqqqjvef

eqqqjvef3#

我正在使用下面的命令来列出一个项目的依赖关系在CSV格式对一个pom文件,你可以调整命令与versions:display-plugin-updates根据您的需要-

dependencyVersions=`mvn -U versions:display-dependency-updates -Dversions.outputLineWidth=120 -DprocessDependencyManagement=false -DprocessPluginDependenciesInPluginManagement=false|grep " -> "|awk '{print $2","$4","$6}'`;
while IFS= read -r line; do echo $line; done< <(printf '%s\n' "$dependencyVersions")
  • -U =强制检查来自远程的更新
  • versions:display-dependency-updates =列出所有依存关系更新
  • -Dversions.outputLineWidth=120 =所需版本插件版本〉2.10.0
  • -DprocessDependencyManagement=false-DprocessPluginDependenciesInPluginManagement=false =从列表中排除

while部分逐行打印输出

相关问题