Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / maven-fetch-metadata.sh
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 The Linux Foundation and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
11 echo "---> maven-fetch-metadata.sh"
12 # Uses wget to fetch a project's maven-metadata.xml files from a Maven repository.
13
14 # Check for "-f" maven param, indicating a change in pom location.
15 pom_path="pom.xml"
16 file_path=$(echo "$MAVEN_PARAMS" | grep -Eo "\-f \S+" | awk '{ print $2 }')
17 if [ -n "$file_path" ]; then
18     if [ -d "$file_path" ]; then
19         pom_path="$file_path/pom.xml"
20     else
21         pom_path="$file_path"
22     fi
23 fi
24
25 # Ensure we fail the job if any steps fail.
26 set -xeu -o pipefail
27
28 project=$(xmlstarlet sel \
29     -N "x=http://maven.apache.org/POM/4.0.0" -t \
30     --if "/x:project/x:groupId" \
31         -v "/x:project/x:groupId" \
32     --elif "/x:project/x:parent/x:groupId" \
33         -v "/x:project/x:parent/x:groupId" \
34     --else -o "" "$pom_path")
35 project_path="${project//.//}"
36
37 mkdir -p "$WORKSPACE/m2repo/$project_path"
38 pushd "$WORKSPACE/m2repo/$project_path"
39     # Temporarily disable failing for wget
40     # If 404 happens we don't care because it might be a new project.
41     set +e
42     wget -nv --recursive \
43          --accept maven-metadata.xml \
44          -R "index.html*" \
45          --execute robots=off \
46          --no-parent \
47          --no-host-directories \
48          --cut-dirs="$NEXUS_CUT_DIRS" \
49          "$NEXUS_URL/content/repositories/$NEXUS_REPO/$project_path/"
50     set -e  # Re-enable.
51 popd
52
53 # Backup metadata - Used later to find metadata files that have not been modified
54 mkdir -p "$WORKSPACE/m2repo-backup"
55 cp -a "$WORKSPACE/m2repo/"* "$WORKSPACE/m2repo-backup"