Skip to content

Commit 66c734c

Browse files
committed
Update mvn-tools.
1 parent 7889467 commit 66c734c

File tree

5 files changed

+207
-97
lines changed

5 files changed

+207
-97
lines changed

mvn-tools/mvn-auto

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,20 @@ source mvn-tools || exit
2626
# TODO: Support for deleted file detection:
2727
# When a file is deleted from a source tree, the artifact should be
2828
# added to the reactor and first cleaned.
29+
# Support for pom artifacts:
30+
# POM artifacts have no target/ directory to compare mtimes against.
2931

3032

3133
# Functions.
3234
# loadModules -- invoke whenever the modules array is needed.
3335
loadModules() {
3436
(( ${#modules[@]} )) && return
37+
local profiles
3538

3639
# Build list of all modules allowed in the maven reactor.
37-
IFS=$'\n' read -d '' -r -a profiles < <(xpathNodes ~/.m2/settings.xml '/settings/activeProfiles/activeProfile/text()') ||:
40+
IFS=$'\n' read -d '' -r -a profiles < <(xmlstarlet sel -t -v '/_:settings/_:activeProfiles/_:activeProfile/text()' ~/.m2/settings.xml 2>/dev/null) ||:
3841
emit "Enumerating modules in the Maven reactor" --
39-
modules=()
42+
modules=(.)
4043
addModules() {
4144
grep -qF '<module>' "$1" || return 0
4245

@@ -52,23 +55,26 @@ loadModules() {
5255

5356
done < <(
5457
# Sub modules from the module's pom.xml's module list.
55-
xpathNodes "$1" '/project/modules/module/text()'
58+
xmlstarlet sel -t -v '/_:project/_:modules/_:module/text()' "$1" 2>/dev/null ||:
5659
5760
# Profile-enabled sub modules from the module's pom.xml's profiles module lists.
5861
grep -qF '<profile>' "$1" && {
5962
for profile in "${profiles[@]}"; do
60-
xpathNodes "$1" "/project/profiles/profile/id[text()='$profile']/../modules/module/text()"
63+
xmlstarlet sel -t -v "/_:project/_:profiles/_:profile/_:id[text()='$profile']/../_:modules/_:module/text()" "$1" 2>/dev/null ||:
6164
done
6265
}
6366
)
6467
}
6568
addModules ./pom.xml
6669
emit -$?
70+
71+
#printf ' - module: %s\n' "${modules[@]}" >&2
72+
(( ${#modules[@]} ))
6773
}
6874

6975

7076
# Options.
71-
while getopts :aABcCdDfnr:h arg; do
77+
while getopts :aABcCdDfn:h arg; do
7278
case $arg in
7379
a) autoFindArtifacts=1 ;;
7480
A) resetArtifacts=1 ;;
@@ -79,7 +85,6 @@ while getopts :aABcCdDfnr:h arg; do
7985
D) nodistribution=1 ;;
8086
f) force=1 ;;
8187
n) nodeploy=1 ;;
82-
r) remote=$OPTARG ;;
8388

8489
h) showHelp \
8590
'Build and Deploy' \
@@ -96,16 +101,15 @@ while getopts :aABcCdDfnr:h arg; do
96101
'-C' "Clean each artifact before building it (eg. when files were renamed/removed or to rebuild /lib)." \
97102
'-d distribution' "The name in the reactor of the distribution artifact. (Default: [top dir name]-distribution)" \
98103
'-D' "Do not try to build and deploy a distribution." \
104+
'-f' "Force default operations, no questions asked." \
99105
'-n' "Do not try to deploy the distribution after building it."
100-
'-r user@host' "Upload and deploy the distribution to user@host." \
101106

102107
exit ;;
103108
esac
104109
done
105110
shift $((OPTIND-1))
106111

