Jorge Hernandez | 777131d | 2019-01-04 14:43:44 -0600 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # |
| 4 | # ============LICENSE_START======================================================= |
| 5 | # ONAP |
| 6 | # ================================================================================ |
| 7 | # Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. |
| 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========================================================= |
| 21 | ### |
| 22 | |
| 23 | ############################################################################## |
| 24 | # Usage: usage |
| 25 | ############################################################################## |
| 26 | |
| 27 | function usage() { |
| 28 | echo |
| 29 | echo -e "syntax: $(basename "$0") " |
| 30 | echo -e "\t [-f]" |
| 31 | echo -e "\t -a <artifact> " |
| 32 | echo |
| 33 | echo -e "Options:" |
| 34 | echo -e "\t -f|--file-repo: deployment in the file repository" |
| 35 | echo -e "\t -a|--artifact: file artifact (jar or pom) to deploy" |
| 36 | echo |
| 37 | echo |
| 38 | } |
| 39 | |
| 40 | ############################################################################## |
| 41 | # Usage: getPomAttributes <pom-file> <attribute> ... |
| 42 | # |
| 43 | # This function performs simplistic parsing of a 'pom.xml' file, extracting |
| 44 | # the specified attributes (e.g. 'groupId', 'artifactId', 'version'). The |
| 45 | # attributes are returned as environment variables with the associated name |
| 46 | ############################################################################## |
| 47 | |
| 48 | function getPomAttributes |
| 49 | { |
| 50 | if [[ ${DEBUG} == y ]]; then |
| 51 | echo "-- ${FUNCNAME[0]} $* --" |
| 52 | set -x |
| 53 | fi |
| 54 | |
| 55 | local file="$1" |
| 56 | if [[ ! -f "${file}" ]]; then |
| 57 | echo "{1}: file does not exist" |
| 58 | return 1 |
| 59 | fi |
| 60 | |
| 61 | local tab=$'\t' rval=0 attr value |
| 62 | shift |
| 63 | |
| 64 | for attr in "$@" ; do |
| 65 | # Try to fetch the parameter associated with the 'pom.xml' file. |
| 66 | # Initially, the 'parent' element is excluded. If the desired |
| 67 | # parameter is not found, the 'parent' element is included in the |
| 68 | # second attempt. |
| 69 | value=$(sed -n \ |
| 70 | -e '/<parent>/,/<\/parent>/d' \ |
| 71 | -e '/<dependencies>/,/<\/dependencies>/d' \ |
| 72 | -e '/<build>/,/<\/build>/d' \ |
| 73 | -e '/<profiles>/,/<\/profiles>/d' \ |
| 74 | -e '/<description>/,/<\/description>/d' \ |
| 75 | -e '/<packaging>/,/<\/packaging>/d' \ |
| 76 | -e '/<modelVersion>/,/<\/modelVersion>/d' \ |
| 77 | -e '/<properties>/,/<\/properties>/d' \ |
| 78 | -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \ |
| 79 | <"${file}") |
| 80 | |
| 81 | if [[ "${value}" == "" ]]; then |
| 82 | # need to check parent for parameter |
| 83 | value=$(sed -n \ |
| 84 | -e '/<dependencies>/,/<\/dependencies>/d' \ |
| 85 | -e '/<build>/,/<\/build>/d' \ |
| 86 | -e '/<profiles>/,/<\/profiles>/d' \ |
| 87 | -e '/<description>/,/<\/description>/d' \ |
| 88 | -e '/<packaging>/,/<\/packaging>/d' \ |
| 89 | -e '/<modelVersion>/,/<\/modelVersion>/d' \ |
| 90 | -e '/<properties>/,/<\/properties>/d' \ |
| 91 | -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \ |
| 92 | <"${file}") |
| 93 | |
| 94 | if [[ "${value}" == "" ]] ; then |
| 95 | echo "${file}: Can't determine ${attr}" >&2 |
| 96 | rval=1 |
| 97 | fi |
| 98 | fi |
| 99 | |
| 100 | # the following sets an environment variable with the name referred |
| 101 | # to by ${attr} |
| 102 | read "${attr}" <<<"${value}" |
| 103 | done |
| 104 | return ${rval} |
| 105 | } |
| 106 | |
| 107 | |
| 108 | |
| 109 | ############################################################################## |
| 110 | # Usage: deployJar <jar-file> |
| 111 | # |
| 112 | # This function deploys a JAR file in a repository, as well as |
| 113 | # the 'pom.xml' member it contains. |
| 114 | ################################################################# |
| 115 | |
| 116 | function deployJar |
| 117 | { |
| 118 | if [[ ${DEBUG} == y ]]; then |
| 119 | echo "-- ${FUNCNAME[0]} $* --" |
| 120 | set -x |
| 121 | fi |
| 122 | |
| 123 | local artifact="${1}" |
| 124 | if [[ ! -f "${artifact}" ]]; then |
| 125 | echo "{artifact}: does not exist" |
| 126 | return 1 |
| 127 | fi |
| 128 | |
| 129 | local dir=$(mktemp -d) |
| 130 | local jar="${artifact##*/}" |
| 131 | |
| 132 | cp -p "${artifact}" "${dir}/${jar}" |
| 133 | |
| 134 | ( |
| 135 | local rval=0 |
| 136 | cd "${dir}" |
| 137 | |
| 138 | # determine name of 'pom' file within JAR |
| 139 | local pom=$(jar tf "${jar}" META-INF | grep '/pom\.xml$' | head -1) |
| 140 | if [[ -z ${pom} ]] ; then |
| 141 | echo "${jar}: Can't find 'pom.xml'" >&2 |
| 142 | return 1 |
| 143 | fi |
| 144 | jar xf "${jar}" "${pom}" |
| 145 | |
| 146 | local pomProperties=$(jar tf "${jar}" META-INF | grep '/pom\.properties$' | head -1) |
| 147 | if [[ -n ${pomProperties} ]] ; then |
| 148 | # extract pom file |
| 149 | jar xf "${jar}" "${pomProperties}" |
| 150 | source "${pomProperties}" |
| 151 | fi |
| 152 | |
| 153 | if [[ -z ${version} ]]; then |
| 154 | if ! getPomAttributes "${pom}" version ; then |
| 155 | echo "${pom}: Can't extract 'version' from pom" >&2 |
| 156 | return 2 |
| 157 | fi |
| 158 | fi |
| 159 | |
| 160 | local repoId repoUrl |
| 161 | if [[ "${version}" =~ SNAPSHOT ]] ; then |
| 162 | repoId=${SNAPSHOT_REPOSITORY_ID} |
| 163 | repoUrl=${SNAPSHOT_REPOSITORY_URL} |
| 164 | else |
| 165 | repoId=${RELEASE_REPOSITORY_ID} |
| 166 | repoUrl=${RELEASE_REPOSITORY_URL} |
| 167 | fi |
| 168 | |
| 169 | echo "${artifact}: Deploying JAR artifact to repository ${repoUrl} (${repoId})" |
| 170 | mvn deploy:deploy-file \ |
| 171 | -Dfile="${jar}" \ |
| 172 | -Dversion="${version}" \ |
| 173 | -Dpackaging=jar -DgeneratePom=false -DpomFile="${pom}" \ |
| 174 | -DrepositoryId="${repoId}" -Durl="${repoUrl}" \ |
| 175 | -DupdateReleaseInfo=true |
| 176 | |
| 177 | retval=${?} |
| 178 | rm -rf "${dir}" |
| 179 | |
| 180 | return ${retval} |
| 181 | ) |
| 182 | } |
| 183 | |
| 184 | ############################################################################## |
| 185 | # Usage: deployPom <pom-file> |
| 186 | # |
| 187 | # This function deploys a 'pom.xml' file in the local repository |
| 188 | ############################################################################## |
| 189 | |
| 190 | function deployPom |
| 191 | { |
| 192 | if [[ ${DEBUG} == y ]]; then |
| 193 | echo "-- ${FUNCNAME[0]} $* --" |
| 194 | set -x |
| 195 | fi |
| 196 | |
| 197 | local file="${1}" |
| 198 | |
| 199 | if [[ -f ${file} ]]; then |
| 200 | return 1 |
| 201 | fi |
| 202 | |
| 203 | # need to extract attributes from POM file |
| 204 | if getPomAttributes "${1}" artifactId groupId version ; then |
| 205 | local repoId repoUrl |
| 206 | if [[ "${version}" =~ SNAPSHOT ]] ; then |
| 207 | repoId=${SNAPSHOT_REPOSITORY_ID} |
| 208 | repoUrl=${SNAPSHOT_REPOSITORY_URL} |
| 209 | else |
| 210 | repoId=${RELEASE_REPOSITORY_ID} |
| 211 | repoUrl=${RELEASE_REPOSITORY_URL} |
| 212 | fi |
| 213 | |
| 214 | echo "${file}: Deploying POM artifact to remote repository" |
| 215 | mvn deploy:deploy-file -Dfile="${file}" \ |
| 216 | -Dpackaging=pom -DgeneratePom=false \ |
| 217 | -DgroupId="${groupId}" \ |
| 218 | -DartifactId="${artifactId}" \ |
| 219 | -Dversion="${version}" \ |
| 220 | -DrepositoryId="${repoId}" -Durl="${repoUrl}" \ |
| 221 | -DupdateReleaseInfo=true |
| 222 | else |
| 223 | echo "${file}: Can't install pom due to missing attributes" >&2 |
| 224 | return 1 |
| 225 | fi |
| 226 | } |
| 227 | |
| 228 | ############################################################################## |
| 229 | # Usage: deployArtifact |
| 230 | # |
| 231 | # This function deploys a maven artifacts in a repository |
| 232 | ############################################################################## |
| 233 | |
| 234 | function deployArtifact |
| 235 | { |
| 236 | if [[ ${DEBUG} == y ]]; then |
| 237 | echo "-- ${FUNCNAME[0]} $* --" |
| 238 | set -x |
| 239 | fi |
| 240 | |
| 241 | local file="${1}" |
| 242 | if [[ -z "${file}" ]]; then |
| 243 | echo "${file}: artifact file not provided" |
| 244 | return 1 |
| 245 | fi |
| 246 | |
| 247 | if [[ ! -f "${file}" ]]; then |
| 248 | echo "${file}: artifact file does not exist" |
| 249 | return 1 |
| 250 | fi |
| 251 | |
| 252 | case "${file}" in |
| 253 | *pom.xml|*.pom) |
| 254 | deployPom "${file}" |
| 255 | ;; |
| 256 | *.jar) |
| 257 | deployJar "${file}" |
| 258 | ;; |
| 259 | *) echo "${file}: Don't know how to install artifact" >&2 |
| 260 | return 2 |
| 261 | ;; |
| 262 | esac |
| 263 | |
| 264 | return ${?} |
| 265 | } |
| 266 | |
| 267 | ############################################################################## |
| 268 | # MAIN |
| 269 | ############################################################################## |
| 270 | |
| 271 | if [[ ${DEBUG} == y ]]; then |
| 272 | echo "-- $0 $* --" |
| 273 | set -x |
| 274 | fi |
| 275 | |
| 276 | retval=0 |
| 277 | |
| 278 | until [[ -z "$1" ]]; do |
| 279 | case $1 in |
| 280 | -a|--artifact) shift |
| 281 | ARTIFACT=$1 |
| 282 | ;; |
| 283 | -f|--file-repo) FILE_REPO_ID="file-repository" |
| 284 | FILE_REPO_URL="file:${HOME}/.m2/file-repository" |
| 285 | ;; |
| 286 | *) usage |
| 287 | exit 1 |
| 288 | ;; |
| 289 | esac |
| 290 | shift |
| 291 | done |
| 292 | |
| 293 | if [[ -z ${ARTIFACT} ]]; then |
| 294 | echo "No artifact file provided: $*" |
| 295 | usage |
| 296 | exit 1 |
| 297 | fi |
| 298 | |
| 299 | if [[ -n ${SNAPSHOT_REPOSITORY_URL} ]] && [[ -n ${RELEASE_REPOSITORY_URL} ]]; then |
| 300 | deployArtifact "${ARTIFACT}" |
| 301 | retval=${?} |
| 302 | else |
| 303 | FILE_REPO_ID="file-repository" |
| 304 | FILE_REPO_URL="file:${HOME}/.m2/file-repository" |
| 305 | fi |
| 306 | |
| 307 | if [[ -n ${FILE_REPO_ID} ]]; then |
| 308 | SNAPSHOT_REPOSITORY_ID="${FILE_REPO_ID}" |
| 309 | SNAPSHOT_REPOSITORY_URL="${FILE_REPO_URL}" |
| 310 | RELEASE_REPOSITORY_ID="${FILE_REPO_ID}" |
| 311 | RELEASE_REPOSITORY_URL="${FILE_REPO_URL}" |
| 312 | |
| 313 | mkdir -p "${FILE_REPO_URL#file:}" 2> /dev/null |
| 314 | deployArtifact "${ARTIFACT}" |
| 315 | retval=${?} |
| 316 | fi |
| 317 | |
| 318 | exit ${retval} |