blob: 2fc101bdc23b7894a3ba2624499708e5e15f25f4 [file] [log] [blame]
Jorge Hernandez777131d2019-01-04 14:43:44 -06001#!/usr/bin/env bash
2
3#
4# ============LICENSE_START=======================================================
5# ONAP
6# ================================================================================
jhh4112fec2019-12-09 18:11:13 -06007# Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
Jorge Hernandez777131d2019-01-04 14:43:44 -06008# ================================================================================
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=========================================================
21###
22
23##############################################################################
24# Usage: usage
25##############################################################################
26
27function usage() {
28 echo
29 echo -e "syntax: $(basename "$0") "
jhh4112fec2019-12-09 18:11:13 -060030 echo -e "\t [-f|-l|-d]"
31 echo -e "\t -s <custom-settings> "
Jorge Hernandez777131d2019-01-04 14:43:44 -060032 echo -e "\t -a <artifact> "
33 echo
34 echo -e "Options:"
jhh4112fec2019-12-09 18:11:13 -060035 echo -e "\t -f|--file-repo: deploy in the file repository"
36 echo -e "\t -l|--local-repo: install in the local repository"
37 echo -e "\t -d|--dependencies: install dependencies in the local repository"
38 echo -e "\t -s|--settings: custom settings.xml"
39 echo -e "\t -a|--artifact: file artifact (jar or pom) to deploy and/or install"
Jorge Hernandez777131d2019-01-04 14:43:44 -060040 echo
41 echo
42}
43
44##############################################################################
jhh4112fec2019-12-09 18:11:13 -060045# Usage: init <artifact>
46#
47# If the artifact is a jar, this function extracts the maven metadata for
48# consumption in this script.
49#
50# As a result the global variables will be set:
51# WORKING_DIR: working directory with extracted files.
52# WORKING_POM: pom file location
53# WORKING_POM_PROPERTIES: pom properties file location
54##############################################################################
55
56function init
57{
58 if [[ ${DEBUG} == y ]]; then
59 echo "-- ${FUNCNAME[0]} $* --"
60 set -x
61 fi
62
63 local artifact="${1}"
64 if [[ ! -f ${artifact} ]]; then
65 echo "${artifact}: artifact does not exist"
66 return 1
67 fi
68
69 if [[ ${artifact} != *.jar ]]; then
70 return 0
71 fi
72
73 local dir=$(mktemp -d)
74 local jar="${artifact##*/}"
75
76 WORKING_DIR=$(realpath "${dir}")
77
78 cp -p "${artifact}" "${WORKING_DIR}/${jar}"
79 pushd "${dir}"
80
81 local rc=0
82
83 # determine name of 'pom' file within JAR
84 local pom=$(jar tf "${jar}" META-INF | grep '/pom\.xml$' | head -1)
85 if [[ -n ${pom} ]] ; then
86 jar xf "${jar}" "${pom}"
87 WORKING_POM=$(realpath "${pom}")
88 else
89 echo "${artifact}: pom not found"
90 fi
91
92 local pomProperties=$(jar tf "${jar}" META-INF | grep '/pom\.properties$' | head -1)
93 if [[ -n ${pomProperties} ]]; then
94 jar xf "${jar}" "${pomProperties}"
95 WORKING_POM_PROPERTIES=$(realpath ${pomProperties})
96 source "${WORKING_POM_PROPERTIES}"
97 echo "${artifact}: sourcing in ${WORKING_POM_PROPERTIES}"
98 else
99 echo "${artifact}: pom.properties not found"
100 if [[ -n ${WORKING_POM} ]]; then
101 if ! getPomAttributes "${WORKING_POM}" artifactId groupId version ; then
102 echo "${WORKING_POM}: cannot extract maven coordinates"
103 rc=1
104 fi
105 else
106 echo "${artifact}: cannot extract maven coordinates"
107 rc=1
108 fi
109 fi
110
111 if [[ -z ${version} ]] || [[ -z ${groupId} ]] || [[ -z ${artifactId} ]]; then
112 echo "${artifact}: some coordinates cannot be extracted"
113 rc=1
114 fi
115
116 echo "${artifact}: coordinates ${groupId}:${artifactId}:${version}"
117 popd
118
119 return ${rc}
120}
121
122##############################################################################
123# Usage: cleanup
124#
125# Clean up temporary resources.
126##############################################################################
127
128function cleanup
129{
130 if [[ ${DEBUG} == y ]]; then
131 echo "-- ${FUNCNAME[0]} $* --"
132 set -x
133 fi
134
135 if [[ -n ${WORKING_DIR} ]]; then
136 rm -rf "${WORKING_DIR}"
137 fi
138}
139
140##############################################################################
Jorge Hernandez777131d2019-01-04 14:43:44 -0600141# Usage: getPomAttributes <pom-file> <attribute> ...
142#
143# This function performs simplistic parsing of a 'pom.xml' file, extracting
144# the specified attributes (e.g. 'groupId', 'artifactId', 'version'). The
145# attributes are returned as environment variables with the associated name
146##############################################################################
147
148function getPomAttributes
149{
150 if [[ ${DEBUG} == y ]]; then
151 echo "-- ${FUNCNAME[0]} $* --"
152 set -x
153 fi
154
155 local file="$1"
156 if [[ ! -f "${file}" ]]; then
jhh4112fec2019-12-09 18:11:13 -0600157 echo "${file}: file does not exist"
Jorge Hernandez777131d2019-01-04 14:43:44 -0600158 return 1
159 fi
160
161 local tab=$'\t' rval=0 attr value
162 shift
163
164 for attr in "$@" ; do
165 # Try to fetch the parameter associated with the 'pom.xml' file.
166 # Initially, the 'parent' element is excluded. If the desired
167 # parameter is not found, the 'parent' element is included in the
168 # second attempt.
169 value=$(sed -n \
170 -e '/<parent>/,/<\/parent>/d' \
171 -e '/<dependencies>/,/<\/dependencies>/d' \
172 -e '/<build>/,/<\/build>/d' \
173 -e '/<profiles>/,/<\/profiles>/d' \
174 -e '/<description>/,/<\/description>/d' \
175 -e '/<packaging>/,/<\/packaging>/d' \
176 -e '/<modelVersion>/,/<\/modelVersion>/d' \
177 -e '/<properties>/,/<\/properties>/d' \
178 -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
179 <"${file}")
180
181 if [[ "${value}" == "" ]]; then
182 # need to check parent for parameter
183 value=$(sed -n \
184 -e '/<dependencies>/,/<\/dependencies>/d' \
185 -e '/<build>/,/<\/build>/d' \
186 -e '/<profiles>/,/<\/profiles>/d' \
187 -e '/<description>/,/<\/description>/d' \
188 -e '/<packaging>/,/<\/packaging>/d' \
189 -e '/<modelVersion>/,/<\/modelVersion>/d' \
190 -e '/<properties>/,/<\/properties>/d' \
191 -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
192 <"${file}")
193
194 if [[ "${value}" == "" ]] ; then
jhh4112fec2019-12-09 18:11:13 -0600195 echo "${file}: Can't determine ${attr}"
Jorge Hernandez777131d2019-01-04 14:43:44 -0600196 rval=1
197 fi
198 fi
199
200 # the following sets an environment variable with the name referred
201 # to by ${attr}
202 read "${attr}" <<<"${value}"
203 done
204 return ${rval}
205}
206
Jorge Hernandez777131d2019-01-04 14:43:44 -0600207##############################################################################
208# Usage: deployJar <jar-file>
209#
210# This function deploys a JAR file in a repository, as well as
211# the 'pom.xml' member it contains.
212#################################################################
213
214function deployJar
215{
216 if [[ ${DEBUG} == y ]]; then
217 echo "-- ${FUNCNAME[0]} $* --"
218 set -x
219 fi
220
jhh4112fec2019-12-09 18:11:13 -0600221 local file="${1}"
222
223 if [[ ! -f ${file} ]]; then
Jorge Hernandez777131d2019-01-04 14:43:44 -0600224 return 1
225 fi
226
jhh4112fec2019-12-09 18:11:13 -0600227 local repoId repoUrl
228 if [[ "${version}" =~ SNAPSHOT ]] ; then
229 repoId=${SNAPSHOT_REPOSITORY_ID}
230 repoUrl=${SNAPSHOT_REPOSITORY_URL}
231 else
232 repoId=${RELEASE_REPOSITORY_ID}
233 repoUrl=${RELEASE_REPOSITORY_URL}
234 fi
Jorge Hernandez777131d2019-01-04 14:43:44 -0600235
jhh4112fec2019-12-09 18:11:13 -0600236 if [[ -z ${repoUrl} ]] || [[ -z ${repoId} ]]; then
237 echo "{file}: no repository id/url to deploy jar"
238 return 1
239 fi
Jorge Hernandez777131d2019-01-04 14:43:44 -0600240
jhh4112fec2019-12-09 18:11:13 -0600241 echo "${file}: deploying jar artifact to repository ${repoId}: ${repoUrl}"
242 echo "${file}: coordinates ${groupId} ${artifactId} ${version}"
Jorge Hernandez777131d2019-01-04 14:43:44 -0600243
jhh4112fec2019-12-09 18:11:13 -0600244 mvn ${CUSTOM_SETTINGS} deploy:deploy-file \
245 -Dfile="${file}" \
246 -Dversion="${version}" \
247 -Dpackaging=jar \
248 -DgeneratePom=false \
249 -DpomFile="${WORKING_POM}" \
250 -DrepositoryId="${repoId}" \
251 -Durl="${repoUrl}" \
252 -DupdateReleaseInfo=true
Jorge Hernandez777131d2019-01-04 14:43:44 -0600253
jhh4112fec2019-12-09 18:11:13 -0600254 return ${?}
Jorge Hernandez777131d2019-01-04 14:43:44 -0600255}
256
257##############################################################################
258# Usage: deployPom <pom-file>
259#
260# This function deploys a 'pom.xml' file in the local repository
261##############################################################################
262
263function deployPom
264{
265 if [[ ${DEBUG} == y ]]; then
266 echo "-- ${FUNCNAME[0]} $* --"
267 set -x
268 fi
269
270 local file="${1}"
271
jhh4112fec2019-12-09 18:11:13 -0600272 if [[ ! -f ${file} ]]; then
Jorge Hernandez777131d2019-01-04 14:43:44 -0600273 return 1
274 fi
275
jhh4112fec2019-12-09 18:11:13 -0600276 if ! getPomAttributes "${file}" artifactId groupId version ; then
277 echo "${file}: cannot deploy pom due to missing attributes"
278 return 1
279 fi
Jorge Hernandez777131d2019-01-04 14:43:44 -0600280
jhh4112fec2019-12-09 18:11:13 -0600281 local repoId repoUrl
282 if [[ "${version}" =~ SNAPSHOT ]] ; then
283 repoId=${SNAPSHOT_REPOSITORY_ID}
284 repoUrl=${SNAPSHOT_REPOSITORY_URL}
Jorge Hernandez777131d2019-01-04 14:43:44 -0600285 else
jhh4112fec2019-12-09 18:11:13 -0600286 repoId=${RELEASE_REPOSITORY_ID}
287 repoUrl=${RELEASE_REPOSITORY_URL}
Jorge Hernandez777131d2019-01-04 14:43:44 -0600288 fi
jhh4112fec2019-12-09 18:11:13 -0600289
290 echo "${file}: deploying pom artifact to repository ${repoId}: ${repoUrl}"
291 echo "${file}: coordinates ${groupId} ${artifactId} ${version}"
292
293 mvn ${CUSTOM_SETTINGS} deploy:deploy-file \
294 -Dfile="${file}" \
295 -Dpackaging=pom \
296 -DgeneratePom=false \
297 -DgroupId="${groupId}" \
298 -DartifactId="${artifactId}" \
299 -Dversion="${version}" \
300 -DrepositoryId="${repoId}" \
301 -Durl="${repoUrl}" \
302 -DupdateReleaseInfo=true
303
304 return ${?}
Jorge Hernandez777131d2019-01-04 14:43:44 -0600305}
306
307##############################################################################
308# Usage: deployArtifact
309#
jhh4112fec2019-12-09 18:11:13 -0600310# This function deploys a maven artifact in a repository
Jorge Hernandez777131d2019-01-04 14:43:44 -0600311##############################################################################
312
313function deployArtifact
314{
315 if [[ ${DEBUG} == y ]]; then
316 echo "-- ${FUNCNAME[0]} $* --"
317 set -x
318 fi
319
320 local file="${1}"
321 if [[ -z "${file}" ]]; then
322 echo "${file}: artifact file not provided"
323 return 1
324 fi
325
326 if [[ ! -f "${file}" ]]; then
327 echo "${file}: artifact file does not exist"
328 return 1
329 fi
330
331 case "${file}" in
332 *pom.xml|*.pom)
333 deployPom "${file}"
jhh4112fec2019-12-09 18:11:13 -0600334 return ${?}
Jorge Hernandez777131d2019-01-04 14:43:44 -0600335 ;;
336 *.jar)
337 deployJar "${file}"
jhh4112fec2019-12-09 18:11:13 -0600338 return ${?}
Jorge Hernandez777131d2019-01-04 14:43:44 -0600339 ;;
jhh4112fec2019-12-09 18:11:13 -0600340 *) echo "${file}: Don't know how to deploy artifact"
341 return 1
Jorge Hernandez777131d2019-01-04 14:43:44 -0600342 ;;
343 esac
jhh4112fec2019-12-09 18:11:13 -0600344}
345
346##############################################################################
347# Usage: installJar <artifact-file>
348#
349# This function installs a jar packaged artifact in the local repository
350##############################################################################
351
352function installJar
353{
354 if [[ ${DEBUG} == y ]]; then
355 echo "-- ${FUNCNAME[0]} $* --"
356 set -x
357 fi
358
359 local file="${1}"
360
361 if [[ ! -f ${file} ]]; then
362 return 1
363 fi
364
365 mvn ${CUSTOM_SETTINGS} org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file \
366 -Dfile="${file}"
367
368 return $?
369}
370
371##############################################################################
372# Usage: installPom <pom-file>
373#
374# This function installs a pom file in the local repository
375##############################################################################
376
377function installPom
378{
379 if [[ ${DEBUG} == y ]]; then
380 echo "-- ${FUNCNAME[0]} $* --"
381 set -x
382 fi
383
384 local file="${1}"
385
386 if [[ ! -f ${file} ]]; then
387 return 1
388 fi
389
390 mvn ${CUSTOM_SETTINGS} org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file \
391 -Dpackaging=pom \
392 -Dfile="${file}" \
393 -DpomFile="${file}"
394
395 return $?
396}
397
398##############################################################################
399# Usage: installArtifact
400#
401# This function installs a maven artifacts in the local repository
402##############################################################################
403
404function installArtifact
405{
406 if [[ ${DEBUG} == y ]]; then
407 echo "-- ${FUNCNAME[0]} $* --"
408 set -x
409 fi
410
411 local file="${1}"
412 if [[ -z "${file}" ]]; then
413 echo "${file}: artifact file not provided"
414 return 1
415 fi
416
417 if [[ ! -f "${file}" ]]; then
418 echo "${file}: artifact file does not exist"
419 return 1
420 fi
421
422 case "${file}" in
423 *pom.xml|*.pom)
424 installPom "${file}"
425 return ${?}
426 ;;
427 *.jar)
428 installJar "${file}"
429 return ${?}
430 ;;
431 *) echo "${file}: Don't know how to install the artifact"
432 return 1
433 ;;
434 esac
435}
436
437##############################################################################
438# Usage: installDependencies <pom-file>
439#
440# This function installs the dependencies of an artifact in the file or
441# local repository
442##############################################################################
443
444function installDependencies
445{
446 if [[ ${DEBUG} == y ]]; then
447 echo "-- ${FUNCNAME[0]} $* --"
448 set -x
449 fi
450
451 local file="${1}"
452
453 if [[ ! -f ${file} ]]; then
454 return 1
455 fi
456
457 if [[ -z ${DEPENDENCY_REPO_URL} ]]; then
458 echo "${file}: no repo url to install dependencies"
459 return 1
460 fi
461
462 echo "${file}: deploying dependencies from repository ${DEPENDENCY_REPO_URL}"
463 echo "${file}: coordinates ${groupId} ${artifactId} ${version}"
464
465 mvn ${CUSTOM_SETTINGS} org.apache.maven.plugins:maven-dependency-plugin:3.1.1:get \
466 -DartifactId="${artifactId}" \
467 -DgroupId="${groupId}" \
468 -Dversion="${version}" \
469 -DremoteRepositories="${DEPENDENCY_REPO_URL}"
Jorge Hernandez777131d2019-01-04 14:43:44 -0600470
471 return ${?}
472}
473
474##############################################################################
475# MAIN
476##############################################################################
477
478if [[ ${DEBUG} == y ]]; then
479 echo "-- $0 $* --"
480 set -x
481fi
482
jhh4112fec2019-12-09 18:11:13 -0600483# reset globals
484
485unset ARTIFACT_FILE
486unset LOCAL_INSTALL
487unset INSTALL_DEPS
488unset FILE_REPO_INSTALL
489unset WORKING_DIR
490unset WORKING_POM
491unset WORKING_POM_PROPERTIES
492unset DEPENDENCY_REPO_URL
493unset SETTINGS_FILE
494unset CUSTOM_SETTINGS
495
496# process input
Jorge Hernandez777131d2019-01-04 14:43:44 -0600497
498until [[ -z "$1" ]]; do
499 case $1 in
jhh4112fec2019-12-09 18:11:13 -0600500 -a|--artifact) shift
501 ARTIFACT_FILE=$1
502 ;;
503 -s|--settings) shift
504 SETTINGS_FILE=$1
505 ;;
506 -l|--local-repo) LOCAL_INSTALL="true"
507 ;;
508 -d|--dependencies) INSTALL_DEPS="true"
509 ;;
510 -f|--file-repo) FILE_REPO_INSTALL="true"
511 ;;
512 *) usage
513 exit 1
514 ;;
Jorge Hernandez777131d2019-01-04 14:43:44 -0600515 esac
516 shift
517done
518
jhh4112fec2019-12-09 18:11:13 -0600519if [[ -z ${ARTIFACT_FILE} ]]; then
Jorge Hernandez777131d2019-01-04 14:43:44 -0600520 echo "No artifact file provided: $*"
521 usage
522 exit 1
523fi
524
jhh4112fec2019-12-09 18:11:13 -0600525if [[ -n ${SETTINGS_FILE} ]]; then
526 CUSTOM_SETTINGS="--settings=${SETTINGS_FILE}"
Jorge Hernandez777131d2019-01-04 14:43:44 -0600527fi
528
jhh4112fec2019-12-09 18:11:13 -0600529# retval has the count of failed operations
530
531retval=0
532
533# initialize
534
535init "${ARTIFACT_FILE}"
536retval=$?
537if [[ ${retval} != 0 ]]; then
538 cleanup
539 exit ${retval}
540fi
541
542# remote repo deploy operation
543#
544# SNAPSHOT_REPOSITORY_URL and RELEASE_REPOSITORY_URL
545# are pre-existing environmental variables (base.conf)
546
547if [[ -n ${SNAPSHOT_REPOSITORY_URL} ]] || [[ -n ${RELEASE_REPOSITORY_URL} ]]; then
548 deployArtifact "${ARTIFACT_FILE}"
549 retval=$(( retval + ${?} ))
550fi
551
552# deploy in file repository
553
554if [[ -n ${FILE_REPO_INSTALL} ]]; then
555 FILE_REPO_ID="file-repository"
556 FILE_REPO_URL="file:${HOME}/.m2/file-repository"
557
Jorge Hernandez777131d2019-01-04 14:43:44 -0600558 SNAPSHOT_REPOSITORY_ID="${FILE_REPO_ID}"
559 SNAPSHOT_REPOSITORY_URL="${FILE_REPO_URL}"
560 RELEASE_REPOSITORY_ID="${FILE_REPO_ID}"
561 RELEASE_REPOSITORY_URL="${FILE_REPO_URL}"
562
563 mkdir -p "${FILE_REPO_URL#file:}" 2> /dev/null
jhh4112fec2019-12-09 18:11:13 -0600564 deployArtifact "${ARTIFACT_FILE}"
565 retval=$(( retval + ${?} ))
Jorge Hernandez777131d2019-01-04 14:43:44 -0600566fi
567
jhh4112fec2019-12-09 18:11:13 -0600568# install in local repository
569
570if [[ -n ${LOCAL_INSTALL} ]]; then
571 installArtifact "${ARTIFACT_FILE}"
572 retval=$(( retval + ${?} ))
573fi
574
575# install dependencies in local and/or file repositories
576
577if [[ -n ${INSTALL_DEPS} ]]; then
578 if [[ -n ${FILE_REPO_INSTALL} ]]; then
579 DEPENDENCY_REPO_URL="${FILE_REPO_URL}"
580 installDependencies "${ARTIFACT_FILE}"
581 retval=$(( retval + ${?} ))
582 fi
583
584 if [[ -n ${LOCAL_INSTALL} ]]; then
585 DEPENDENCY_REPO_URL="file:${HOME}/.m2/repository"
586 installDependencies "${ARTIFACT_FILE}"
587 retval=$(( retval + ${?} ))
588 fi
589fi
590
591cleanup
592
Jorge Hernandez777131d2019-01-04 14:43:44 -0600593exit ${retval}