aboutsummaryrefslogtreecommitdiffstats
path: root/policy-management/src/main/server-gen/bin/deploy-artifact
blob: 81f5f14c10eede4503739af8fa12de0a342a298f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env bash

#
# ============LICENSE_START=======================================================
# ONAP
# ================================================================================
# Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
# ================================================================================
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============LICENSE_END=========================================================
###

##############################################################################
# Usage: usage
##############################################################################

function usage() {
    echo
    echo -e "syntax: $(basename "$0") "
    echo -e "\t [-f]"
    echo -e "\t -a <artifact> "
    echo
    echo -e "Options:"
    echo -e "\t -f|--file-repo: deployment in the file repository"
    echo -e "\t -a|--artifact: file artifact (jar or pom) to deploy"
    echo
    echo
}

##############################################################################
# Usage: getPomAttributes <pom-file> <attribute> ...
#
# This function performs simplistic parsing of a 'pom.xml' file, extracting
# the specified attributes (e.g. 'groupId', 'artifactId', 'version'). The
# attributes are returned as environment variables with the associated name
##############################################################################

function getPomAttributes
{
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} $* --"
        set -x
    fi

    local file="$1"
    if [[ ! -f "${file}" ]]; then
        echo "{1}: file does not exist"
        return 1
    fi

    local tab=$'\t' rval=0 attr value
    shift

    for attr in "$@" ; do
        # Try to fetch the parameter associated with the 'pom.xml' file.
        # Initially, the 'parent' element is excluded. If the desired
        # parameter is not found, the 'parent' element is included in the
        # second attempt.
        value=$(sed -n \
            -e '/<parent>/,/<\/parent>/d' \
            -e '/<dependencies>/,/<\/dependencies>/d' \
            -e '/<build>/,/<\/build>/d' \
            -e '/<profiles>/,/<\/profiles>/d' \
            -e '/<description>/,/<\/description>/d' \
            -e '/<packaging>/,/<\/packaging>/d' \
            -e '/<modelVersion>/,/<\/modelVersion>/d' \
            -e '/<properties>/,/<\/properties>/d' \
            -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
            <"${file}")

        if [[ "${value}" == "" ]]; then
            # need to check parent for parameter
            value=$(sed -n \
                -e '/<dependencies>/,/<\/dependencies>/d' \
                -e '/<build>/,/<\/build>/d' \
                -e '/<profiles>/,/<\/profiles>/d' \
                -e '/<description>/,/<\/description>/d' \
                -e '/<packaging>/,/<\/packaging>/d' \
                -e '/<modelVersion>/,/<\/modelVersion>/d' \
                -e '/<properties>/,/<\/properties>/d' \
                -e "/^[ ${tab}]*<${attr}>\([^<]*\)<\/${attr}>.*/{s//\1/p;}" \
                <"${file}")

            if [[ "${value}" == "" ]] ; then
                echo "${file}: Can't determine ${attr}" >&2
                rval=1
            fi
        fi

        # the following sets an environment variable with the name referred
        # to by ${attr}
        read "${attr}" <<<"${value}"
    done
    return ${rval}
}



##############################################################################
# Usage: deployJar <jar-file>
#
# This function deploys a JAR file in a repository, as well as
# the 'pom.xml' member it contains.
#################################################################

function deployJar
{
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} $* --"
        set -x
    fi

    local artifact="${1}"
    if [[ ! -f "${artifact}" ]]; then
        echo "{artifact}: does not exist"
        return 1
    fi

    local dir=$(mktemp -d)
    local jar="${artifact##*/}"

    cp -p "${artifact}" "${dir}/${jar}"

    (
        local rval=0
        cd "${dir}"

        # determine name of 'pom' file within JAR
        local pom=$(jar tf "${jar}" META-INF | grep '/pom\.xml$' | head -1)
        if [[ -z ${pom} ]] ; then
            echo "${jar}: Can't find 'pom.xml'" >&2
            return 1
        fi
        jar xf "${jar}" "${pom}"

        local pomProperties=$(jar tf "${jar}" META-INF | grep '/pom\.properties$' | head -1)
        if [[ -n ${pomProperties} ]] ; then
            # extract pom file
            jar xf "${jar}" "${pomProperties}"
            source "${pomProperties}"
        fi

        if [[ -z ${version} ]]; then
            if ! getPomAttributes "${pom}" version ; then
                echo "${pom}: Can't extract 'version' from pom" >&2
                return 2
            fi
        fi

        local repoId repoUrl
        if [[ "${version}" =~ SNAPSHOT ]] ; then
            repoId=${SNAPSHOT_REPOSITORY_ID}
            repoUrl=${SNAPSHOT_REPOSITORY_URL}
        else
            repoId=${RELEASE_REPOSITORY_ID}
            repoUrl=${RELEASE_REPOSITORY_URL}
        fi

        echo "${artifact}: Deploying JAR artifact to repository ${repoUrl} (${repoId})"
        mvn deploy:deploy-file \
            -Dfile="${jar}" \
            -Dversion="${version}" \
            -Dpackaging=jar -DgeneratePom=false -DpomFile="${pom}" \
            -DrepositoryId="${repoId}" -Durl="${repoUrl}" \
            -DupdateReleaseInfo=true

        retval=${?}
        rm -rf "${dir}"

        return ${retval}
    )
}

