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,13 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
124
126
@ Parameter (property = "argLine" )
125
127
private String argLine ;
126
128
129
+ /**
130
+ * Stores the original argLine.
131
+ * If branch based properties are needed, this will be used as reference
132
+ * for the argLine manipulation.
133
+ */
134
+ private String argLineOrig ;
135
+
127
136
/**
128
137
* Whether to make a GPG-signed commit.
129
138
*
@@ -149,6 +158,70 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
149
158
@ Parameter (property = "versionProperty" )
150
159
private String versionProperty ;
151
160
161
+ /**
162
+ * Property to treat as <code>changelist</code> property.
163
+ * Used for Maven CI friendly versioning handling. Only relevant in conjunction
164
+ * with the <code>xxxChangelistValue</code>'s.
165
+ *
166
+ * @since 1.17.0
167
+ */
168
+ @ Parameter (property = "changelistProperty" , defaultValue = "changelist" )
169
+ private String changelistProperty ;
170
+
171
+ /**
172
+ * The value to pass as <code>changelist</code> value when running on the
173
+ * production branch.
174
+ *
175
+ * @since 1.17.0
176
+ */
177
+ @ Parameter (property = "productionChangelistValue" )
178
+ private String productionChangelistValue ;
179
+
180
+ /**
181
+ * The value to pass as <code>changelist</code> value when running on the
182
+ * hotfix branch.
183
+ *
184
+ * @since 1.17.0
185
+ */
186
+ @ Parameter (property = "hotfixChangelistValue" )
187
+ private String hotfixChangelistValue ;
188
+
189
+ /**
190
+ * The value to pass as <code>changelist</code> value when running on the
191
+ * release branch.
192
+ *
193
+ * @since 1.17.0
194
+ */
195
+ @ Parameter (property = "releaseChangelistValue" )
196
+ private String releaseChangelistValue ;
197
+
198
+ /**
199
+ * The value to pass as <code>changelist</code> value when running on the
200
+ * development branch.
201
+ *
202
+ * @since 1.17.0
203
+ */
204
+ @ Parameter (property = "developmentChangelistValue" )
205
+ private String developmentChangelistValue ;
206
+
207
+ /**
208
+ * The value to pass as <code>changelist</code> value when running on the
209
+ * feature branch.
210
+ *
211
+ * @since 1.17.0
212
+ */
213
+ @ Parameter (property = "featureChangelistValue" )
214
+ private String featureChangelistValue ;
215
+
216
+ /**
217
+ * The value to pass as <code>changelist</code> value when running on the
218
+ * support branch.
219
+ *
220
+ * @since 1.17.0
221
+ */
222
+ @ Parameter (property = "supportChangelistValue" )
223
+ private String supportChangelistValue ;
224
+
152
225
/**
153
226
* Whether to skip updating version. Useful with {@link #versionProperty} to be
154
227
* able to update <code>revision</code> property without modifying version tag.
@@ -171,6 +244,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
171
244
*/
172
245
@ Parameter (property = "mvnExecutable" )
173
246
private String mvnExecutable ;
247
+
174
248
/**
175
249
* The path to the Git executable. Defaults to "git".
176
250
*/
@@ -191,7 +265,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
191
265
192
266
@ Component
193
267
protected ProjectBuilder projectBuilder ;
194
-
268
+
195
269
/** Default prompter. */
196
270
@ Component
197
271
protected Prompter prompter ;
@@ -611,7 +685,7 @@ protected boolean gitCheckTagExists(final String tagName) throws MojoFailureExce
611
685
* @throws MojoFailureException
612
686
* @throws CommandLineException
613
687
*/
614
- protected void gitCheckout (final String branchName )
688
+ private void gitCheckout (final String branchName )
615
689
throws MojoFailureException , CommandLineException {
616
690
getLog ().info ("Checking out '" + branchName + "' branch." );
617
691
@@ -628,7 +702,7 @@ protected void gitCheckout(final String branchName)
628
702
* @throws MojoFailureException
629
703
* @throws CommandLineException
630
704
*/
631
- protected void gitCreateAndCheckout (final String newBranchName ,
705
+ private void gitCreateAndCheckout (final String newBranchName ,
632
706
final String fromBranchName ) throws MojoFailureException ,
633
707
CommandLineException {
634
708
getLog ().info (
@@ -1288,5 +1362,192 @@ public String getError() {
1288
1362
1289
1363
public void setArgLine (String argLine ) {
1290
1364
this .argLine = argLine ;
1365
+ this .argLineOrig = argLine ;
1366
+ }
1367
+
1368
+ /**
1369
+ * Executes git checkout and sets Maven CI friendly settings per branch.
1370
+ *
1371
+ * @param branchType
1372
+ * Branch type to set config for.
1373
+ * @param branchName
1374
+ * Branch name to checkout.
1375
+ * @throws MojoExecutionException
1376
+ * @throws MojoFailureException
1377
+ * @throws CommandLineException
1378
+ */
1379
+ protected void checkoutAndSetConfigForBranch (final BranchType branchType , final String branchName )
1380
+ throws MojoExecutionException , MojoFailureException , CommandLineException {
1381
+ if (branchType == null ) {
1382
+ throw new MojoExecutionException ("INTERNAL: given BranchType is null" );
1383
+ }
1384
+
1385
+ gitCheckout (branchName );
1386
+ setConfigForBranchType (branchType );
1387
+ }
1388
+
1389
+ /**
1390
+ * Executes git checkout -b and sets Maven CI friendly settings per branch.
1391
+ *
1392
+ * @param branchType
1393
+ * Branch type to set config for.
1394
+ * @param newBranchName
1395
+ * Create branch with this name.
1396
+ * @param fromBranchName
1397
+ * Create branch from this branch.
1398
+ * @throws MojoExecutionException
1399
+ * @throws MojoFailureException
1400
+ * @throws CommandLineException
1401
+ */
1402
+ protected void createAndCheckoutAndSetConfigForBranch (final BranchType branchType , final String newBranchName ,
1403
+ final String fromBranchName ) throws MojoExecutionException , MojoFailureException , CommandLineException {
1404
+ if (branchType == null ) {
1405
+ throw new MojoExecutionException ("INTERNAL: given BranchType is null" );
1406
+ }
1407
+
1408
+ gitCreateAndCheckout (newBranchName , fromBranchName );
1409
+ setConfigForBranchType (branchType );
1410
+ }
1411
+
1412
+ /**
1413
+ * Sets Maven CI friendly settings dependent of the type of branch.
1414
+ *
1415
+ * @param branchType
1416
+ * Branch type to set config for.
1417
+ * @throws MojoExecutionException
1418
+ */
1419
+ protected void setConfigForBranchType (final BranchType branchType ) throws MojoExecutionException {
1420
+ if (branchType == null ) {
1421
+ throw new MojoExecutionException ("INTERNAL: given BranchType is null" );
1422
+ }
1423
+
1424
+ final boolean noChangelistValueToBeModified = productionChangelistValue == null
1425
+ && hotfixChangelistValue == null && releaseChangelistValue == null
1426
+ && developmentChangelistValue == null && featureChangelistValue == null
1427
+ && supportChangelistValue == null ;
1428
+
1429
+ if (StringUtils .isBlank (changelistProperty ) || noChangelistValueToBeModified ) {
1430
+ return ;
1431
+ }
1432
+
1433
+ switch (branchType ) {
1434
+ case PRODUCTION :
1435
+ setChangelistPropertyToValue (productionChangelistValue );
1436
+ break ;
1437
+ case HOTFIX :
1438
+ setChangelistPropertyToValue (hotfixChangelistValue );
1439
+ break ;
1440
+ case RELEASE :
1441
+ setChangelistPropertyToValue (releaseChangelistValue );
1442
+ break ;
1443
+ case DEVELOPMENT :
1444
+ setChangelistPropertyToValue (developmentChangelistValue );
1445
+ break ;
1446
+ case FEATURE :
1447
+ setChangelistPropertyToValue (featureChangelistValue );
1448
+ break ;
1449
+ case SUPPORT :
1450
+ setChangelistPropertyToValue (supportChangelistValue );
1451
+ break ;
1452
+ }
1453
+ }
1454
+
1455
+ /**
1456
+ * Sets the <code>changelist</code> property globally.
1457
+ *
1458
+ * @param changelistPropertyValue
1459
+ * The value to set the <code>changelist</code> property to.
1460
+ * Remove the property if the value is null.
1461
+ */
1462
+ private void setChangelistPropertyToValue (final String changelistPropertyValue ) {
1463
+ setProperty (changelistProperty , changelistPropertyValue , mavenSession );
1464
+ }
1465
+
1466
+ /**
1467
+ * Sets a property globally in system-properties, optionally in the properties of the
1468
+ * <code>MavenSession</code> and replaces the argLine to contain the value as well.
1469
+ *
1470
+ * @param key
1471
+ * The key of the property to set.
1472
+ * @param value
1473
+ * The value of the property to set, if null, the property gets removed.
1474
+ * @param session
1475
+ * An optional <code>MavenSession</code> can be passed, to replace the properties
1476
+ * in it's property-lists.
1477
+ */
1478
+ private void setProperty (final String key , final String value , final MavenSession session ) {
1479
+ if (StringUtils .isBlank (key )) {
1480
+ return ;
1481
+ }
1482
+
1483
+ if (session != null ) {
1484
+ setPropertyInProperties (key , value , session .getProjectBuildingRequest ().getUserProperties ());
1485
+ }
1486
+
1487
+ argLine = replacePropertyInArgline (key , value , argLineOrig );
1488
+ }
1489
+
1490
+ /**
1491
+ * Sets a property in the <code>Properties</code>.
1492
+ * Updates the <code>Properties</code> and manipulate the <code>argLine</code> inside
1493
+ * of the <code>Properties</code> as well.
1494
+ *
1495
+ * @param key
1496
+ * The key of the property to set.
1497
+ * @param value
1498
+ * The value of the property to set, if null, the property gets removed.
1499
+ * @param properties
1500
+ * The properties where to replace the entry.
1501
+ */
1502
+ private void setPropertyInProperties (final String key , final String value , final Properties properties ) {
1503
+ if (StringUtils .isBlank (key ) || properties == null ) {
1504
+ return ;
1505
+ }
1506
+
1507
+ final String argLineFromProperty = properties .getProperty ("argLine" );
1508
+ final String replaced = replacePropertyInArgline (key , value , argLineFromProperty );
1509
+
1510
+ if (replaced == null ) {
1511
+ properties .remove ("argLine" );
1512
+ } else {
1513
+ properties .put ("argLine" , replaced );
1514
+ }
1515
+
1516
+ if (value == null ) {
1517
+ properties .remove (key );
1518
+ } else {
1519
+ properties .put (key , value );
1520
+ }
1521
+ }
1522
+
1523
+ /**
1524
+ * Replaces/sets a property in a argLine-String.
1525
+ *
1526
+ * @param key
1527
+ * The key of the property to set.
1528
+ * @param value
1529
+ * The value of the property to set, if null, the property gets removed.
1530
+ * @param argLine
1531
+ * A argLine-representation used to replace the key.
1532
+ * @return a new argLine where the property is replaced/set.
1533
+ */
1534
+ private String replacePropertyInArgline (final String key , final String value , final String argLine ) {
1535
+ final String javaProperty = "-D" + key + "=" ;
1536
+ final String argLinePropertyRegex = javaProperty + "\\ S*" ;
1537
+ final String argLinePropertyReplacement = (value == null ) ? "" : javaProperty + value ;
1538
+
1539
+ if (StringUtils .isBlank (argLine ) || !argLine .contains (javaProperty )) {
1540
+ // noop: old argLine is empty or does not contain the property and no property to set
1541
+ if (StringUtils .isBlank (argLinePropertyReplacement )) {
1542
+ return argLine ;
1543
+ // append: old argLine is empty or does not contain the property and property to set
1544
+ } else {
1545
+ final String argLineReadyToAppend = StringUtils .isBlank (argLine ) ? "" : argLine + " " ;
1546
+ return argLineReadyToAppend + argLinePropertyReplacement ;
1547
+ }
1548
+ // replace or remove: old argLine contains property, replacement: new or empty
1549
+ } else {
1550
+ return argLine .replaceAll (argLinePropertyRegex , argLinePropertyReplacement );
1551
+ }
1291
1552
}
1292
1553
}
0 commit comments