Skip to content

Commit 6c39952

Browse files
ctruedenclaude
andcommitted
release-version.sh: support multi-module repositories
To release one module of a multi-module repository, run the script from that module's directory. The maven-release-plugin handles this case correctly on its own: it commits and tags from the module directory, writes release.properties there, and release:perform then builds only that module from the checked-out tag. What remains is managing versions of sibling modules: - Before release:prepare, the module's SNAPSHOT dependencies on sibling modules are pinned to their latest releases, discovered from the release.properties files of existing tags. This pin commit is then squashed into the release commit on the tag, so that the tag shows all changes made for the release at once. If a sibling has changed since its latest release, the script refuses to continue (unless --skip-version-check is given), since that sibling should be released first. - Afterward, those dependencies are restored to SNAPSHOT versions, and sibling modules depending on the released module are updated to its new development version, all within the single "Bump to next development cycle" commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 293fbb5 commit 6c39952

1 file changed

Lines changed: 180 additions & 8 deletions

File tree

‎release-version.sh‎

Lines changed: 180 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,59 @@ no_changes_pending() {
3030
git diff-index --cached --quiet --ignore-submodules HEAD --
3131
}
3232

33+
# -- Multi-module helper functions --
34+
35+
# Prints the module directories listed in the given aggregator POM.
36+
pom_modules() {
37+
sed -n 's;.*<module>\([^<]*\)</module>.*;\1;p' "$1"
38+
}
39+
40+
# Prints the artifactId of the given POM, ignoring that of its parent.
41+
pom_artifactId() {
42+
awk '
43+
/<parent>/ { inParent = 1 }
44+
/<\/parent>/ { inParent = 0; next }
45+
!inParent && /<artifactId>/ {
46+
sub(/.*<artifactId>/, ""); sub(/<\/artifactId>.*/, ""); print; exit
47+
}
48+
' "$1"
49+
}
50+
51+
# Prints the names of properties used as the version of the given artifactId
52+
# in the given POM. E.g., <version>${scijava-meta.version}</version>.
53+
# Note: This assumes the artifactId element precedes the version element.
54+
pom_versionProperties() {
55+
awk -v artifactId="$2" '
56+
/<artifactId>/ {
57+
a = $0; sub(/.*<artifactId>/, "", a); sub(/<\/artifactId>.*/, "", a)
58+
}
59+
a == artifactId && /<version>\$\{[^}]*\}<\/version>/ {
60+
p = $0; sub(/.*<version>\$\{/, "", p); sub(/\}<\/version>.*/, "", p); print p
61+
}
62+
' "$1" | sort -u
63+
}
64+
65+
# Prints the value of the given property in the given POM.
66+
pom_property() {
67+
sed -n "s;.*<$2>\([^<]*\)</$2>.*;\1;p" "$1" | head -n1
68+
}
69+
70+
# Sets the value of the given property in the given POM.
71+
set_pom_property() {
72+
sed -i.bak "s;<$2>[^<]*</$2>;<$2>$3</$2>;" "$1" &&
73+
rm -f "$1.bak"
74+
}
75+
76+
# Prints the latest released version of the given artifactId, followed by
77+
# the tag from which it was released, by scanning the release.properties
78+
# files of all tags. This works both for components released individually
79+
# and for those released together as part of an aggregator.
80+
latest_release() {
81+
git grep -F -e "\\:$1=" $(git tag -l) -- ':/*release.properties' 2>/dev/null |
82+
sed -n 's;^\([^:]*\):[^:]*:project\.rel\.[^=]*=\(.*\);\2 \1;p' |
83+
sort -V | tail -n1
84+
}
85+
3386
# -- Constants and settings --
3487

3588
SCIJAVA_BASE_REPOSITORY=-DaltDeploymentRepository=scijava.releases::default::dav:https://maven.scijava.org/content/repositories
@@ -100,9 +153,15 @@ die "Usage: $0 [options] [<version>]
100153
101154
Where <version> is the version to release. If omitted, it will prompt you.
102155
156+
To release a module of a multi-module repository, run this script from
157+
the module's directory. Its SNAPSHOT dependencies on sibling modules are
158+
pinned to their latest releases for the release, and afterward, sibling
159+
modules depending on it are updated to its new development version.
160+
103161
Options include:
104162
--dry-run - Simulate the release without actually doing it.
105-
--skip-version-check - Skips the SemVer and parent pom version checks.
163+
--skip-version-check - Skips the SemVer, parent pom and sibling module
164+
version checks.
106165
--skip-branch-check - Skips the default branch check.
107166
--skip-license-update - Skips update of the copyright blurbs.
108167
--skip-push - Do not push to the remote git repository.
@@ -220,6 +279,62 @@ If you are certain you want to release from this branch,
220279
try again with --skip-branch-check flag."
221280
}
222281

