• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2023 Google LLC
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#      http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15################################################################################
16
17# This script checks if the Maven direct dependencies of the given Bazel target
18# match the ones in the POM file. In case of discrepancies, it prints the diff
19# on standard output and exits with exit code 1.
20
21set -eo pipefail
22
23usage() {
24  echo "Usage: $0 [-h] [-e <excluded group ids>] <bazel target> <pom file>"
25  echo
26  echo " <bazel target>: The Bazel target that generate the JAR."
27  echo " <pom file>: The POM file path."
28  echo " -e: Maven artifact to be ignored (of the form package:artifact, e.g. com.google.protobuf:protobuf-java"
29  echo " -h: Show this help message."
30  exit 1
31}
32
33BAZEL_CMD="bazel"
34# Prefer using Bazelisk if available.
35if command -v "bazelisk" &> /dev/null; then
36  BAZEL_CMD="bazelisk"
37fi
38readonly BAZEL_CMD
39
40BAZEL_TARGET=
41POM_FILE=
42IGNORED_MAVEN_PACKAGE=
43process_params() {
44  while getopts "he:" opt; do
45    case "${opt}" in
46      e) IGNORED_MAVEN_PACKAGE="${OPTARG}" ;;
47      *) usage ;;
48    esac
49  done
50  shift $((OPTIND - 1))
51  readonly IGNORED_MAVEN_PACKAGE
52
53  BAZEL_TARGET="$1"
54  POM_FILE="$2"
55
56  readonly BAZEL_TARGET
57  readonly POM_FILE
58}
59
60process_params "$@"
61
62echo " === Obtaining Maven dependencies"
63
64readonly MAVEN_DIRECT_DEPS="$(mktemp)"
65mvn dependency:list -DoutputFile="${MAVEN_DIRECT_DEPS}" \
66  -DexcludeTransitive=true -f "${POM_FILE}" -q
67
68# Get the sorted list of deps and get them in the form:
69#    <groupId>:<artifactId>:<version>
70POM_FILE_DEPS="$(cat "${MAVEN_DIRECT_DEPS}" \
71  | grep compile | cut -d: -f1,2,4 | sed -E 's/^\s+//' | sort)"
72
73echo " === Obtaining Bazel dependencies"
74
75BAZEL_MAVEN_DEPS="$("${BAZEL_CMD}" query --output=build \
76  'attr(tags, .*,filter(@maven, deps('"${BAZEL_TARGET}"', 2)))' \
77    | grep maven_coordinates | cut -d'"' -f2 | cut -d'=' -f2 | sort)"
78
79if [[ ! -z "${IGNORED_MAVEN_PACKAGE}" ]]; then
80  BAZEL_MAVEN_DEPS=$(echo "${BAZEL_MAVEN_DEPS}" | grep -v -F "${IGNORED_MAVEN_PACKAGE}":)
81  POM_FILE_DEPS=$(echo "${POM_FILE_DEPS}" | grep -v -F "${IGNORED_MAVEN_PACKAGE}":)
82fi
83
84readonly BAZEL_MAVEN_DEPS
85readonly POM_FILE_DEPS
86
87if ! cmp -s <(echo "${BAZEL_MAVEN_DEPS}" ) <(echo "${POM_FILE_DEPS}"); then
88  echo "ERROR: There are the following mismatches between the dependencies in \
89${BAZEL_TARGET} and the ones in ${POM_FILE}:" >&2
90  echo
91  diff -y <(echo "${BAZEL_MAVEN_DEPS}" ) <(echo "${POM_FILE_DEPS}")
92  echo
93  exit 1
94fi
95