##############################################################################
# Usage: deployPom <pom-file>
#
# This function deploys a 'pom.xml' file in the local repository
##############################################################################

function deployPom
{
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} $* --"
        set -x
    fi

    local file="${1}"

    if [[ -f ${file} ]]; then
        return 1
    fi

    # need to extract attributes from POM file
    if getPomAttributes "${1}" artifactId groupId version ; then
        local repoId repoUrl
        if [[ "${version}" =~ SNAPSHOT ]] ; then
            repoId=${SNAPSHOT_REPOSITORY_ID}
            repoUrl=${SNAPSHOT_REPOSITORY_URL}
        else
            repoId=${RELEASE_REPOSITORY_ID}
            repoUrl=${RELEASE_REPOSITORY_URL}
        fi

        echo "${file}: Deploying POM artifact to remote repository"
        mvn deploy:deploy-file -Dfile="${file}" \
            -Dpackaging=pom -DgeneratePom=false \
            -DgroupId="${groupId}" \
            -DartifactId="${artifactId}" \
            -Dversion="${version}" \
            -DrepositoryId="${repoId}" -Durl="${repoUrl}" \
            -DupdateReleaseInfo=true
    else
        echo "${file}: Can't install pom due to missing attributes" >&2
        return 1
    fi
}

##############################################################################
# Usage: deployArtifact
#
# This function deploys a maven artifacts in a repository
##############################################################################

function deployArtifact
{
    if [[ ${DEBUG} == y ]]; then
        echo "-- ${FUNCNAME[0]} $* --"
        set -x
    fi

    local file="${1}"
    if [[ -z "${file}" ]]; then
        echo "${file}: artifact file not provided"
        return 1
    fi

    if [[ ! -f "${file}" ]]; then
        echo "${file}: artifact file does not exist"
        return 1
    fi

    case "${file}" in
        *pom.xml|*.pom)
            deployPom "${file}"
            ;;
        *.jar)
            deployJar "${file}"
            ;;
        *)  echo "${file}: Don't know how to install artifact" >&2
            return 2
            ;;
    esac

    return ${?}
}

##############################################################################
# MAIN
##############################################################################

if [[ ${DEBUG} == y ]]; then
    echo "-- $0 $* --"
    set -x
fi

retval=0

until [[ -z "$1" ]]; do
    case $1 in
        -a|--artifact)  shift
                        ARTIFACT=$1
                        ;;
        -f|--file-repo) FILE_REPO_ID="file-repository"
                        FILE_REPO_URL="file:${HOME}/.m2/file-repository"
                        ;;
        *)              usage
                        exit 1
                        ;;
    esac
    shift
done

if [[ -z ${ARTIFACT} ]]; then
    echo "No artifact file provided: $*"
    usage
    exit 1
fi

if [[ -n ${SNAPSHOT_REPOSITORY_URL} ]] && [[ -n ${RELEASE_REPOSITORY_URL} ]]; then
    deployArtifact "${ARTIFACT}"
    retval=${?}
else
    FILE_REPO_ID="file-repository"
    FILE_REPO_URL="file:${HOME}/.m2/file-repository"
fi

if [[ -n ${FILE_REPO_ID} ]]; then
    SNAPSHOT_REPOSITORY_ID="${FILE_REPO_ID}"
    SNAPSHOT_REPOSITORY_URL="${FILE_REPO_URL}"
    RELEASE_REPOSITORY_ID="${FILE_REPO_ID}"
    RELEASE_REPOSITORY_URL="${FILE_REPO_URL}"

    mkdir -p "${FILE_REPO_URL#file:}" 2> /dev/null
    deployArtifact "${ARTIFACT}"
    retval=${?}
fi

exit ${retval}