-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathGitFlowReleaseMojo.java
More file actions
346 lines (291 loc) · 11.2 KB
/
GitFlowReleaseMojo.java
File metadata and controls
346 lines (291 loc) · 11.2 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright 2014-2019 Aleksandr Mashchenko.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.amashchenko.maven.plugin.gitflow;
import java.util.HashMap;
import java.util.Map;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.util.StringUtils;
/**
* The git flow release mojo.
*
* @since 1.2.0
*/
@Mojo(name = "release", aggregator = true)
public class GitFlowReleaseMojo extends AbstractGitFlowMojo {
/** Whether to skip tagging the release in Git. */
@Parameter(property = "skipTag", defaultValue = "false")
private boolean skipTag = false;
/**
* Whether to skip calling Maven test goal before releasing.
*
* @since 1.0.5
*/
@Parameter(property = "skipTestProject", defaultValue = "false")
private boolean skipTestProject = false;
/**
* Whether to allow SNAPSHOT versions in dependencies.
*
* @since 1.2.2
*/
@Parameter(property = "allowSnapshots", defaultValue = "false")
private boolean allowSnapshots = false;
/**
* Whether to rebase branch or merge. If <code>true</code> then rebase will
* be performed.
*
* @since 1.2.3
*/
@Parameter(property = "releaseRebase", defaultValue = "false")
private boolean releaseRebase = false;
/**
* Whether to use <code>--no-ff</code> option when merging.
*
* @since 1.2.3
*/
@Parameter(property = "releaseMergeNoFF", defaultValue = "true")
private boolean releaseMergeNoFF = true;
/**
* Whether to push to the remote.
*
* @since 1.3.0
* @deprecated 1.13.0 {@link #pushRemoteRelease}
*/
@Deprecated
@Parameter(property = "pushRemote", defaultValue = "true")
private boolean pushRemote;
/**
* Whether to push to the remote.
*
* @since 1.13.0
*/
@Parameter(property = "pushRemoteRelease", defaultValue = "true")
private boolean pushRemoteRelease = true;
/**
* Release version to use instead of the default next release version in non
* interactive mode.
*
* @since 1.3.1
*/
@Parameter(property = "releaseVersion", defaultValue = "")
private String releaseVersion = "";
/**
* Whether to use <code>--ff-only</code> option when merging.
*
* @since 1.4.0
*/
@Parameter(property = "releaseMergeFFOnly", defaultValue = "false")
private boolean releaseMergeFFOnly = false;
/**
* Whether to remove qualifiers from the next development version.
*
* @since 1.6.0
*/
@Parameter(property = "digitsOnlyDevVersion", defaultValue = "false")
private boolean digitsOnlyDevVersion = false;
/**
* Development version to use instead of the default next development
* version in non interactive mode.
*
* @since 1.6.0
*/
@Parameter(property = "developmentVersion", defaultValue = "")
private String developmentVersion = "";
/**
* Which digit to increment in the next development version. Starts from
* zero.
*
* @since 1.6.0
*/
@Parameter(property = "versionDigitToIncrement")
private Integer versionDigitToIncrement;
/**
* Maven goals to execute before the release.
*
* @since 1.8.0
*/
@Parameter(property = "preReleaseGoals")
private String preReleaseGoals;
/**
* Maven goals to execute after the release.
*
* @since 1.8.0
*/
@Parameter(property = "postReleaseGoals")
private String postReleaseGoals;
/**
* Whether to make a GPG-signed tag.
*
* @since 1.9.0
*/
@Parameter(property = "gpgSignTag", defaultValue = "false")
private boolean gpgSignTag = false;
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration(preReleaseGoals, postReleaseGoals);
try {
// set git flow configuration
initGitFlowConfig();
// check uncommitted changes
checkUncommittedChanges();
// git for-each-ref --count=1 refs/heads/release/*
final String releaseBranch = gitFindBranches(
gitFlowConfig.getReleaseBranchPrefix(), true);
if (StringUtils.isNotBlank(releaseBranch)) {
throw new MojoFailureException(
"Release branch already exists. Cannot start release.");
}
if (fetchRemote) {
// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getDevelopmentBranch());
// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig.getDevelopmentBranch());
if (notSameProdDevName()) {
// checkout from remote if doesn't exist
gitFetchRemoteAndCreate(gitFlowConfig.getProductionBranch());
// fetch and check remote
gitFetchRemoteAndCompare(gitFlowConfig
.getProductionBranch());
}
}
// need to be in develop to check snapshots and to get correct
// project version
// git checkout develop
gitCheckout(gitFlowConfig.getDevelopmentBranch());
// check snapshots dependencies
if (!allowSnapshots) {
checkSnapshotDependencies();
}
if (!skipTestProject) {
// mvn clean test
mvnCleanTest();
}
// get current project version from pom
final String currentVersion = getCurrentProjectVersion();
String defaultVersion = null;
if (tychoBuild) {
defaultVersion = currentVersion;
} else {
// get default release version
defaultVersion = new GitFlowVersionInfo(currentVersion)
.getReleaseVersionString();
}
if (defaultVersion == null) {
throw new MojoFailureException(
"Cannot get default project version.");
}
String version = null;
if (settings.isInteractiveMode()) {
try {
while (version == null) {
version = prompter.prompt("What is release version? ["
+ defaultVersion + "]");
if (!"".equals(version)
&& (!GitFlowVersionInfo.isValidVersion(version) || !validBranchName(version))) {
getLog().info("The version is not valid.");
version = null;
}
}
} catch (PrompterException e) {
throw new MojoFailureException("release", e);
}
} else {
version = releaseVersion;
}
if (StringUtils.isBlank(version)) {
getLog().info("Version is blank. Using default version.");
version = defaultVersion;
}
// maven goals before release
if (StringUtils.isNotBlank(preReleaseGoals)) {
mvnRun(preReleaseGoals);
}
Map<String, String> messageProperties = new HashMap<String, String>();
messageProperties.put("version", version);
// execute if version changed
if (!version.equals(currentVersion)) {
// mvn set version
mvnSetVersions(version);
// git commit -a -m updating versions for release
gitCommit(commitMessages.getReleaseStartMessage(), messageProperties);
}
if (notSameProdDevName()) {
// git checkout master
gitCheckout(gitFlowConfig.getProductionBranch());
gitMerge(gitFlowConfig.getDevelopmentBranch(), releaseRebase,
releaseMergeNoFF, releaseMergeFFOnly, commitMessages.getReleaseFinishMergeMessage(),
messageProperties);
}
if (!skipTag) {
if (tychoBuild && ArtifactUtils.isSnapshot(version)) {
version = version.replace("-" + Artifact.SNAPSHOT_VERSION, "");
}
messageProperties.put("version", version);
// git tag -a ...
gitTag(gitFlowConfig.getVersionTagPrefix() + version,
commitMessages.getTagReleaseMessage(), gpgSignTag, messageProperties);
}
// maven goals after release
if (StringUtils.isNotBlank(postReleaseGoals)) {
mvnRun(postReleaseGoals);
}
if (notSameProdDevName()) {
// git checkout develop
gitCheckout(gitFlowConfig.getDevelopmentBranch());
}
// get next snapshot version
final String nextSnapshotVersion;
if (!settings.isInteractiveMode()
&& StringUtils.isNotBlank(developmentVersion)) {
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(version);
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}
nextSnapshotVersion = versionInfo
.nextSnapshotVersion(versionDigitToIncrement);
}
if (StringUtils.isBlank(nextSnapshotVersion)) {
throw new MojoFailureException(
"Next snapshot version is blank.");
}
// mvn set version
mvnSetVersions(nextSnapshotVersion);
messageProperties.put("version", nextSnapshotVersion);
// git commit -a -m updating for next development version
gitCommit(commitMessages.getReleaseFinishMessage(), messageProperties);
if (installProject) {
// mvn clean install
mvnCleanInstall();
}
if (pushRemoteRelease) {
gitPush(gitFlowConfig.getProductionBranch(), !skipTag);
if (notSameProdDevName()) {
gitPush(gitFlowConfig.getDevelopmentBranch(), !skipTag);
}
}
} catch (Exception e) {
throw new MojoFailureException("release", e);
}
}
}