107112
# Validate parameters.
108-
[[ $remote ]] || emit -y "Missing remote"
109113
distribution=${distribution:-${PWD##*/}-distribution}
110114
[[ -e $distribution ]] || distribution=
111115
(( nodistribution )) && distribution=
@@ -120,13 +124,13 @@ fi
120124
while (( ! nobuild )); do
121125

122126
if (( autoFindArtifacts )); then
123-
loadModules
127+
loadModules || emit -r "No modules found."
124128

125129
# Automatically find outdated artifacts.
126130
emit "Scanning for outdated artifacts"
127131
while read file; do
128132
files=("$file")
129-
[[ $file = ./pom.xml ]] && files=(*/pom.xml)
133+
#[[ $file = ./pom.xml ]] && files=(*/pom.xml)
130134

131135
for file in "${files[@]}"; do
132136
target=$file
@@ -136,39 +140,53 @@ while (( ! nobuild )); do
136140
target=${target#./}
137141

138142
# Only consider this target if it's in the module list.
139-
inArray "$target" "${modules[@]}" || continue
143+
inArray "$target" "${modules[@]}" || {
144+
#echo "Ignoring: $target, not a module." >&2
145+
continue
146+
}
140147

141148
emit "Adding (Outdated) : $bold$target$reset:$file"
142149
targets+=("$PWD/$target")
143150
done
144-
done < <(find . \
151+
done < <(export -f latest
152+
find . \
145153
\( -type d ! \( -exec bash -c 'test -e "$1/pom.xml"' -- {} \; -o -name 'src' -o -path '*/src/*' \) -prune \) -o \
146154
-name 'pom.xml' -exec bash -c '
147155
shopt -s nullglob
148-
ref=( "${1%/pom.xml}"/target/*.{[jwe]ar,zip} )
156+
ref=$(latest "${1%/pom.xml}"/target/*.{[jwe]ar,zip})
157+
#printf "Candidate: %s <> %s" "$1" "$ref" >&2
149158
if [[ -e $ref && $1 -nt $ref ]]; then
159+
#echo ", outdated." >&2
150160
echo "$1"
161+
else
162+
: #echo ", up-to-date." >&2
151163
fi
152164
:' -- {} \; -o \
153165
! -type d -path '*/src/main/*' -exec bash -c '
154166
shopt -s nullglob
155167
for f; do
156168
[[ ${f%%/src/main/*} != ${ref%/target/*} ]] && \
157-
ref=( "${f%%/src/main/*}"/target/*[.]{[jwe]ar,zip} )
169+
ref=$(latest "${f%%/src/main/*}"/target/*[.]{[jwe]ar,zip})
158170
if [[ ! -e $ref ]]; then
159-
continue
160-
#while [[ $f = */* && ! -e $f/pom.xml ]]
161-
#do f=${f%/*}; done
162-
#echo "$f"
171+
#continue
172+
while [[ $f = */* && ! -e $f/pom.xml ]]
173+
do f=${f%/*}; done
174+
echo "$f"
163175
elif [[ $f -nt $ref ]]; then
164176
echo "$f"
177+
#echo "outdated: $f" >&2
178+
else
179+
: #echo "uptodate: $f, $ref" >&2
165180
fi
166181
done
167182
:' -- {} + | sort -u)
168183
fi
169184

170185
# Add manually specified targets.
171186
opts=()
187+
for profile in "${profiles[@]}"; do
188+
opts+=("-P$profile")
189+
done
172190
for target; do
173191
[[ $target = -* ]] && { opts+=("$target"); continue; }
174192

@@ -177,9 +195,10 @@ while (( ! nobuild )); do
177195
done
178196
cd "$root"
179197

180-
(( ! ${#targets[@]} && ! force )) && {
181-
if ! ask -y!N 'No artifacts selected for build; build anyway?'; then
182-
if (( nodeploy || nodistribution )) || ! ask -Y!n 'Continue deployment with existing build?'; then
198+
(( ! ${#targets[@]} )) && {
199+
if (( force )) || ! ask -c 'y!N' 'No artifacts selected for build; build anyway?'; then
200+
if (( nodeploy || nodistribution || force )) || ! ask -c 'y!N' 'Continue deployment with existing build?'; then
201+
emit "No artifacts selected for build. Nothing to build or deploy."
183202
exit
184203
fi
185204

@@ -209,35 +228,56 @@ while (( ! nobuild )); do
209228
# Begin the build.
210229
if (( ${#targets[@]} )); then
211230
echo
212-
emit "Building"
213-
mvn -pl "$(printf '%s,' "${targets[@]}")" -npu -amd "${opts[@]}" ${cleanAll:+clean} install & mvnpid=$!
231+
if inArray . "${targets[@]}"; then
232+
emit "Building with: mvn -npu "${opts[@]}" ${cleanAll:+clean} install"
233+
mvn -npu "${opts[@]}" ${cleanAll:+clean} install & mvnpid=$!
234+
else
235+
emit "Building with: mvn -pl "$(printf '%s,' "${targets[@]}")" -npu -amd "${opts[@]}" ${cleanAll:+clean} install"
236+
mvn -pl "$(printf '%s,' "${targets[@]}")" -npu -amd "${opts[@]}" ${cleanAll:+clean} install & mvnpid=$!
237+
fi
214238
(( clean && ! nodistribution )) && [[ -e $distribution ]] && ( sleep 10; kill -0 $mvnpid 2>/dev/null && rm -rf "$distribution/target/" ) & cleanpid=$!
215-
trap "kill $mvnpid $cleanpid 2>/dev/null" EXIT
239+
trap "kill $mvnpid $cleanpid 2>/dev/null ||:" EXIT
216240
wait $mvnpid
217241
fi
218242

219243
break
220244
done
221245

222246
if [[ -e $distribution ]] && (( ! nodeploy && ! nodistribution )); then
223-
224-
# Upload the distribution.
225247
echo
226-
emit "Shutting down remote distribution"
227-
ssh -qt "$remote" "$(printf 's=%q/scripts/stop; [[ -e $s ]] || exit 0; "$s"' "${src%%/*}")"
228248

229-
emit "Unpacking distribution"
249+
# Find the package
230250
cd "$distribution/target/"
231-
unzip -uo "${distribution##*/}"-*.zip
232-
src=("${distribution##*/}"-*/*/); src=("${src[@]%/}")
233-
234-
emit "Uploading distribution to $remote"
235-
ssh -qt "$remote" "$(printf 's=%q/scripts/stop; [[ -e $s ]] || exit 0; "$s" -w' "${src%%/*}")"
236-
rsync -avP --delete-delay "${src[@]}" "$remote":"${src%%/*}/"
237-
238-
emit "(Re)Starting remote distribution"
239-
ssh -qt "$remote" "$(printf 'dist=%q
240-
"$dist/scripts/start" -f' "${src%%/*}")"
251+
shopt -s nullglob
252+
package=(*.{[jwe]ar,zip})
253+
shopt -u nullglob
254+
255+
# Handle the package
256+
case $package in
257+
*.zip)
258+
emit "Unpacking distribution"
259+
unzip -uo "$package"
260+
src=("${distribution##*/}"-*/*/); src=("${src[@]%/}")
261+
262+
emit "Shutting down remote distribution"
263+
ssh -qt ${deployToPort:+-p "$deployToPort"} "$deployToHost" "$(printf 'bash -lc %q' "$(printf 's=%q/scripts/stop; [[ -e $s ]] || exit 0; "$s"' "${deployToPath:-~}/${src%%/*}")")"
264+
265+
emit "Uploading distribution to $deployTo:${deployToPath:-~}/${src%%/*}"
266+
ssh -qt ${deployToPort:+-p "$deployToPort"} "$deployToHost" "$(printf 'bash -lc %q' "$(printf 's=%q/scripts/stop; [[ -e $s ]] || exit 0; "$s" -w' "${deployToPath:-~}/${src%%/*}")")"
267+
rsync -avP --delete-after -e "ssh ${deployToPort:+-p $deployToPort}" "${src[@]}" "$deployToHost":"${deployToPath:-~}/${src%%/*}/"
268+
269+
emit "(Re)Starting remote distribution"
270+
ssh -qt ${deployToPort:+-p "$deployToPort"} "$deployToHost" "$(printf 'bash -lc %q' "$(printf 'dist=%q; "$dist/scripts/start" -f' "${deployToPath:-~}/${src%%/*}")")"
271+
;;
272+
*.ear|*.war)
273+
emit "Uploading package to $deployTo:${deployToPath:-~}/$package"
274+
rsync -avP --delete-after -e "ssh ${deployToPort:+-p $deployToPort}" "$package" "$deployToHost":"${deployToPath:-~}"
275+
;;
276+
277+
*)
278+
emit -R "Don't know how to handle package: $package"
279+
;;
280+
esac
241281
fi
242282

243283
# Print timestamp.

mvn-tools/mvn-install

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# #
2020
# #
2121
# Copyright 2010, lhunath #
22-
# * http://lhunath.lyndir.com #
22+
# * http://www.lhunath.com #
2323
# * Maarten Billemont. #
2424
# #
2525
source bashlib
@@ -31,7 +31,7 @@ while getopts :fdu:h opt; do
3131
f) force=1
3232
;;
3333
d) deploy=1
34-
url=${url:-scpexe://maven.lyndir.com/usr/local/scm/maven2}
34+
url=$(ask -d "${url:-scpexe://devel-maven.lyndir.com/usr/local/scm/maven2}" "Deploy to:")
3535
;;
3636
u) url=$OPTARG
3737
;;
@@ -51,20 +51,34 @@ while getopts :fdu:h opt; do
5151
done
5252
shift $((OPTIND-1))
5353

54+
# Functions.
55+
parsePom() {
56+
name=$(xpath "$pomFile" '/project/artifactId/text()' 2>/dev/null | grep . || echo "$name")
57+
group=$(xpath "$pomFile" '/project/groupId/text()' 2>/dev/null | grep . || echo "$group")
58+
version=$(xpath "$pomFile" '/project/version/text()' 2>/dev/null | grep . || echo "$version")
59+
type=$(xpath "$pomFile" '/project/type/text()' 2>/dev/null | grep . || echo "$type") type=${type:-jar}
60+
}
61+
5462
# Defaults.
5563
url=${url:-file://$HOME/.m2/repository/}
5664

5765
(( ! $# )) && {
58-
emit -R "Please specify the archives to process as arguments to this script."
59-
exit 1
66+
if [[ -e pom.xml ]]; then
67+
pomFile=pom.xml
68+
parsePom
69+
set -- ~/.m2/repository/"${group//.//}/${name}/${version}/${name}-${version}.${type}"
70+
else
71+
ftl "Please specify the archives to process as arguments to this script."
72+
exit 1
73+
fi
6074
}
6175
for archive; do
6276
[[ $archive != *.?ar ]] && {
63-
emit -R "The file '$archive' is not a valid Java Archive file."
64-
ask -y!N "Are you sure you want to submit this to the maven repository?" || exit 1
77+
wrn 'The file %s is not a valid Java Archive file.' "$archive"
78+
ask -c 'y!N' "Are you sure you want to submit this to the maven repository?" || exit 1
6579
}
6680
[[ ! -e $archive ]] && {
67-
emit -R "The archive '$archive' does not exist."
81+
ftl 'The archive %s does not exist.' "$archive"
6882
exit 1
6983
}
7084
done
@@ -75,7 +89,7 @@ for file; do
7589
# Guess a few parameters from the filename. # a/b/foo-bar-2.0-sources.jar
7690

7791
archive=${file##*/} # foo-bar-2.0-sources.jar
78-
emit "For: $archive"
92+
inf 'For: %s' "$archive"
7993

8094
type=${archive##*.} # jar
8195

@@ -99,42 +113,42 @@ for file; do
99113
pomFile=${file%.*} # a/b/foo-bar-2.0-sources
100114
pomFile=${pomFile%-$classifier}.pom # a/b/foo-bar-2.0.pom
101115
if [[ -f $pomFile ]]; then
102-
emit "Using: $pomFile"
116+
inf 'Using: %s' "$pomFile"
103117
generatePom=
104-
name=$(xpath "$pomFile" '/project/artifactId/text()' 2>/dev/null | grep . || echo "$name")
105-
group=$(xpath "$pomFile" '/project/groupId/text()' 2>/dev/null | grep . || echo "$group")
106-
version=$(xpath "$pomFile" '/project/version/text()' 2>/dev/null | grep . || echo "$version")
107-
type=$(xpath "$pomFile" '/project/type/text()' 2>/dev/null | grep . || echo "$type")
118+
parsePom
108119
else
109120
pomFile=
110121
generatePom=true
111122
fi
112123

113124
# Ask the user to fill in/confirm the necessary parameters.
114-
if [[ ! $group ]] || (( ! force )); then group=$(ask +"$group" 'Group:'); fi
115-
if [[ ! $name ]] || (( ! force )); then name=$(ask +"$name" 'Name:'); fi
116-
if [[ ! $version ]] || (( ! force )); then version=$(ask +"$version" 'Version:'); fi
117-
if [[ ! $type ]] || (( ! force )); then type=$(ask +"$type" 'Package Type:'); fi
118-
if [[ ! $classifier ]] || (( ! force )); then classifier=$(ask +"$classifier" 'Classifier (sources/javadoc/none):'); fi
125+
if [[ ! $group ]] || (( ! force )); then group=$(ask -d "$group" 'Group:'); fi
126+
if [[ ! $name ]] || (( ! force )); then name=$(ask -d "$name" 'Name:'); fi
127+
if [[ ! $version ]] || (( ! force )); then version=$(ask -d "$version" 'Version:'); fi
128+
if [[ ! $type ]] || (( ! force )); then type=$(ask -d "$type" 'Package Type:'); fi
129+
if [[ ! $classifier ]] || (( ! force )); then classifier=$(ask -d "$classifier" 'Classifier (sources/javadoc/none):'); fi
119130
[[ $classifier = none ]] && classifier=
120131

132+
[[ $file = ~/.m2/repository/* ]] && { cp "$file" /tmp && file=/tmp/${file##*/}; }
133+
[[ $pomFile = ~/.m2/repository/* ]] && { cp "$pomFile" /tmp && pomFile=/tmp/${pomFile##*/}; }
134+
121135
mvn deploy:deploy-file \
122-
-Durl="$url" \
123-
-Dfile="$file" \
124-
-DgroupId="$group" \
125-
-DartifactId="$name" \
126-
-Dversion="$version" \
127-
-Dpackaging="$type" \
128-
-Dclassifier="$classifier" \
129-
${generatePom:+-DgeneratePom="$generatePom"} \
130-
${pomFile:+-DpomFile="$pomFile"}
136+
-Durl="$url" \
137+
-Dfile="$file" \
138+
-DgroupId="$group" \
139+
-DartifactId="$name" \
140+
-Dversion="$version" \
141+
-Dpackaging="$type" \
142+
-Dclassifier="$classifier" \
143+
${generatePom:+-DgeneratePom="$generatePom"} \
144+
${pomFile:+-DpomFile="$pomFile"} || continue
131145

132146
echo "<dependency>"
133147
echo " <groupId>$bold$group$reset</groupId>"
134148
echo " <artifactId>$bold$name$reset</artifactId>"
135149
echo " <version>$bold$version$reset</version>"
136150
[[ $type != jar ]] && {
137-
echo " <packaging>$bold$type$reset</packaging>"
151+
echo " <type>$bold$type$reset</type>"
138152
}
139153
[[ $classifier ]] && {
140154
echo " <classifier>$bold$classifier$reset</classifier>"

0 commit comments

Comments
 (0)