Check MAVEN_PARAMS for -f flag in metadata fetch
[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
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 -E "\-f \S+" | awk '{ print $2 }')
17 if [ ! -z $file_path ]; then
18     pom_path="$file_path/pom.xml"
19 fi
20
21 # Ensure we fail the job if any steps fail.
22 set -xeu -o pipefail
23
24 project=$(xmlstarlet sel \
25     -N "x=http://maven.apache.org/POM/4.0.0" -t \
26     --if "/x:project/x:groupId" \
27       -v "/x:project/x:groupId" \
28     --elif "/x:project/x:parent/x:groupId" \
29       -v "/x:project/x:parent/x:groupId" \
30     --else -o "" $pom_path)
31 project_path="${project//.//}"
32
33 mkdir -p "$WORKSPACE/m2repo/$project_path"
34 pushd "$WORKSPACE/m2repo/$project_path"
35     # Temporarily disable failing for wget
36     # If 404 happens we don't care because it might be a new project.
37     set +e
38     wget -nv --recursive \
39          --accept maven-metadata.xml \
40          -R "index.html*" \
41          --execute robots=off \
42          --no-parent \
43          --no-host-directories \
44          --cut-dirs="$NEXUS_CUT_DIRS" \
45          "$NEXUS_URL/content/repositories/$NEXUS_REPO/$project_path/"
46     set -e  # Re-enable.
47 popd
48
49 # Backup metadata - Used later to find metadata files that have not been modified
50 mkdir -p "$WORKSPACE/m2repo-backup"
51 cp -a "$WORKSPACE/m2repo/"* "$WORKSPACE/m2repo-backup"