|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright (c) 2025 Oracle and/or its affiliates. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +set -o pipefail || true # trace ERR through pipes |
| 19 | +set -o errtrace || true # trace ERR through commands and functions |
| 20 | +set -o errexit || true # exit the script if any statement returns a non-true return value |
| 21 | + |
| 22 | +on_error(){ |
| 23 | + CODE="${?}" && \ |
| 24 | + set +x && \ |
| 25 | + printf "[ERROR] Error(code=%s) occurred at %s:%s command: %s\n" \ |
| 26 | + "${CODE}" "${BASH_SOURCE[0]}" "${LINENO}" "${BASH_COMMAND}" >&2 |
| 27 | +} |
| 28 | +trap on_error ERR |
| 29 | + |
| 30 | +usage(){ |
| 31 | + cat <<EOF |
| 32 | +
|
| 33 | +DESCRIPTION: Create a Maven settings.xml file that includes central staging repo so we can use it for testing. |
| 34 | +
|
| 35 | +USAGE: |
| 36 | +
|
| 37 | +$(basename "${0}") --dir=D |
| 38 | +
|
| 39 | + --dir=path |
| 40 | + Directory to place settings.xml file in. Defaults to ${HOME}/.m2 |
| 41 | +EOF |
| 42 | +} |
| 43 | + |
| 44 | +# parse command line args |
| 45 | +ARGS=( "${@}" ) |
| 46 | +for ((i=0;i<${#ARGS[@]};i++)) |
| 47 | +{ |
| 48 | + ARG=${ARGS[${i}]} |
| 49 | + case ${ARG} in |
| 50 | + "--dir="*) |
| 51 | + DESTINATION_DIRECTORY=${ARG#*=} |
| 52 | + ;; |
| 53 | + *) |
| 54 | + echo "ERROR: unknown argument: ${ARG}" |
| 55 | + exit 1 |
| 56 | + ;; |
| 57 | + esac |
| 58 | +} |
| 59 | + |
| 60 | +if [ -z "${CENTRAL_USER}" ] ; then |
| 61 | + echo "ERROR: environment variable CENTRAL_USER is required." >&2 |
| 62 | + usage |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +if [ -z "${CENTRAL_PASSWORD}" ] ; then |
| 67 | + echo "ERROR: environment variable CENTRAL_PASSWORD is required." >&2 |
| 68 | + usage |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | + |
| 72 | +if [ -z "${DESTINATION_DIRECTORY}" ] ; then |
| 73 | + DESTINATION_DIRECTORY="${HOME}/.m2" |
| 74 | + mkdir -p "${DESTINATION_DIRECTORY}" |
| 75 | +fi |
| 76 | + |
| 77 | +if [ ! -d "${DESTINATION_DIRECTORY}" ] ; then |
| 78 | + echo "ERROR: destination directory ${DESTINATION_DIRECTORY} does not exist or is not a directory" >&2 |
| 79 | + usage |
| 80 | + exit 1 |
| 81 | +fi |
| 82 | + |
| 83 | +readonly DESTINATION_DIRECTORY |
| 84 | + |
| 85 | +if [ -e "${DESTINATION_DIRECTORY}/settings.xml" ]; then |
| 86 | + echo "WARNING: ${DESTINATION_DIRECTORY}/settings.xml already exists. Copying to ${DESTINATION_DIRECTORY}/settings.xml.bk" >&2 |
| 87 | + cp "${DESTINATION_DIRECTORY}/settings.xml" "${DESTINATION_DIRECTORY}/settings.xml.bk" |
| 88 | +fi |
| 89 | + |
| 90 | +# Create credential needed to access Maven Central Publishing portal |
| 91 | +BEARER=$(printf "%s:%s" "${CENTRAL_USER}" "${CENTRAL_PASSWORD}" | base64) |
| 92 | +readonly BEARER |
| 93 | + |
| 94 | +maven_settings() { |
| 95 | + cat <<EOF |
| 96 | + <settings> |
| 97 | + <servers> |
| 98 | + <server> |
| 99 | + <id>central.manual.testing</id> |
| 100 | + <configuration> |
| 101 | + <httpHeaders> |
| 102 | + <property> |
| 103 | + <name>Authorization</name> |
| 104 | + <value>Bearer ${BEARER}</value> |
| 105 | + </property> |
| 106 | + </httpHeaders> |
| 107 | + </configuration> |
| 108 | + </server> |
| 109 | + </servers> |
| 110 | + <profiles> |
| 111 | + <profile> |
| 112 | + <id>central.manual.testing</id> |
| 113 | + <repositories> |
| 114 | + <repository> |
| 115 | + <id>central.manual.testing</id> |
| 116 | + <name>Central Testing repository</name> |
| 117 | + <url>https://central.sonatype.com/api/v1/publisher/deployments/download</url> |
| 118 | + </repository> |
| 119 | + </repositories> |
| 120 | + <pluginRepositories> |
| 121 | + <pluginRepository> |
| 122 | + <id>central.manual.testing</id> |
| 123 | + <name>Central Testing repository</name> |
| 124 | + <url>https://central.sonatype.com/api/v1/publisher/deployments/download</url> |
| 125 | + </pluginRepository> |
| 126 | + </pluginRepositories> |
| 127 | + </profile> |
| 128 | + </profiles> |
| 129 | +</settings> |
| 130 | +EOF |
| 131 | +} |
| 132 | + |
| 133 | +setup_central_settings() { |
| 134 | + echo "INFO: creating settings.xml in ${DESTINATION_DIRECTORY}" |
| 135 | + maven_settings > "${DESTINATION_DIRECTORY}/settings.xml" |
| 136 | +} |
| 137 | + |
| 138 | +setup_central_settings |
0 commit comments