282+
# Detect whether we are releasing a module of a multi-module repository.
283+
# If so, the module is released on its own, from its own directory.
284+
debug "Checking for multi-module repository"
285+
moduleDir=
286+
if test "$(git rev-parse --show-prefix)" -a -f ../pom.xml
287+
then
288+
pom_modules ../pom.xml | grep -qxF "$(basename "$PWD")" &&
289+
moduleDir=$(basename "$PWD")
290+
fi
291+
if test "$moduleDir"
292+
then
293+
debug "Releasing module '$moduleDir' of multi-module repository"
294+
artifactId=$(pom_artifactId pom.xml)
295+
test "$artifactId" || die "Could not extract artifactId from pom.xml"
296+
siblingDirs=$(pom_modules ../pom.xml | grep -vxF "$moduleDir")
297+
298+
# Plan which SNAPSHOT versions of sibling modules to pin to releases.
299+
# Each line of siblingPins is: <property> <snapshot-version> <release-version>
300+
debug "Checking versions of sibling module dependencies"
301+
siblingPins=
302+
for siblingDir in $siblingDirs
303+
do
304+
test -f "../$siblingDir/pom.xml" || continue
305+
siblingArtifactId=$(pom_artifactId "../$siblingDir/pom.xml")
306+
for property in $(pom_versionProperties pom.xml "$siblingArtifactId")
307+
do
308+
snapshotVersion=$(pom_property pom.xml "$property")
309+
case "$snapshotVersion" in
310+
*-SNAPSHOT) ;;
311+
*) continue ;;
312+
esac
313+
release=$(latest_release "$siblingArtifactId")
314+
test "$release" || die "No release of sibling module '$siblingArtifactId' found.
315+
Please release it first, then try again."
316+
releaseVersion=${release%% *}
317+
releaseTag=${release#* }
318+
debug "Pinning $property: $snapshotVersion -> $releaseVersion"
319+
# Note: Changes to versions, SCM tags and copyright years do not count.
320+
test "$SKIP_VERSION_CHECK" ||
321+
git diff --quiet \
322+
-I'<version>[^<]*</version>' \
323+
-I'\.version>[^<]*</' \
324+
-I'<tag>[^<]*</tag>' \
325+
-I'Copyright (C)' \
326+
"$releaseTag" HEAD -- "../$siblingDir" \
327+
":(exclude)../$siblingDir/release.properties" ||
328+
die "Sibling module '$siblingArtifactId' has changed since its $releaseVersion release.
329+
Please release it first, so that $artifactId can depend on those changes.
330+
Use \"git diff $releaseTag HEAD -- ../$siblingDir\" to see what changed.
331+
Or if you are sure, try again with --skip-version-check flag."
332+
siblingPins="$siblingPins$property $snapshotVersion $releaseVersion
333+
"
334+
done
335+
done
336+
fi
337+
223338
# If REMOTE is unset, use branch's upstream remote by default.
224339
REMOTE="${REMOTE:-$remote}"
225340

@@ -337,26 +452,81 @@ exclude them by setting license.excludes in your POM; e.g.:
337452
Alternately, try again with the --skip-license-update flag.'
338453
}
339454

455+
# Pin SNAPSHOT dependencies on sibling modules to their release versions.
456+
# The release plugin refuses to release with SNAPSHOT dependencies. This pin
457+
# commit is later squashed into the release commit, and is not on the branch.
458+
preReleaseHead=$(git rev-parse HEAD)
459+
if test "$siblingPins"
460+
then
461+
debug "Pinning sibling module dependencies to release versions"
462+
printf '%s' "$siblingPins" | while read property snapshotVersion releaseVersion
463+
do
464+
$DRY_RUN set_pom_property pom.xml "$property" "$releaseVersion"
465+
done &&
466+
$DRY_RUN git commit -m "Use release versions of sibling modules" pom.xml ||
467+
die 'Failed to pin sibling module dependencies to release versions.'
468+
fi
469+
340470
# Prepare new release without pushing (requires the release plugin >= 2.1).
341471
debug "Preparing new release"
342472
$DRY_RUN mvn $BATCH_MODE release:prepare -DpushChanges=false -Dresume=false $TAG \
343473
$PROFILE $DEV_VERSION -DreleaseVersion="$VERSION" \
344474
"-Darguments=-Dgpg.skip=true ${EXTRA_ARGS# }" ||
345-
die 'The release preparation step failed -- look above for errors and fix them.
346-
Use "mvn javadoc:javadoc | grep error" to check for javadoc syntax errors.'
475+
die "The release preparation step failed -- look above for errors and fix them.
476+
Use \"mvn javadoc:javadoc | grep error\" to check for javadoc syntax errors.
477+
Use \"mvn release:clean && git reset --hard $preReleaseHead\" to start over."
347478

