11#! /usr/bin/env bash
22
3- mvn release:clean release:prepare
3+ set -e # Exit on any error
44
5- mvn release:perform
5+ # Colors for output
6+ RED=' \033[0;31m'
7+ GREEN=' \033[0;32m'
8+ YELLOW=' \033[1;33m'
9+ NC=' \033[0m' # No Color
10+
11+ # Function to print colored output
12+ print_status () {
13+ echo -e " ${GREEN} [INFO]${NC} $1 "
14+ }
15+
16+ print_warning () {
17+ echo -e " ${YELLOW} [WARN]${NC} $1 "
18+ }
19+
20+ print_error () {
21+ echo -e " ${RED} [ERROR]${NC} $1 "
22+ }
23+
24+ # Check if we're on develop branch
25+ CURRENT_BRANCH=$( git rev-parse --abbrev-ref HEAD)
26+ if [ " $CURRENT_BRANCH " != " develop" ]; then
27+ print_warning " Currently on branch: $CURRENT_BRANCH "
28+ print_warning " It's recommended to start releases from 'develop' branch"
29+ read -p " Continue anyway? (y/N): " -n 1 -r
30+ echo
31+ if [[ ! $REPLY =~ ^[Yy]$ ]]; then
32+ print_error " Aborted by user"
33+ exit 1
34+ fi
35+ fi
36+
37+ # Ensure working directory is clean
38+ if ! git diff-index --quiet HEAD --; then
39+ print_error " Working directory is not clean. Please commit or stash changes first."
40+ exit 1
41+ fi
42+
43+ # Get current version from pom.xml
44+ CURRENT_VERSION=$( mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
45+ print_status " Current version: $CURRENT_VERSION "
46+
47+ # Extract release version (remove -SNAPSHOT if present)
48+ RELEASE_VERSION=${CURRENT_VERSION% -SNAPSHOT}
49+ RELEASE_BRANCH=" release/$RELEASE_VERSION "
50+
51+ print_status " Creating release branch: $RELEASE_BRANCH "
52+
53+ # Create and switch to release branch
54+ git checkout -b " $RELEASE_BRANCH "
55+
56+ print_status " Starting Maven release process..."
57+
58+ # Configure Maven release plugin to not push changes automatically
59+ mvn release:clean release:prepare -DpushChanges=false -DlocalCheckout=true
60+
61+ print_status " Performing Maven release (deploying to Sonatype)..."
62+ mvn release:perform
63+
64+ # Capture the commit hashes
65+ RELEASE_COMMIT=$( git log --oneline -2 --pretty=format:" %H" | tail -1)
66+ SNAPSHOT_COMMIT=$( git log --oneline -1 --pretty=format:" %H" )
67+
68+ print_status " Release commit: $RELEASE_COMMIT "
69+ print_status " Development commit (SNAPSHOT bump): $SNAPSHOT_COMMIT "
70+
71+ # Switch to master and merge only the release commit
72+ print_status " Merging release commit to master branch..."
73+ git checkout master
74+ git pull origin master # Ensure master is up to date
75+
76+ # Merge only the release commit (not the SNAPSHOT bump)
77+ git merge --no-ff " $RELEASE_COMMIT " -m " Release version $RELEASE_VERSION "
78+
79+ # Push master branch with tags
80+ print_status " Pushing master branch and tags..."
81+ git push origin master
82+ git push origin --tags
83+
84+ # Switch to develop and merge the SNAPSHOT commit
85+ print_status " Merging development version back to develop..."
86+ git checkout develop
87+ git pull origin develop # Ensure develop is up to date
88+
89+ # Merge the entire release branch (including SNAPSHOT bump)
90+ git merge --no-ff " $RELEASE_BRANCH " -m " Post-release version bump"
91+
92+ # Push develop branch
93+ git push origin develop
94+
95+ # Clean up release branch
96+ print_status " Cleaning up release branch..."
97+ git branch -d " $RELEASE_BRANCH "
98+
99+ # Optional: delete remote release branch if it was pushed
100+ if git ls-remote --heads origin " $RELEASE_BRANCH " | grep -q " $RELEASE_BRANCH " ; then
101+ print_warning " Remote release branch exists. Delete it? (y/N): "
102+ read -p " " -n 1 -r
103+ echo
104+ if [[ $REPLY =~ ^[Yy]$ ]]; then
105+ git push origin --delete " $RELEASE_BRANCH "
106+ fi
107+ fi
108+
109+ print_status " Release $RELEASE_VERSION completed successfully!"
110+ print_status " - Master branch contains release version $RELEASE_VERSION "
111+ print_status " - Develop branch contains next development version"
112+ print_status " - Artifacts deployed to Sonatype"
113+
114+ # Show final status
115+ print_status " Current branch status:"
116+ echo " Master: $( git log --oneline -1 master) "
117+ echo " Develop: $( git log --oneline -1 develop) "
0 commit comments