Skip to content

Instantly share code, notes, and snippets.

@steve-taylor
Created September 24, 2017 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steve-taylor/20bbc5f6b77d5e46c39c6b85b2d635a0 to your computer and use it in GitHub Desktop.
Save steve-taylor/20bbc5f6b77d5e46c39c6b85b2d635a0 to your computer and use it in GitHub Desktop.
Version bump and publish Maven+Git project
#!/bin/bash
#
# Version bump and publish this Git+Maven repo.
#
# Usage: ./publish.sh {major|minor|patch}
#
# For example, to bump the version from 1.5.9 to 1.6.0:
#
# ./publish.sh minor
# Validate command line
if
[[ $# -ne 1 ]] || \
! [[ $1 =~ ^(major|minor|patch)$ ]];
then
echo "Usage: $0 {major|minor|patch}"
exit 1
fi
# Ensure we're on the develop branch and up-to-date with origin
git fetch
git checkout develop
git reset --hard origin/develop
# Get the current project's version from its pom.xml
current_version=$(mvn -q -Dexec.executable='echo' -Dexec.args='${project.version}' --non-recursive exec:exec)
current_version_semver=( ${current_version//./ } )
# Extract the major, minor, and patch components from the current version
major="${current_version_semver[0]}"
minor="${current_version_semver[1]}"
patch="${current_version_semver[2]}"
# Validate the current version. (We can't bump it if it's not valid semver.)
if
[[ ${#current_version_semver[@]} -ne 3 ]] || \
! [[ ${major} =~ ^[0-9]+$ ]] || \
! [[ ${minor} =~ ^[0-9]+$ ]] || \
! [[ ${patch} =~ ^[0-9]+$ ]];
then
echo "Current version (${current_version}) is not valid semver. Aborting."
exit 1
fi
case "$1" in
major)
new_version="$((major+1)).0.0"
;;
minor)
new_version="${major}.$((minor+1)).0"
;;
patch)
new_version="${major}.${minor}.$((patch+1))"
;;
*)
# If this happens, it's a bug
echo "Well, this is embarrassing. The command line argument should have already been validated."
exit 1
esac
# Version bump pom.xml
mvn -q versions:set -DnewVersion=${new_version}
# Commit and tag the version
git add -A
git commit -m "Version ${new_version}"
git tag -a "v${new_version}" -m "Version ${new_version}"
# Merge new version into master
git checkout master
git reset --hard origin/master
git merge develop
# Push everything and go back to develop
git push origin master
git push --tags
git checkout develop
git push
echo "$current_version -> $new_version"
@steve-taylor
Copy link
Author

Put this in the root of your Maven project (alongside .git and pom.xml). After you have merged one or more pull requests to develop, run this script to version the latest batch of merged pull requests. This will

  • version bump pom.xml
  • commit the pom.xml change to develop
  • tag develop, using the new version
  • merge develop to master
  • push develop, master and the new tag to origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment