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,167 @@ 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
+ * This includes re-configuring the <code>argLine</code> which is passed to the maven commands in
1415
+ * <code>executeMvnCommand</code> and manipulates the user properties inside of the <code>MavenSession</code>,
1416
+ * to guarantee that internal mvn commands via eg <code>ProjectBuilder.build</code> also uses the correct properties.
1417
+ *
1418
+ * @param branchType
1419
+ * Branch type to set config for.
1420
+ * @throws MojoExecutionException
1421
+ */
1422
+ protected void setConfigForBranchType (final BranchType branchType ) throws MojoExecutionException {
1423
+ if (branchType == null ) {
1424
+ throw new MojoExecutionException ("INTERNAL: given BranchType is null" );
1425
+ }
1426
+
1427
+ final boolean noChangelistValueToBeModified = productionChangelistValue == null
1428
+ && hotfixChangelistValue == null && releaseChangelistValue == null
1429
+ && developmentChangelistValue == null && featureChangelistValue == null
1430
+ && supportChangelistValue == null ;
1431
+
1432
+ if (StringUtils .isBlank (changelistProperty ) || noChangelistValueToBeModified ) {
1433
+ return ;
1434
+ }
1435
+
1436
+ final String changelistValue ;
1437
+
1438
+ switch (branchType ) {
1439
+ case PRODUCTION :
1440
+ changelistValue = productionChangelistValue ;
1441
+ break ;
1442
+ case HOTFIX :
1443
+ changelistValue = hotfixChangelistValue ;
1444
+ break ;
1445
+ case RELEASE :
1446
+ changelistValue = releaseChangelistValue ;
1447
+ break ;
1448
+ case DEVELOPMENT :
1449
+ changelistValue = developmentChangelistValue ;
1450
+ break ;
1451
+ case FEATURE :
1452
+ changelistValue = featureChangelistValue ;
1453
+ break ;
1454
+ case SUPPORT :
1455
+ changelistValue = supportChangelistValue ;
1456
+ break ;
1457
+ default :
1458
+ throw new MojoExecutionException ("INTERNAL: unhandled case for branchType value: " + branchType );
1459
+ }
1460
+
1461
+ setPropertyInProperties (changelistProperty , changelistValue , mavenSession .getProjectBuildingRequest ().getUserProperties ());
1462
+ argLine = replacePropertyInArgline (changelistProperty , changelistValue , argLineOrig );
1463
+ }
1464
+
1465
+ /**
1466
+ * Sets a property in the given <code>Properties</code>.
1467
+ * Updates the <code>Properties</code> and manipulates the <code>argLine</code> inside
1468
+ * of the <code>Properties</code> 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 properties
1475
+ * The properties where to replace the entry.
1476
+ */
1477
+ private void setPropertyInProperties (final String key , final String value , final Properties properties ) {
1478
+ if (StringUtils .isBlank (key ) || properties == null ) {
1479
+ return ;
1480
+ }
1481
+
1482
+ final String argLineFromProperty = properties .getProperty ("argLine" );
1483
+ final String replaced = replacePropertyInArgline (key , value , argLineFromProperty );
1484
+
1485
+ if (replaced == null ) {
1486
+ properties .remove ("argLine" );
1487
+ } else {
1488
+ properties .put ("argLine" , replaced );
1489
+ }
1490
+
1491
+ if (value == null ) {
1492
+ properties .remove (key );
1493
+ } else {
1494
+ properties .put (key , value );
1495
+ }
1496
+ }
1497
+
1498
+ /**
1499
+ * Replaces/sets a property in a argLine-String.
1500
+ *
1501
+ * @param key
1502
+ * The key of the property to set.
1503
+ * @param value
1504
+ * The value of the property to set, if null, the property gets removed.
1505
+ * @param argLine
1506
+ * A argLine-representation used to replace the key.
1507
+ * @return a new argLine where the property is replaced/set.
1508
+ */
1509
+ private String replacePropertyInArgline (final String key , final String value , final String argLine ) {
1510
+ final String javaProperty = "-D" + key + "=" ;
1511
+ final String argLinePropertyRegex = javaProperty + "\\ S*" ;
1512
+ final String argLinePropertyReplacement = (value == null ) ? "" : javaProperty + value ;
1513
+
1514
+ if (StringUtils .isBlank (argLine ) || !argLine .contains (javaProperty )) {
1515
+ // noop: old argLine is empty or does not contain the property and no property to set
1516
+ if (StringUtils .isBlank (argLinePropertyReplacement )) {
1517
+ return argLine ;
1518
+ // append: old argLine is empty or does not contain the property and property to set
1519
+ } else {
1520
+ final String argLineReadyToAppend = StringUtils .isBlank (argLine ) ? "" : argLine + " " ;
1521
+ return argLineReadyToAppend + argLinePropertyReplacement ;
1522
+ }
1523
+ // replace or remove: old argLine contains property, replacement: new or empty
1524
+ } else {
1525
+ return argLine .replaceAll (argLinePropertyRegex , argLinePropertyReplacement );
1526
+ }
1291
1527
}
1292
1528
}
0 commit comments