forked from CogComp/cogcomp-nlp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
increment-pom-versions.sh
executable file
·210 lines (174 loc) · 7.35 KB
/
increment-pom-versions.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
#!/bin/bash
#################################################################################
# This script, run from the root of a Maven single or multi-module project, will
# update the pom files to increment the build number on the version.
# If a newer version of a parent pom exists, that will also be updated to the
# current latest version.
# If desired, dependencies (all or a subset) can also be updated. This is
# generally useful for internal libraries where you'd probably want to switch to
# using the latest as soon as possible.
#
# Here are some examples as to what this will to with the version
# (existing --> after script run):
# 1.0-SNAPSHOT --> 1.1
# 1.0.0.0 --> 1.0.0.1
# 1.0.0.1 --> 1.0.0.2
# 8 --> 9
#
# Nonstandard versions (e.g. with alphabetic characters) have NOT been tested
# thoroughly with this! Test with -d (dry run) to make sure this doesn't hose
# things.
#################################################################################
# The following can contain multiple <groupId>:<artifactId> pairings. If more are
# desired, each should be space-delimited. The format of each is:
# "groupId:artifactId[:type:classifier:version]"
# See http://mojo.codehaus.org/versions-maven-plugin/use-latest-versions-mojo.html#includesList
# for more information. If this is left as an empty string, no dependency updates
# will be done.
#UPDATE_DEPENDENCIES_WITH_GROUPID="com.example*:*"
#UPDATE_DEPENDENCIES_WITH_GROUPID="com.example.foo:bar:1.*"
#UPDATE_DEPENDENCIES_WITH_GROUPID="com.example.foo:* com.example.bar:* com.example.fluffy:*"
UPDATE_DEPENDENCIES_WITH_GROUPID=""
#################################################################################
MAVEN_BIN=`which mvn`
MAVEN_VERSIONS_PLUGIN="org.codehaus.mojo:versions-maven-plugin:1.3.1"
MAVEN_VERSIONS_PLUGIN_SET_GOAL="${MAVEN_VERSIONS_PLUGIN}:set -DgenerateBackupPoms=false"
MAVEN_VERSIONS_PLUGIN_UPDATE_PARENT_GOAL="${MAVEN_VERSIONS_PLUGIN}:update-parent -DgenerateBackupPoms=false -DallowSnapshots=true"
MAVEN_VERSIONS_PLUGIN_UPDATE_DEPENDENCIES_GOAL="${MAVEN_VERSIONS_PLUGIN}:use-latest-versions -DgenerateBackupPoms=false -DallowSnapshots=false"
MAVEN_HELP_PLUGIN="org.apache.maven.plugins:maven-help-plugin:2.1.1"
MAVEN_HELP_PLUGIN_EVALUATE_VERSION_GOAL="${MAVEN_HELP_PLUGIN}:evaluate -Dexpression=project.version"
COMMIT=false
LAST_COMMIT_HASH=`git log -1 --pretty=format:"%H"`
function printUsage() {
echo "Usage: ${0} [option(s)]"
echo
echo " -h Show this help text."
echo " -d At the end also commit to the master branch of origin"
echo " -v new_version Override generated version to the specific version."
echo " This should be in the format: major.minor.patch.build. (e.g. 2.8.1.0)"
}
while getopts ":v:bhdj" OPT; do
case ${OPT} in
d)
COMMIT=true
;;
h)
printUsage
exit 0
;;
v)
NEXT_PROJECT_VERSION="${OPTARG}"
;;
\?)
echo "Invalid option: -${OPTARG}" >&2
printUsage
exit 30
;;
:)
echo "Option -$OPTARG requires an argument." >&2
printUsage
exit 1
;;
esac
done
function validatePomExists() {
CURRENT_DIRECTORY=`pwd`
if [ -f pom.xml ] ; then
echo "Found pom.xml file: [${CURRENT_DIRECTORY}/pom.xml]"
else
echo "ERROR: No pom.xml file detected in current directory [${CURRENT_DIRECTORY}]. Exiting script with error status."
exit 50
fi
}
function validatePom() {
${MAVEN_BIN} validate
STATUS=`echo $?`
if [ ${STATUS} -ne 0 ] ; then
echo "ERROR: Maven POM did not validate successfully. Exiting script with error status."
exit 40
fi
}
function initCurrentProjectVersion() {
echo -n "Detecting current project version number..."
CURRENT_PROJECT_VERSION=`${MAVEN_BIN} ${MAVEN_HELP_PLUGIN_EVALUATE_VERSION_GOAL} | egrep '^[0-9\.]*(-SNAPSHOT)?$'`
if [ -z ${CURRENT_PROJECT_VERSION} ] ; then
echo " ERROR: Couldn't detect current version. Validating pom in case there was a validation issue."
validatePom
echo " ERROR: Couldn't detect current version. Exiting with error status."
exit 20
else
echo " Version found: [${CURRENT_PROJECT_VERSION}]"
fi
}
function initNextProjectVersion() {
local CLEANED=`echo ${CURRENT_PROJECT_VERSION} | sed -e 's/[^0-9][^0-9]*$//'`
local CURRENT_BUILD_NUMBER=`echo ${CLEANED} | sed -e 's/[0-9]*\.//g'`
local NEXT_BUILD_NUMBER=`expr ${CURRENT_BUILD_NUMBER} + 1`
echo "Sanitized current project version: [${CLEANED}]"
echo "Current build number in project version: [${CURRENT_BUILD_NUMBER}]"
echo "Calculated next build number: [${NEXT_BUILD_NUMBER}]"
if [ -z ${NEXT_PROJECT_VERSION} ] ; then
NEXT_PROJECT_VERSION=`echo ${CLEANED} | sed -e "s/[0-9][0-9]*\([^0-9]*\)$/${NEXT_BUILD_NUMBER}/"`
else
echo "Version number was overridden on the command line. Using [${NEXT_PROJECT_VERSION}] to calculate next version."
NEXT_PROJECT_VERSION="${NEXT_PROJECT_VERSION}"
fi
echo "Next project version: [${NEXT_PROJECT_VERSION}]"
}
function updateProjectPomsToNextVersion() {
echo "Updating project version to [${NEXT_PROJECT_VERSION}]..."
${MAVEN_BIN} ${MAVEN_VERSIONS_PLUGIN_SET_GOAL} -DnewVersion=${NEXT_PROJECT_VERSION}
}
function updateToLatestParentPom() {
echo "Updating parent pom to latest version..."
${MAVEN_BIN} ${MAVEN_VERSIONS_PLUGIN_UPDATE_PARENT_GOAL}
}
function updateToLatestDependencies() {
echo "Updating dependencies to latest versions..."
for DEPENDENCY in ${UPDATE_DEPENDENCIES_WITH_GROUPID} ; do
echo "Updating dependencies matching [${DEPENDENCY}]..."
${MAVEN_BIN} ${MAVEN_VERSIONS_PLUGIN_UPDATE_DEPENDENCIES_GOAL} -DincludesList=${DEPENDENCY}
done
}
function commitBuildNumberChanges() {
echo "Preparing updated files for commit..."
git status
# add the now updated pom files
echo "Adding pom files..."
for POM in `find . -name pom.xml` ; do
git add ${POM}
echo " - ${POM}"
done
echo "Committing changes..."
local BUILD_NUMBER_CHANGES_COMMIT_MESSAGE="Auto commit from CI - incremented build number from [${CURRENT_PROJECT_VERSION}] to [${NEXT_PROJECT_VERSION}]."
git commit -m "${BUILD_NUMBER_CHANGES_COMMIT_MESSAGE}"
echo "Pushing changes to master..."
git push origin master
}
#################################################################################
# Here's where the fun begins.
#################################################################################
# Make sure that there's a pom that we can do anything with.
validatePomExists
# Figure out which commit was last done and expose a variable with the branch
# name where that commit happened.
# TODO: don't do this yet... it's flaky and I need to figure out why.
# Assume master for now.
LAST_COMMIT_BRANCH="master"
#################################################################################
# Update the project POMs with the new build number.
#################################################################################
initCurrentProjectVersion
initNextProjectVersion
updateProjectPomsToNextVersion
updateToLatestParentPom
updateToLatestDependencies
#################################################################################
# Commit/Push updated files up to the repository
#################################################################################
if [ ${COMMIT} = true ] ; then
commitBuildNumberChanges
else
echo "Dry run specified. Skipping commit/push process."
fi
echo "Version updated successfully!"