Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 3 | # ============LICENSE_START======================================================= |
| 4 | # ONAP |
| 5 | # ================================================================================ |
jhh | 3f563fe | 2020-03-05 22:32:58 -0600 | [diff] [blame^] | 6 | # Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved. |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 7 | # Modifications Copyright (C) 2020 Bell Canada. |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 8 | # ================================================================================ |
| 9 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | # you may not use this file except in compliance with the License. |
| 11 | # You may obtain a copy of the License at |
| 12 | # |
| 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | # |
| 15 | # Unless required by applicable law or agreed to in writing, software |
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | # See the License for the specific language governing permissions and |
| 19 | # limitations under the License. |
| 20 | # ============LICENSE_END========================================================= |
jhh | 3f563fe | 2020-03-05 22:32:58 -0600 | [diff] [blame^] | 21 | |
| 22 | source ${POLICY_HOME}/etc/profile.d/env.sh |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 23 | |
| 24 | ############################################################################## |
| 25 | # Usage: usage |
| 26 | ############################################################################## |
| 27 | |
| 28 | function usage() { |
| 29 | echo |
| 30 | echo -e "syntax: $(basename "$0") " |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 31 | echo -e "\t [-f|-l|-d]" |
| 32 | echo -e "\t -s <custom-settings> " |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 33 | echo -e "\t -a <artifact> " |
| 34 | echo |
| 35 | echo -e "Options:" |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 36 | echo -e "\t -f|--file-repo: deploy in the file repository" |
| 37 | echo -e "\t -l|--local-repo: install in the local repository" |
| 38 | echo -e "\t -d|--dependencies: install dependencies in the local repository" |
| 39 | echo -e "\t -s|--settings: custom settings.xml" |
| 40 | echo -e "\t -a|--artifact: file artifact (jar or pom) to deploy and/or install" |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 41 | echo |
| 42 | echo |
| 43 | } |
| 44 | |
| 45 | ############################################################################## |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 46 | # Usage: init <artifact> |
| 47 | # |
| 48 | # If the artifact is a jar, this function extracts the maven metadata for |
| 49 | # consumption in this script. |
| 50 | # |
| 51 | # As a result the global variables will be set: |
| 52 | # WORKING_DIR: working directory with extracted files. |
| 53 | # WORKING_POM: pom file location |
| 54 | # WORKING_POM_PROPERTIES: pom properties file location |
| 55 | ############################################################################## |
| 56 | |
| 57 | function init |
| 58 | { |
| 59 | if [[ ${DEBUG} == y ]]; then |
| 60 | echo "-- ${FUNCNAME[0]} $* --" |
| 61 | set -x |
| 62 | fi |
| 63 | |
| 64 | local artifact="${1}" |
| 65 | if [[ ! -f ${artifact} ]]; then |
| 66 | echo "${artifact}: artifact does not exist" |
| 67 | return 1 |
| 68 | fi |
| 69 | |
| 70 | if [[ ${artifact} != *.jar ]]; then |
| 71 | return 0 |
| 72 | fi |
| 73 | |
| 74 | local dir=$(mktemp -d) |
| 75 | local jar="${artifact##*/}" |
| 76 | |
| 77 | WORKING_DIR=$(realpath "${dir}") |
| 78 | |
| 79 | cp -p "${artifact}" "${WORKING_DIR}/${jar}" |
| 80 | pushd "${dir}" |
| 81 | |
| 82 | local rc=0 |
| 83 | |
| 84 | # determine name of 'pom' file within JAR |
| 85 | local pom=$(jar tf "${jar}" META-INF | grep '/pom\.xml$' | head -1) |
| 86 | if [[ -n ${pom} ]] ; then |
| 87 | jar xf "${jar}" "${pom}" |
| 88 | WORKING_POM=$(realpath "${pom}") |
| 89 | else |
| 90 | echo "${artifact}: pom not found" |
| 91 | fi |
| 92 | |
| 93 | local pomProperties=$(jar tf "${jar}" META-INF | grep '/pom\.properties$' | head -1) |
| 94 | if [[ -n ${pomProperties} ]]; then |
| 95 | jar xf "${jar}" "${pomProperties}" |
| 96 | WORKING_POM_PROPERTIES=$(realpath ${pomProperties}) |
| 97 | source "${WORKING_POM_PROPERTIES}" |
| 98 | echo "${artifact}: sourcing in ${WORKING_POM_PROPERTIES}" |
| 99 | else |
| 100 | echo "${artifact}: pom.properties not found" |
| 101 | if [[ -n ${WORKING_POM} ]]; then |
| 102 | if ! getPomAttributes "${WORKING_POM}" artifactId groupId version ; then |
| 103 | echo "${WORKING_POM}: cannot extract maven coordinates" |
| 104 | rc=1 |
| 105 | fi |
| 106 | else |
| 107 | echo "${artifact}: cannot extract maven coordinates" |
| 108 | rc=1 |
| 109 | fi |
| 110 | fi |
| 111 | |
| 112 | if [[ -z ${version} ]] || [[ -z ${groupId} ]] || [[ -z ${artifactId} ]]; then |
| 113 | echo "${artifact}: some coordinates cannot be extracted" |
| 114 | rc=1 |
| 115 | fi |
| 116 | |
| 117 | echo "${artifact}: coordinates ${groupId}:${artifactId}:${version}" |
| 118 | popd |
| 119 | |
| 120 | return ${rc} |
| 121 | } |
| 122 | |
| 123 | ############################################################################## |
| 124 | # Usage: cleanup |
| 125 | # |
| 126 | # Clean up temporary resources. |
| 127 | ############################################################################## |
| 128 | |
| 129 | function cleanup |
| 130 | { |
| 131 | if [[ ${DEBUG} == y ]]; then |
| 132 | echo "-- ${FUNCNAME[0]} $* --" |
| 133 | set -x |
| 134 | fi |
| 135 | |
| 136 | if [[ -n ${WORKING_DIR} ]]; then |
| 137 | rm -rf "${WORKING_DIR}" |
| 138 | fi |
| 139 | } |
| 140 | |
| 141 | ############################################################################## |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 142 | # Usage: getPomAttributes <pom-file> <attribute> ... |
| 143 | # |
| 144 | # This function performs simplistic parsing of a 'pom.xml' file, extracting |
| 145 | # the specified attributes (e.g. 'groupId', 'artifactId', 'version'). The |
| 146 | # attributes are returned as environment variables with the associated name |
| 147 | ############################################################################## |
| 148 | |
| 149 | function getPomAttributes |
| 150 | { |
| 151 | if [[ ${DEBUG} == y ]]; then |
| 152 | echo "-- ${FUNCNAME[0]} $* --" |
| 153 | set -x |
| 154 | fi |
| 155 | |
| 156 | local file="$1" |
| 157 | if [[ ! -f "${file}" ]]; then |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 158 | echo "${file}: file does not exist" |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 159 | return 1 |
| 160 | fi |
| 161 | |
| 162 | local tab=$'\t' rval=0 attr value |
| 163 | shift |
| 164 | |
| 165 | for attr in "$@" ; do |
| 166 | # Try to fetch the parameter associated with the 'pom.xml' file. |
| 167 | # Initially, the 'parent' element is excluded. If the desired |
| 168 | # parameter is not found, the 'parent' element is included in the |
| 169 | # second attempt. |
| 170 | value=$(sed -n \ |
| 171 | -e '/<parent>/,/<\/parent>/d' \ |
| 172 | -e '/<dependencies>/,/<\/dependencies>/d' \ |
| 173 | -e '/<build>/,/<\/build>/d' \ |
| 174 | -e '/<profiles>/,/<\/profiles>/d' \ |
| 175 | -e '/<description>/,/<\/description>/d' \ |
| 176 | -e '/<packaging>/,/<\/packaging>/d' \ |
| 177 | -e '/<modelVersion>/,/<\/modelVersion>/d' \ |
| 178 | -e '/<properties>/,/<\/properties>/d' \ |
| 179 | -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \ |
| 180 | <"${file}") |
| 181 | |
| 182 | if [[ "${value}" == "" ]]; then |
| 183 | # need to check parent for parameter |
| 184 | value=$(sed -n \ |
| 185 | -e '/<dependencies>/,/<\/dependencies>/d' \ |
| 186 | -e '/<build>/,/<\/build>/d' \ |
| 187 | -e '/<profiles>/,/<\/profiles>/d' \ |
| 188 | -e '/<description>/,/<\/description>/d' \ |
| 189 | -e '/<packaging>/,/<\/packaging>/d' \ |
| 190 | -e '/<modelVersion>/,/<\/modelVersion>/d' \ |
| 191 | -e '/<properties>/,/<\/properties>/d' \ |
| 192 | -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \ |
| 193 | <"${file}") |
| 194 | |
| 195 | if [[ "${value}" == "" ]] ; then |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 196 | echo "${file}: Can't determine ${attr}" |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 197 | rval=1 |
| 198 | fi |
| 199 | fi |
| 200 | |
| 201 | # the following sets an environment variable with the name referred |
| 202 | # to by ${attr} |
| 203 | read "${attr}" <<<"${value}" |
| 204 | done |
| 205 | return ${rval} |
| 206 | } |
| 207 | |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 208 | ############################################################################## |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 209 | # Usage: setMavenProxyArgs |
| 210 | # |
| 211 | # This function performs parsing of http proxy environment variable if provided, |
| 212 | # extracting the attributes such as proxy host, port, username and password. |
| 213 | # These proxy attributes are set into the global variable for maven proxy |
| 214 | # settings to be used as build arguments with maven commands. |
| 215 | ############################################################################## |
| 216 | |
| 217 | function setMavenProxyArgs |
| 218 | { |
| 219 | if [[ ${DEBUG} == y ]]; then |
| 220 | echo "-- ${FUNCNAME[0]} $* --" |
| 221 | set -x |
| 222 | fi |
| 223 | |
| 224 | if [[ -z ${http_proxy} ]]; then |
| 225 | return 0 |
| 226 | fi |
| 227 | |
Rashmi Pujar | 9d5791a | 2020-01-31 10:33:13 -0500 | [diff] [blame] | 228 | local proxy_creds="${http_proxy#*//}" |
| 229 | local proxy="${proxy_creds#*@}" |
| 230 | local host="${proxy%:*}" |
| 231 | local port="${proxy#*:}" |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 232 | MVN_PROXY_SETTINGS="-DproxyHost=${host} -DproxyPort=${port}" |
| 233 | |
| 234 | if [[ "$proxy_creds" == *"@"* ]]; then |
Rashmi Pujar | 9d5791a | 2020-01-31 10:33:13 -0500 | [diff] [blame] | 235 | local creds="${proxy_creds%%@*}" |
| 236 | local username="${creds%:*}" |
| 237 | local password="${creds#*:}" |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 238 | MVN_PROXY_SETTINGS+=" -DproxyUsername=${username} -DproxyPassword=${password}" |
| 239 | fi |
| 240 | } |
| 241 | |
| 242 | ############################################################################## |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 243 | # Usage: deployJar <jar-file> |
| 244 | # |
| 245 | # This function deploys a JAR file in a repository, as well as |
| 246 | # the 'pom.xml' member it contains. |
| 247 | ################################################################# |
| 248 | |
| 249 | function deployJar |
| 250 | { |
| 251 | if [[ ${DEBUG} == y ]]; then |
| 252 | echo "-- ${FUNCNAME[0]} $* --" |
| 253 | set -x |
| 254 | fi |
| 255 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 256 | local file="${1}" |
| 257 | |
| 258 | if [[ ! -f ${file} ]]; then |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 259 | return 1 |
| 260 | fi |
| 261 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 262 | local repoId repoUrl |
| 263 | if [[ "${version}" =~ SNAPSHOT ]] ; then |
| 264 | repoId=${SNAPSHOT_REPOSITORY_ID} |
| 265 | repoUrl=${SNAPSHOT_REPOSITORY_URL} |
| 266 | else |
| 267 | repoId=${RELEASE_REPOSITORY_ID} |
| 268 | repoUrl=${RELEASE_REPOSITORY_URL} |
| 269 | fi |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 270 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 271 | if [[ -z ${repoUrl} ]] || [[ -z ${repoId} ]]; then |
| 272 | echo "{file}: no repository id/url to deploy jar" |
| 273 | return 1 |
| 274 | fi |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 275 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 276 | echo "${file}: deploying jar artifact to repository ${repoId}: ${repoUrl}" |
| 277 | echo "${file}: coordinates ${groupId} ${artifactId} ${version}" |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 278 | |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 279 | mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} deploy:deploy-file \ |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 280 | -Dfile="${file}" \ |
| 281 | -Dversion="${version}" \ |
| 282 | -Dpackaging=jar \ |
| 283 | -DgeneratePom=false \ |
| 284 | -DpomFile="${WORKING_POM}" \ |
| 285 | -DrepositoryId="${repoId}" \ |
| 286 | -Durl="${repoUrl}" \ |
| 287 | -DupdateReleaseInfo=true |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 288 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 289 | return ${?} |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | ############################################################################## |
| 293 | # Usage: deployPom <pom-file> |
| 294 | # |
| 295 | # This function deploys a 'pom.xml' file in the local repository |
| 296 | ############################################################################## |
| 297 | |
| 298 | function deployPom |
| 299 | { |
| 300 | if [[ ${DEBUG} == y ]]; then |
| 301 | echo "-- ${FUNCNAME[0]} $* --" |
| 302 | set -x |
| 303 | fi |
| 304 | |
| 305 | local file="${1}" |
| 306 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 307 | if [[ ! -f ${file} ]]; then |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 308 | return 1 |
| 309 | fi |
| 310 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 311 | if ! getPomAttributes "${file}" artifactId groupId version ; then |
| 312 | echo "${file}: cannot deploy pom due to missing attributes" |
| 313 | return 1 |
| 314 | fi |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 315 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 316 | local repoId repoUrl |
| 317 | if [[ "${version}" =~ SNAPSHOT ]] ; then |
| 318 | repoId=${SNAPSHOT_REPOSITORY_ID} |
| 319 | repoUrl=${SNAPSHOT_REPOSITORY_URL} |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 320 | else |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 321 | repoId=${RELEASE_REPOSITORY_ID} |
| 322 | repoUrl=${RELEASE_REPOSITORY_URL} |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 323 | fi |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 324 | |
| 325 | echo "${file}: deploying pom artifact to repository ${repoId}: ${repoUrl}" |
| 326 | echo "${file}: coordinates ${groupId} ${artifactId} ${version}" |
| 327 | |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 328 | mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} deploy:deploy-file \ |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 329 | -Dfile="${file}" \ |
| 330 | -Dpackaging=pom \ |
| 331 | -DgeneratePom=false \ |
| 332 | -DgroupId="${groupId}" \ |
| 333 | -DartifactId="${artifactId}" \ |
| 334 | -Dversion="${version}" \ |
| 335 | -DrepositoryId="${repoId}" \ |
| 336 | -Durl="${repoUrl}" \ |
| 337 | -DupdateReleaseInfo=true |
| 338 | |
| 339 | return ${?} |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | ############################################################################## |
| 343 | # Usage: deployArtifact |
| 344 | # |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 345 | # This function deploys a maven artifact in a repository |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 346 | ############################################################################## |
| 347 | |
| 348 | function deployArtifact |
| 349 | { |
| 350 | if [[ ${DEBUG} == y ]]; then |
| 351 | echo "-- ${FUNCNAME[0]} $* --" |
| 352 | set -x |
| 353 | fi |
| 354 | |
| 355 | local file="${1}" |
| 356 | if [[ -z "${file}" ]]; then |
| 357 | echo "${file}: artifact file not provided" |
| 358 | return 1 |
| 359 | fi |
| 360 | |
| 361 | if [[ ! -f "${file}" ]]; then |
| 362 | echo "${file}: artifact file does not exist" |
| 363 | return 1 |
| 364 | fi |
| 365 | |
| 366 | case "${file}" in |
| 367 | *pom.xml|*.pom) |
| 368 | deployPom "${file}" |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 369 | return ${?} |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 370 | ;; |
| 371 | *.jar) |
| 372 | deployJar "${file}" |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 373 | return ${?} |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 374 | ;; |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 375 | *) echo "${file}: Don't know how to deploy artifact" |
| 376 | return 1 |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 377 | ;; |
| 378 | esac |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | ############################################################################## |
| 382 | # Usage: installJar <artifact-file> |
| 383 | # |
| 384 | # This function installs a jar packaged artifact in the local repository |
| 385 | ############################################################################## |
| 386 | |
| 387 | function installJar |
| 388 | { |
| 389 | if [[ ${DEBUG} == y ]]; then |
| 390 | echo "-- ${FUNCNAME[0]} $* --" |
| 391 | set -x |
| 392 | fi |
| 393 | |
| 394 | local file="${1}" |
| 395 | |
| 396 | if [[ ! -f ${file} ]]; then |
| 397 | return 1 |
| 398 | fi |
| 399 | |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 400 | mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} \ |
| 401 | org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file -Dfile="${file}" |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 402 | |
| 403 | return $? |
| 404 | } |
| 405 | |
| 406 | ############################################################################## |
| 407 | # Usage: installPom <pom-file> |
| 408 | # |
| 409 | # This function installs a pom file in the local repository |
| 410 | ############################################################################## |
| 411 | |
| 412 | function installPom |
| 413 | { |
| 414 | if [[ ${DEBUG} == y ]]; then |
| 415 | echo "-- ${FUNCNAME[0]} $* --" |
| 416 | set -x |
| 417 | fi |
| 418 | |
| 419 | local file="${1}" |
| 420 | |
| 421 | if [[ ! -f ${file} ]]; then |
| 422 | return 1 |
| 423 | fi |
| 424 | |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 425 | mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} \ |
| 426 | org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file \ |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 427 | -Dpackaging=pom \ |
| 428 | -Dfile="${file}" \ |
| 429 | -DpomFile="${file}" |
| 430 | |
| 431 | return $? |
| 432 | } |
| 433 | |
| 434 | ############################################################################## |
| 435 | # Usage: installArtifact |
| 436 | # |
| 437 | # This function installs a maven artifacts in the local repository |
| 438 | ############################################################################## |
| 439 | |
| 440 | function installArtifact |
| 441 | { |
| 442 | if [[ ${DEBUG} == y ]]; then |
| 443 | echo "-- ${FUNCNAME[0]} $* --" |
| 444 | set -x |
| 445 | fi |
| 446 | |
| 447 | local file="${1}" |
| 448 | if [[ -z "${file}" ]]; then |
| 449 | echo "${file}: artifact file not provided" |
| 450 | return 1 |
| 451 | fi |
| 452 | |
| 453 | if [[ ! -f "${file}" ]]; then |
| 454 | echo "${file}: artifact file does not exist" |
| 455 | return 1 |
| 456 | fi |
| 457 | |
| 458 | case "${file}" in |
| 459 | *pom.xml|*.pom) |
| 460 | installPom "${file}" |
| 461 | return ${?} |
| 462 | ;; |
| 463 | *.jar) |
| 464 | installJar "${file}" |
| 465 | return ${?} |
| 466 | ;; |
| 467 | *) echo "${file}: Don't know how to install the artifact" |
| 468 | return 1 |
| 469 | ;; |
| 470 | esac |
| 471 | } |
| 472 | |
| 473 | ############################################################################## |
| 474 | # Usage: installDependencies <pom-file> |
| 475 | # |
| 476 | # This function installs the dependencies of an artifact in the file or |
| 477 | # local repository |
| 478 | ############################################################################## |
| 479 | |
| 480 | function installDependencies |
| 481 | { |
| 482 | if [[ ${DEBUG} == y ]]; then |
| 483 | echo "-- ${FUNCNAME[0]} $* --" |
| 484 | set -x |
| 485 | fi |
| 486 | |
| 487 | local file="${1}" |
| 488 | |
| 489 | if [[ ! -f ${file} ]]; then |
| 490 | return 1 |
| 491 | fi |
| 492 | |
| 493 | if [[ -z ${DEPENDENCY_REPO_URL} ]]; then |
| 494 | echo "${file}: no repo url to install dependencies" |
| 495 | return 1 |
| 496 | fi |
| 497 | |
| 498 | echo "${file}: deploying dependencies from repository ${DEPENDENCY_REPO_URL}" |
| 499 | echo "${file}: coordinates ${groupId} ${artifactId} ${version}" |
| 500 | |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 501 | mvn ${CUSTOM_SETTINGS} ${MVN_PROXY_SETTINGS} \ |
| 502 | org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \ |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 503 | -DartifactId="${artifactId}" \ |
| 504 | -DgroupId="${groupId}" \ |
| 505 | -Dversion="${version}" \ |
| 506 | -DremoteRepositories="${DEPENDENCY_REPO_URL}" |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 507 | |
| 508 | return ${?} |
| 509 | } |
| 510 | |
| 511 | ############################################################################## |
| 512 | # MAIN |
| 513 | ############################################################################## |
| 514 | |
| 515 | if [[ ${DEBUG} == y ]]; then |
| 516 | echo "-- $0 $* --" |
| 517 | set -x |
| 518 | fi |
| 519 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 520 | # reset globals |
| 521 | |
| 522 | unset ARTIFACT_FILE |
| 523 | unset LOCAL_INSTALL |
| 524 | unset INSTALL_DEPS |
| 525 | unset FILE_REPO_INSTALL |
| 526 | unset WORKING_DIR |
| 527 | unset WORKING_POM |
| 528 | unset WORKING_POM_PROPERTIES |
| 529 | unset DEPENDENCY_REPO_URL |
| 530 | unset SETTINGS_FILE |
| 531 | unset CUSTOM_SETTINGS |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 532 | unset MVN_PROXY_SETTINGS |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 533 | |
| 534 | # process input |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 535 | |
| 536 | until [[ -z "$1" ]]; do |
| 537 | case $1 in |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 538 | -a|--artifact) shift |
| 539 | ARTIFACT_FILE=$1 |
| 540 | ;; |
| 541 | -s|--settings) shift |
| 542 | SETTINGS_FILE=$1 |
| 543 | ;; |
| 544 | -l|--local-repo) LOCAL_INSTALL="true" |
| 545 | ;; |
| 546 | -d|--dependencies) INSTALL_DEPS="true" |
| 547 | ;; |
| 548 | -f|--file-repo) FILE_REPO_INSTALL="true" |
| 549 | ;; |
| 550 | *) usage |
| 551 | exit 1 |
| 552 | ;; |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 553 | esac |
| 554 | shift |
| 555 | done |
| 556 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 557 | if [[ -z ${ARTIFACT_FILE} ]]; then |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 558 | echo "No artifact file provided: $*" |
| 559 | usage |
| 560 | exit 1 |
| 561 | fi |
| 562 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 563 | if [[ -n ${SETTINGS_FILE} ]]; then |
| 564 | CUSTOM_SETTINGS="--settings=${SETTINGS_FILE}" |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 565 | fi |
| 566 | |
Rashmi Pujar | bb03caf | 2020-01-24 15:34:04 -0500 | [diff] [blame] | 567 | # Set proxy attributes into MVN_PROXY_SETTINGS variable |
| 568 | setMavenProxyArgs |
| 569 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 570 | # retval has the count of failed operations |
| 571 | |
| 572 | retval=0 |
| 573 | |
| 574 | # initialize |
| 575 | |
| 576 | init "${ARTIFACT_FILE}" |
| 577 | retval=$? |
| 578 | if [[ ${retval} != 0 ]]; then |
| 579 | cleanup |
| 580 | exit ${retval} |
| 581 | fi |
| 582 | |
| 583 | # remote repo deploy operation |
| 584 | # |
| 585 | # SNAPSHOT_REPOSITORY_URL and RELEASE_REPOSITORY_URL |
| 586 | # are pre-existing environmental variables (base.conf) |
| 587 | |
| 588 | if [[ -n ${SNAPSHOT_REPOSITORY_URL} ]] || [[ -n ${RELEASE_REPOSITORY_URL} ]]; then |
| 589 | deployArtifact "${ARTIFACT_FILE}" |
| 590 | retval=$(( retval + ${?} )) |
| 591 | fi |
| 592 | |
| 593 | # deploy in file repository |
| 594 | |
| 595 | if [[ -n ${FILE_REPO_INSTALL} ]]; then |
| 596 | FILE_REPO_ID="file-repository" |
| 597 | FILE_REPO_URL="file:${HOME}/.m2/file-repository" |
| 598 | |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 599 | SNAPSHOT_REPOSITORY_ID="${FILE_REPO_ID}" |
| 600 | SNAPSHOT_REPOSITORY_URL="${FILE_REPO_URL}" |
| 601 | RELEASE_REPOSITORY_ID="${FILE_REPO_ID}" |
| 602 | RELEASE_REPOSITORY_URL="${FILE_REPO_URL}" |
| 603 | |
| 604 | mkdir -p "${FILE_REPO_URL#file:}" 2> /dev/null |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 605 | deployArtifact "${ARTIFACT_FILE}" |
| 606 | retval=$(( retval + ${?} )) |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 607 | fi |
| 608 | |
jhh | 4112fec | 2019-12-09 18:11:13 -0600 | [diff] [blame] | 609 | # install in local repository |
| 610 | |
| 611 | if [[ -n ${LOCAL_INSTALL} ]]; then |
| 612 | installArtifact "${ARTIFACT_FILE}" |
| 613 | retval=$(( retval + ${?} )) |
| 614 | fi |
| 615 | |
| 616 | # install dependencies in local and/or file repositories |
| 617 | |
| 618 | if [[ -n ${INSTALL_DEPS} ]]; then |
| 619 | if [[ -n ${FILE_REPO_INSTALL} ]]; then |
| 620 | DEPENDENCY_REPO_URL="${FILE_REPO_URL}" |
| 621 | installDependencies "${ARTIFACT_FILE}" |
| 622 | retval=$(( retval + ${?} )) |
| 623 | fi |
| 624 | |
| 625 | if [[ -n ${LOCAL_INSTALL} ]]; then |
| 626 | DEPENDENCY_REPO_URL="file:${HOME}/.m2/repository" |
| 627 | installDependencies "${ARTIFACT_FILE}" |
| 628 | retval=$(( retval + ${?} )) |
| 629 | fi |
| 630 | fi |
| 631 | |
| 632 | cleanup |
| 633 | |
Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 634 | exit ${retval} |