348-
# Squash the maven-release-plugin's two commits into one.
479+
# Squash the maven-release-plugin's two commits (and any pin commit) into one.
349480
if test -z "$DRY_RUN"
350481
then
351482
debug "Squashing release commits"
352483
test "[maven-release-plugin] prepare for next development iteration" = \
353484
"$(git show -s --format=%s HEAD)" ||
354485
die "maven-release-plugin's commits are unexpectedly missing!"
355486
fi
356-
$DRY_RUN git reset --soft HEAD^^ &&
487+
$DRY_RUN git reset --soft "$preReleaseHead" &&
488+
489+
# For a module of a multi-module repository: restore its SNAPSHOT dependencies
490+
# on sibling modules, and update sibling modules depending on it to use its new
491+
# development version.
492+
if test "$moduleDir"
493+
then
494+
debug "Updating sibling module dependency versions"
495+
printf '%s' "$siblingPins" | while read property snapshotVersion releaseVersion
496+
do
497+
$DRY_RUN set_pom_property pom.xml "$property" "$snapshotVersion"
498+
done &&
499+
if test -z "$DRY_RUN"
500+
then
501+
devVersion=$(sed -n "s;^project\.dev\.[^=]*:$artifactId=;;p" release.properties)
502+
test "$devVersion" || die "Could not extract development version from release.properties"
503+
else
504+
devVersion="<dev-version>"
505+
fi &&
506+
for siblingDir in $siblingDirs
507+
do
508+
siblingPom="../$siblingDir/pom.xml"
509+
test -f "$siblingPom" || continue
510+
for property in $(pom_versionProperties "$siblingPom" "$artifactId")
511+
do
512+
case "$(pom_property "$siblingPom" "$property")" in
513+
*-SNAPSHOT)
514+
debug "Updating $property in $siblingPom to $devVersion"
515+
$DRY_RUN set_pom_property "$siblingPom" "$property" "$devVersion"
516+
;;
517+
esac
518+
done
519+
done &&
520+
$DRY_RUN git add -u -- ..
521+
fi &&
357522
if ! git diff-index --cached --quiet --ignore-submodules HEAD --
358523
then
359-
$DRY_RUN git commit -s -m "Bump to next development cycle"
524+
if test "$moduleDir"
525+
then
526+
$DRY_RUN git commit -s -m "Bump $artifactId to next development cycle"
527+
else
528+
$DRY_RUN git commit -s -m "Bump to next development cycle"
529+
fi
360530
fi &&
361531

362532
# Check that release-only files were not erroneously committed.
@@ -387,7 +557,8 @@ else
387557
tag="<tag>"
388558
fi &&
389559

390-
# Rewrite the tag to include release.properties.
560+
# Rewrite the tag to include release.properties, squashing any pin commit
561+
# into the release commit, so that the tag shows all release changes at once.
391562
debug "Rewriting tag to include release.properties"
392563
test -n "$tag" &&
393564
# HACK: SciJava projects use SSH (git@github.com:...) for developerConnection.
@@ -399,8 +570,9 @@ test -n "$tag" &&
399570
$DRY_RUN sed -i.bak -e 's|^scm.url=scm\\:git\\:git@github.com\\:|scm.url=scm\\:git\\:https\\://github.com/|' release.properties &&
400571
$DRY_RUN rm release.properties.bak &&
401572
$DRY_RUN git checkout "$tag" &&
573+
$DRY_RUN git reset --soft "$preReleaseHead" &&
402574
$DRY_RUN git add -f release.properties &&
403-
$DRY_RUN git commit --amend --no-edit &&
575+
$DRY_RUN git commit -C "$tag" &&
404576
$DRY_RUN git tag -d "$tag" &&
405577
$DRY_RUN git tag "$tag" HEAD &&
406578
$DRY_RUN git checkout @{-1} &&

0 commit comments

Comments
 (0)