is there any way of making "mvn versions:display-plugin-updates" output dependencies in a structured format (e.g. CSV, XML or JSON) so that it can be easily parsed?
2 Answers
So, without any warranties, here is a piece of bash script thanks to a colleague of mine that takes an output from display-plugin-updates|display-dependency-updates and converts to a comma-separated list; supports multi-line-outputs and works for us
#!/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
I had the same problem and nowadays there is an easy solution.
First of all you have to use versions:dependency-updates-report instead of versions:display-dependency-updates.
./mvnw versions:dependency-updates-report
By default it generates an HTML file at target/site/dependency-updates-report.html. Change the configuration in your pom.xml to get the result in XML format.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.11.0</version>
<configuration>
<formats>xml</formats>
</configuration>
</plugin>
Now run the goal again and you should see the XML file at target/dependency-updates-report.xml.
I'm using dasel to work with this file in my CI/CD pipelines. The pipeline breaks a soon as I have an outdated dependency.
#!/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
I hope that helps you and anyone who might come here after some searching online. Let's make sure we're not using outdated dependencies in our Maven projects.