forked from salesforce/pomgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaven.sh
executable file
·386 lines (302 loc) · 13.1 KB
/
maven.sh
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#!/bin/bash
# Copyright (c) 2018, salesforce.com, inc.
# All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
set -e
print_usage() {
cat << EOF
Usage: bazel run @pomgen//maven.sh -a action(s) -l path/to/library/root/dir
Required arguments:
-a (actions)
specifies which action(s) to run, see below for detaols on the actions.
Make sure you run 'pomgen' before trying other actions.
The actions to run may be comma-separated if there is more than one,
for example: -a pomgen,install
Optional arguments:
-l (library path)
library path - the relative path to the root directory of a library,
defaults to all libraries in the repository
For example: -l path/to/lib/dir (the dir that has MVN-INF/LIBRARY.root)
-i (ignore references)
by default, the library specified using -l is used as a starting point and
upstream libraries are processed as well. This behavior can be disabled
using -i
-d (debug)
enables debug logging
-f (force)
runs pomgen even if no changes to artifacts have been made
-h (help)
this message
Mandatory action:
pomgen: generates pom files for Maven artifacts. IMPORTANT: this action
is required to run at least once before any of the other actions.
Additional supported actions:
install: installs binary (jar) and pom artifacts into the local
Maven repository.
install_all: installs binary, sources, javadoc and pom artifacts into the
local Maven repository.
The sources jar is built by Bazel's java_library implicit
lib<name>-src.jar target - if that target did not run and the sources
jar does not exist, pomgen skips it.
deploy_all: deploys binary, sources, javadoc and pom artifacts to Nexus.
Uses credentials from ~/.m2/settings.xml's "default" server entry.
The sources jar is built by Bazel's java_library implicit
lib<name>-src.jar target - if that target did not run and the sources
jar does not exist, pomgen skips it.
deploy_only: re-attempts upload of all artifacts to Nexus. Assumes
"deploy_all" has run once. This is useful for debugging upload issues.
Uses credentials from ~/.m2/settings.xml's "default" server entry.
clean: removes generated pom files and Maven build "target" directories
from the src tree.
build: builds all libraries being processed (bazel build path/to/lib/...).
pomgen expects all jars that will be uploaded or installed locally to
exist - in case they do not, this action can be helpful.
Supported environment variables:
MVN_ARGS: may be used to pass additional arguments to Maven.
For example to point to settings.xml in a non-standard location:
export MVN_ARGS="--settings /my/path/to/settings.xml"
REPOSITORY_URL: for the 2 deploy actions, the environment variable
REPOSITORY_URL must be set to the remote artifact repository to upload to.
For example, when using Nexus:
export REPOSITORY_URL=https://nexus.host/nexus/repository"
Artifacts will either be uploaded to ${REPOSITORY_URL}/snapshots
or ${REPOSITORY_URL}/releases, based on whether the artifact
version ends in -SNAPTSHOT or not.
REPOSITORY_ID: in settings.xml, the <id> of the <server> entry to use for
authentication. Defaults to "nexus".
See https://maven.apache.org/plugins/maven-deploy-plugin/deploy-file-mojo.html
POM_DESCRIPTION: if set, used as the value of the <description> element
in the generated pom(s).
BZL_BUILD_WILDCARD: the 'build' action uses '...' as wildcard when building
from the root of each library directory. This env var controls the
wildcard to use, specifically in some cases it may be useful to use
'...:all-targets' to include targets not built by default (such as
_deploy.jar from java_binary rules)
Examples (run from repository root):
Generate poms for the hello-world example:
bazel run @pomgen//maven -- -a pomgen -l examples/hello-world
Install all example artifacts into the local Maven repository:
bazel run @pomgen//maven -- -a install -l examples/hello-world
Upload all examples, including javadoc and source jars, to Nexus:
bazel run @pomgen//maven -- -a deploy_all
More than one action may be specified, for example, to generate poms and
then install to the local Maven repository:
bazel run @pomgen//maven -- -a pomgen,install
EOF
}
_build_libraries_file_path() {
local repo_root_path=$1
local library=$2
local libraries_file_path="$repo_root_path/bazel-bin$library/libraries.txt"
echo "$libraries_file_path"
}
_use_libraries_hint_file() {
local repo_root_path=$1
local follow_libraries=$2
local library_path=$3
if [ "$follow_libraries" = false ]; then
echo "false"
else
if [ -z "$library_path" ]; then
echo "false"
else
local path="$(_build_libraries_file_path $repo_root_path $library_path)"
if [ -f "$path" ]; then
echo "true"
else
echo "false"
fi
fi
fi
}
_for_each_library() {
local action=$1
local repo_root_path=$2
local $pom_base_filename=$3
local jar_artifact_classifier=$4
local library_path=$5
if ! [[ "$action" =~ ^(install|build)$ ]]; then
echo "ERROR: Unknown action $action" && exit 1
fi
libraries_file_path="$(_build_libraries_file_path $repo_root_path $library_path)"
echo "[INFO] libraries to process:"
cat "$libraries_file_path"
echo ""
while read library_path;
do
if [[ "$library_path" == \#* ]]; then
# the line starts with the comment character
continue
fi
echo "[INFO] Processing library: $library_path"
if [ "$action" == "install" ]; then
_for_each_pom "install_main_artifact" $repo_root_path $pom_base_filename $jar_artifact_classifier "/$library_path"
elif [ "$action" == "build" ]; then
local cmd="bazel build ${library_path}/${BZL_BUILD_WILDCARD:-"..."}"
echo "[INFO] Running $cmd"
$cmd
fi
done <<< "$(cat $libraries_file_path)"
}
if [ "$#" -eq 0 ]; then
print_usage && exit 1
fi
library_path=""
force_pomgen=false
follow_references=true
debug=false
while getopts "a:l:t:dhfi" option; do
case $option in
a ) actions=$OPTARG
;;
l ) library_path=$OPTARG
;;
d ) debug=true
;;
f ) force_pomgen=true
;;
i ) follow_references=false
;;
h ) print_usage && exit 1
;;
t ) echo "-t has been replaced by -l (-l path/to/library)" && exit 1
esac
done
if [ "$debug" = true ]; then
echo "[DEBUG] Debug logging enabled"
fi
if [ -z "$actions" ] ; then
echo "[ERROR] The action(s) to run must be specified using -a, for example:"
echo " $ bazel run @pomgen//maven -- -a pomgen,install"
echo " bazel run @pomgen//maven for usage information."
exit 1
fi
if [ -z "$library_path" ] ; then
echo "[INFO] No library specified, defaulting to 'all'"
library_path="/."
elif [[ $library_path != /* ]]; then
# library_path needs to start with a '/'
library_path=/$library_path
fi
echo "[INFO] Running ${actions} for library: ${library_path}"
if [ "$debug" = true ]; then
echo "[DEBUG] Running from directory: $(pwd)"
echo "[DEBUG] Directory content: $(find . )"
echo "[DEBUG] Environment: $(env)"
fi
# load helper functions
helper_functions_file="maven_functions.sh"
this_script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [ -f "${this_script_dir}/${helper_functions_file}" ]; then
# look relative to this file
source "${this_script_dir}/${helper_functions_file}"
else
# to support running through "bazel run", look in a few other places
p="external/pomgen/maven/${helper_functions_file}"
if [ -f "${p}" ]; then
# remote repository
source "${p}"
else
p="maven/${helper_functions_file}"
if [ -f "${p}" ]; then
# local pomgen repository
source "${p}"
else
echo "[ERROR] Unable to locate ${helper_functions_file}" && exit 1
fi
fi
fi
# figure out where this script is being run from, accordingly set repo_root_path
if [ -f "WORKSPACE" ]; then
repo_root_path=`pwd`
else
# "bazel run" sets the env var BUILD_WORKING_DIRECTORY, which points
# to the root of the repository
if [ -f "$BUILD_WORKING_DIRECTORY/WORKSPACE" ]; then
# $BUILD_WORKING_DIRECTORY is set by "bazel run"
repo_root_path=$BUILD_WORKING_DIRECTORY
cd $repo_root_path
else
echo "[ERROR] This script must run from the repository root"
exit 1
fi
fi
# load the configured classifier to use for jar artifacts, this classifier is
# used for jars processed below by this script, and it is added to generated
# pom.xml files referencing those jars
jar_artifact_classifier=$(bazel run @pomgen//misc:configvalueloader -- --key artifact.jar_classifier --default None)
# also load the pom base filename - I guess we shouldn't keep adding these load
# stmts, so if we end up adding a 3rd one, refactor to bulk load in a single
# call
pom_base_filename=$(bazel run @pomgen//misc:configvalueloader -- --key general.pom_base_filename)
for action in $(echo $actions | tr "," "\n")
do
if ! [[ "$action" =~ ^(clean|pomgen|install|install_all|deploy_all|deploy_only|build)$ ]]; then
echo "[ERROR] action [$action] must be one of [clean|install|install_all|deploy_all|deploy_only]" && exit 1
fi
echo ""
echo "Running action [$action]"
echo ""
if [ "$action" == "clean" ]; then
_for_each_pom "clean_source_tree" $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
elif [ "$action" == "pomgen" ]; then
# this script uses the special libraries hint file, which contains
# the path to the upstream libraries
extra_args="--write_libraries_hint_file"
if [ "$debug" = true ]; then
extra_args="${extra_args} --verbose"
fi
if [ "$force_pomgen" = true ]; then
extra_args="${extra_args} --force"
fi
if [ "$follow_references" = false ]; then
extra_args="${extra_args} --ignore_references"
fi
if [ "$debug" = true ]; then
echo "[DEBUG] Running with pomgen extra args ${extra_args}"
fi
bazel run @pomgen//:pomgen -- \
--package $library_path \
--destdir $repo_root_path/bazel-bin \
--pom.description "${POM_DESCRIPTION:-""}" $extra_args
elif [ "$action" == "install" ]; then
if [ "$(_use_libraries_hint_file $repo_root_path $follow_references $library_path)" == "true" ]; then
_for_each_library $action $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
else
_for_each_pom "install_main_artifact" $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
fi
elif [ "$action" == "install_all" ]; then
# do not specify $library_path below because the javadoc maven plugin
# looks for dependencies
_for_each_pom "install_main_artifact" $repo_root_path $pom_base_filename $jar_artifact_classifier
_for_each_pom "build_javadoc_jar" $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
_for_each_pom "install_sources_and_javadoc_jars" $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
elif [ "$action" == "deploy_all" ]; then
if [ -z "$REPOSITORY_URL" ]; then
echo "[ERROR] REPOSITORY_URL must be set"
exit 1
fi
# no filter below because the javadoc maven plugin looks for
# dependencies
_for_each_pom "install_main_artifact" $repo_root_path $pom_base_filename $jar_artifact_classifier
_for_each_pom "build_javadoc_jar" $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
_for_each_pom "upload_all_artifacts" $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
elif [ "$action" == "deploy_only" ]; then
if [ -z "$REPOSITORY_URL" ]; then
echo "[ERROR] REPOSITORY_URL must be set"
exit 1
fi
_for_each_pom "upload_all_artifacts" $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
elif [ "$action" == "build" ]; then
if [ "$(_use_libraries_hint_file $repo_root_path $follow_references $library_path )" == "true" ]; then
_for_each_library $action $repo_root_path $pom_base_filename $jar_artifact_classifier $library_path
else
echo "[ERROR] The 'build' action requires -l to point to a library"
echo " The 'pomgen' action must already have run for this library"
echo " Building is not supported with -i, you can just run"
echo " 'bazel build' directly if you do not want to follow references"
exit 1
fi
fi
done