22
22
import java .util .List ;
23
23
import java .util .Map ;
24
24
import java .util .Map .Entry ;
25
+ import java .util .Properties ;
25
26
import java .util .TimeZone ;
26
27
import java .util .regex .Pattern ;
27
28
30
31
import org .apache .maven .execution .MavenSession ;
31
32
import org .apache .maven .model .Dependency ;
32
33
import org .apache .maven .plugin .AbstractMojo ;
34
+ import org .apache .maven .plugin .MojoExecutionException ;
33
35
import org .apache .maven .plugin .MojoFailureException ;
34
36
import org .apache .maven .plugins .annotations .Component ;
35
37
import org .apache .maven .plugins .annotations .Parameter ;
@@ -91,7 +93,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
91
93
*/
92
94
@ Parameter (defaultValue = "false" )
93
95
protected boolean tychoBuild ;
94
-
96
+
95
97
/**
96
98
* Whether to call Maven install goal during the mojo execution.
97
99
*
@@ -124,6 +126,12 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
124
126
@ Parameter (property = "argLine" )
125
127
private String argLine ;
126
128
129
+ /**
130
+ * Stores the branch specific maven arguments.
131
+ * Gets set if branch based properties are requested and appended to the maven commands.
132
+ */
133
+ private String mvnArgsBranchSpecific = "" ;
134
+
127
135
/**
128
136
* Whether to make a GPG-signed commit.
129
137
*
@@ -149,6 +157,70 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
149
157
@ Parameter (property = "versionProperty" )
150
158
private String versionProperty ;
151
159
160
+ /**
161
+ * Property to treat as <code>changelist</code> property.
162
+ * Used for Maven CI friendly versioning handling. Only relevant in conjunction
163
+ * with the <code>xxxChangelistValue</code>'s.
164
+ *
165
+ * @since 1.17.0
166
+ */
167
+ @ Parameter (property = "changelistProperty" , defaultValue = "changelist" )
168
+ private String changelistProperty ;
169
+
170
+ /**
171
+ * The value to pass as <code>changelist</code> value when running on the
172
+ * production branch.
173
+ *
174
+ * @since 1.17.0
175
+ */
176
+ @ Parameter (property = "productionChangelistValue" )
177
+ private String productionChangelistValue ;
178
+
179
+ /**
180
+ * The value to pass as <code>changelist</code> value when running on the
181
+ * hotfix branch.
182
+ *
183
+ * @since 1.17.0
184
+ */
185
+ @ Parameter (property = "hotfixChangelistValue" )
186
+ private String hotfixChangelistValue ;
187
+
188
+ /**
189
+ * The value to pass as <code>changelist</code> value when running on the
190
+ * release branch.
191
+ *
192
+ * @since 1.17.0
193
+ */
194
+ @ Parameter (property = "releaseChangelistValue" )
195
+ private String releaseChangelistValue ;
196
+
197
+ /**
198
+ * The value to pass as <code>changelist</code> value when running on the
199
+ * development branch.
200
+ *
201
+ * @since 1.17.0
202
+ */
203
+ @ Parameter (property = "developmentChangelistValue" )
204
+ private String developmentChangelistValue ;
205
+
206
+ /**
207
+ * The value to pass as <code>changelist</code> value when running on the
208
+ * feature branch.
209
+ *
210
+ * @since 1.17.0
211
+ */
212
+ @ Parameter (property = "featureChangelistValue" )
213
+ private String featureChangelistValue ;
214
+
215
+ /**
216
+ * The value to pass as <code>changelist</code> value when running on the
217
+ * support branch.
218
+ *
219
+ * @since 1.17.0
220
+ */
221
+ @ Parameter (property = "supportChangelistValue" )
222
+ private String supportChangelistValue ;
223
+
152
224
/**
153
225
* Whether to skip updating version. Useful with {@link #versionProperty} to be
154
226
* able to update <code>revision</code> property without modifying version tag.
@@ -171,6 +243,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
171
243
*/
172
244
@ Parameter (property = "mvnExecutable" )
173
245
private String mvnExecutable ;
246
+
174
247
/**
175
248
* The path to the Git executable. Defaults to "git".
176
249
*/
@@ -191,7 +264,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
191
264
192
265
@ Component
193
266
protected ProjectBuilder projectBuilder ;
194
-
267
+
195
268
/** Default prompter. */
196
269
@ Component
197
270
protected Prompter prompter ;
@@ -611,7 +684,7 @@ protected boolean gitCheckTagExists(final String tagName) throws MojoFailureExce
611
684
* @throws MojoFailureException
612
685
* @throws CommandLineException
613
686
*/
614
- protected void gitCheckout (final String branchName )
687
+ private void gitCheckout (final String branchName )
615
688
throws MojoFailureException , CommandLineException {
616
689
getLog ().info ("Checking out '" + branchName + "' branch." );
617
690
@@ -628,7 +701,7 @@ protected void gitCheckout(final String branchName)
628
701
* @throws MojoFailureException
629
702
* @throws CommandLineException
630
703
*/
631
- protected void gitCreateAndCheckout (final String newBranchName ,
704
+ private void gitCreateAndCheckout (final String newBranchName ,
632
705
final String fromBranchName ) throws MojoFailureException ,
633
706
CommandLineException {
634
707
getLog ().info (
@@ -1188,7 +1261,8 @@ private void executeGitCommand(final String... args)
1188
1261
*/
1189
1262
private void executeMvnCommand (final String ... args )
1190
1263
throws CommandLineException , MojoFailureException {
1191
- executeCommand (cmdMvn , true , argLine , args );
1264
+ final String argLineWithBranchSpecifics = joinStrings (argLine , mvnArgsBranchSpecific );
1265
+ executeCommand (cmdMvn , true , argLineWithBranchSpecifics , args );
1192
1266
}
1193
1267
1194
1268
/**
@@ -1289,4 +1363,163 @@ public String getError() {
1289
1363
public void setArgLine (String argLine ) {
1290
1364
this .argLine = argLine ;
1291
1365
}
1366
+
1367
+ /**
1368
+ * Executes git checkout and sets Maven CI friendly settings per branch.
1369
+ *
1370
+ * @param branchType
1371
+ * Type of branch to set config for.
1372
+ * @param branchName
1373
+ * Branch name to checkout.
1374
+ * @throws MojoExecutionException an internal error occurred
1375
+ * @throws MojoFailureException an error with the underlying commands occurred
1376
+ * @throws CommandLineException an error with the underlying commands occurred
1377
+ */
1378
+ protected void checkoutAndSetConfigForBranch (final BranchType branchType , final String branchName )
1379
+ throws MojoExecutionException , MojoFailureException , CommandLineException {
1380
+ if (branchType == null ) {
1381
+ throw new MojoExecutionException ("INTERNAL: given BranchType is null" );
1382
+ }
1383
+
1384
+ gitCheckout (branchName );
1385
+ setConfigForBranchType (branchType );
1386
+ }
1387
+
1388
+ /**
1389
+ * Executes git checkout -b and sets Maven CI friendly settings per branch.
1390
+ *
1391
+ * @param branchType
1392
+ * Type of branch to set config for.
1393
+ * @param newBranchName
1394
+ * Create branch with this name.
1395
+ * @param fromBranchName
1396
+ * Create branch from this branch.
1397
+ * @throws MojoExecutionException an internal error occurred
1398
+ * @throws MojoFailureException an error with the underlying commands occurred
1399
+ * @throws CommandLineException an error with the underlying commands occurred
1400
+ */
1401
+ protected void createAndCheckoutAndSetConfigForBranch (final BranchType branchType , final String newBranchName ,
1402
+ final String fromBranchName ) throws MojoExecutionException , MojoFailureException , CommandLineException {
1403
+ if (branchType == null ) {
1404
+ throw new MojoExecutionException ("INTERNAL: given BranchType is null" );
1405
+ }
1406
+
1407
+ gitCreateAndCheckout (newBranchName , fromBranchName );
1408
+ setConfigForBranchType (branchType );
1409
+ }
1410
+
1411
+ /**
1412
+ * Sets Maven CI friendly settings dependent of the type of branch.
1413
+ * This includes settings passed to the maven commands in <code>executeMvnCommand</code> and manipulates
1414
+ * the user properties inside the <code>MavenSession</code>, to guarantee that internal mvn commands
1415
+ * via e.g. <code>ProjectBuilder.build</code> also uses the correct properties.
1416
+ *
1417
+ * @param branchType
1418
+ * Type of branch to set config for.
1419
+ * @throws MojoExecutionException an internal error occurred
1420
+ */
1421
+ protected void setConfigForBranchType (final BranchType branchType ) throws MojoExecutionException {
1422
+ if (branchType == null ) {
1423
+ throw new MojoExecutionException ("INTERNAL: given BranchType is null" );
1424
+ }
1425
+
1426
+ final boolean noChangelistValueToBeModified = productionChangelistValue == null
1427
+ && hotfixChangelistValue == null && releaseChangelistValue == null
1428
+ && developmentChangelistValue == null && featureChangelistValue == null
1429
+ && supportChangelistValue == null ;
1430
+
1431
+ if (StringUtils .isBlank (changelistProperty ) || noChangelistValueToBeModified ) {
1432
+ return ;
1433
+ }
1434
+
1435
+ final String changelistValue ;
1436
+
1437
+ switch (branchType ) {
1438
+ case PRODUCTION :
1439
+ changelistValue = productionChangelistValue ;
1440
+ break ;
1441
+ case HOTFIX :
1442
+ changelistValue = hotfixChangelistValue ;
1443
+ break ;
1444
+ case RELEASE :
1445
+ changelistValue = releaseChangelistValue ;
1446
+ break ;
1447
+ case DEVELOPMENT :
1448
+ changelistValue = developmentChangelistValue ;
1449
+ break ;
1450
+ case FEATURE :
1451
+ changelistValue = featureChangelistValue ;
1452
+ break ;
1453
+ case SUPPORT :
1454
+ changelistValue = supportChangelistValue ;
1455
+ break ;
1456
+ default :
1457
+ throw new MojoExecutionException ("INTERNAL: unhandled case for branchType value: " + branchType );
1458
+ }
1459
+
1460
+ setPropertyInProperties (changelistProperty , changelistValue , mavenSession .getProjectBuildingRequest ().getUserProperties ());
1461
+ mvnArgsBranchSpecific = getJavaPropertyAsArgLineString (changelistProperty , changelistValue );
1462
+ }
1463
+
1464
+ /**
1465
+ * Sets a property in the given <code>Properties</code>.
1466
+ *
1467
+ * @param key
1468
+ * The key of the property to set.
1469
+ * @param value
1470
+ * The value of the property to set, if null, the property gets removed.
1471
+ * @param properties
1472
+ * The properties where to replace the entry.
1473
+ */
1474
+ private void setPropertyInProperties (final String key , final String value , final Properties properties ) {
1475
+ if (StringUtils .isBlank (key ) || properties == null ) {
1476
+ return ;
1477
+ }
1478
+
1479
+ if (value == null ) {
1480
+ properties .remove (key );
1481
+ } else {
1482
+ properties .put (key , value );
1483
+ }
1484
+ }
1485
+
1486
+ /**
1487
+ * Retrieve a string representation of a java property defined by key/value.
1488
+ *
1489
+ * @param key
1490
+ * The key of the property to set.
1491
+ * @param value
1492
+ * The value of the property to set, if null, an empty string is returned.
1493
+ * @return
1494
+ * A string representation to be used as java argument.
1495
+ * Empty if key is null or empty, or the value is null.
1496
+ */
1497
+ private String getJavaPropertyAsArgLineString (final String key , final String value ) {
1498
+ if (StringUtils .isBlank (key ) || value == null ) {
1499
+ return "" ;
1500
+ } else {
1501
+ return "-D" + key + "=" + value ;
1502
+ }
1503
+ }
1504
+
1505
+ /**
1506
+ * Join two <code>String</code>'s with a space.
1507
+ * Both <code>String</code>'s can be null. And always a non-null value is returned.
1508
+ *
1509
+ * @param a
1510
+ * The first string, where the second gets appended to. null is treated as empty.
1511
+ * @param b
1512
+ * The second string, which gets appended to the first one. null is treated as empty.
1513
+ * @return
1514
+ * The combined string, if both strings are null or empty, an empty String is returned.
1515
+ */
1516
+ private String joinStrings (final String a , final String b ) {
1517
+ if (StringUtils .isBlank (a )) {
1518
+ return StringUtils .clean (b );
1519
+ } else if (StringUtils .isBlank (b )) {
1520
+ return StringUtils .clean (a );
1521
+ } else {
1522
+ return a + " " + b ;
1523
+ }
1524
+ }
1292
1525
}
0 commit comments