1#!/bin/bash 2# Copyright (C) 2024 The Dagger Authors. 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 16JAVA_HOME="$(cd "${JAVA_HOME}" && pwd)" # this is used outside of the root 17 18TMPDIR="$(mktemp -d)" 19for jar in "$@"; do 20 unzip -qq -B "${jar}" -d "${TMPDIR}" 21done 22 23pushd "${TMPDIR}" &>/dev/null 24 25find=(find) 26if [[ "$(uname -s)" == "Darwin" ]]; then 27 # Mac uses BSD find, which requires extra args for regex matching. 28 find+=(-E) 29 suffix='(~[0-9]*)?' 30else 31 # Default to GNU find, which must escape parentheses. 32 suffix='\(~[0-9]*\)?' 33fi 34 35# Concatenate similar files in META-INF that allow it. 36for meta_inf_pattern in services/.* ${MERGE_META_INF_FILES}; do 37 regex="META-INF/${meta_inf_pattern}${suffix}" 38 for file in $("${find[@]}" META-INF -regex "${regex}"); do 39 original="$(sed s/"~[0-9]*$"// <<< "${file}")" 40 if [[ "${file}" != "${original}" ]]; then 41 cat "${file}" >> "${original}" 42 rm "${file}" 43 fi 44 done 45done 46 47# build-data.properties is emitted by Bazel with target information that can 48# cause conflicts. Delete it since it doesn't make sense to keep in the merged 49# jar anyway. 50rm build-data.properties* 51rm META-INF/MANIFEST.MF* 52rm -rf META-INF/maven/ 53duplicate_files="$(find * -type f -regex '.*~[0-9]*$')" 54if [[ -n "${duplicate_files}" ]]; then 55 echo "Error: duplicate files in merged jar: ${duplicate_files}" 56 exit 1 57fi 58"${JAVA_HOME}/bin/jar" cf combined.jar * 59 60popd &>/dev/null 61 62# If the RULES_FILE exists and is not empty then run jarjar. 63# Otherwise, we're done so just copy the combined jar to the output file. 64if [[ -f "${RULES_FILE}" && -s "${RULES_FILE}" ]]; then 65 "${JARJAR}" process "${RULES_FILE}" "${TMPDIR}/combined.jar" "${OUTFILE}" 66else 67 cp $TMPDIR/combined.jar $OUTFILE 68fi 69 70rm -rf "${TMPDIR}" 71