From f52936163e512a126befed7c091bcba94f9fc768 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Mon, 5 Feb 2018 15:03:01 -0500 Subject: [PATCH] Add maven metadata validator tool Add a tool to validate maven-metadata.xml to catch timestamp issues in builds and fail the build if a mismatch is detected. Issue: RELENG-772 Change-Id: I35a7b9c3b12321a3a7af24ab36397e38ebb01de9 Signed-off-by: Thanh Ha --- shell/common-variables.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++ shell/maven-deploy.sh | 2 +- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/shell/common-variables.sh b/shell/common-variables.sh index 6a5d2fb0..deb10589 100644 --- a/shell/common-variables.sh +++ b/shell/common-variables.sh @@ -29,3 +29,63 @@ lftools_activate() { source "/tmp/v/lftools/bin/activate" set -u # Restore unbound variable checking } + +# Check maven-metadata.xml for any unexpected timestamp mismatches +maven_metadata_validate() { + stage_dir="$1" + + if [ -z "$1" ]; then + echo "Usage: maven_metadata_validate STAGE_REPO_DIR" + exit 1 + fi + + error_detected=0 + mapfile -t files < <(find "$stage_dir" -name maven-metadata.xml | grep SNAPSHOT) + + for f in "${files[@]}"; do + timestamp=$(xmlstarlet sel \ + -t -v "/metadata/versioning/snapshot/timestamp" "$f") + + # Scan all snapshot versions but ignore javadoc and source jars + mapfile -t ext_timestamps < <(xmlstarlet sel \ + -t -m "/metadata/versioning/snapshotVersions/snapshotVersion" \ + -n \ + --if "classifier='javadoc'" \ + -o "" \ + --elif "classifier='sources'" \ + -o "" \ + --else \ + -o "extension:" -v extension \ + -o " value:" -v value \ + -o " updated:" -v updated \ + "$f") + + for t in "${ext_timestamps[@]}"; do + # Ignore blank timestamps caused by xmlstarlet ignores + if [[ -z "$t" ]]; then + continue + fi + + timestamp_error=0 + if [[ $t != *"$timestamp"* ]]; then + echo "Metadata $f 'value:$timestamp' mismatch vs '$t'" + timestamp_error=1 + fi + # Updated is timestamp without the dot character + if [[ $t != *"${timestamp//\./}"* ]]; then + echo "Metadata $f 'updated:${timestamp//\./}' mismatch vs '$t'" + timestamp_error=1 + fi + + if [[ $timestamp_error != 0 ]]; then + error_detected=1 + cat "$f" + fi + done + done + + if [ $error_detected -ne 0 ]; then + echo "ERROR: Mismatches in maven-metadata discovered. Quitting..." + exit 1 + fi +} diff --git a/shell/maven-deploy.sh b/shell/maven-deploy.sh index ac0f7293..c30963bb 100644 --- a/shell/maven-deploy.sh +++ b/shell/maven-deploy.sh @@ -39,5 +39,5 @@ fi set -u # Re-enable. find "$m2repo_dir" -type d -empty -delete - +maven_metadata_validate "$m2repo_dir" lftools deploy nexus -s "$nexus_repo_url" "$m2repo_dir" -- 2.16.6