2222import java .util .List ;
2323import java .util .Map ;
2424import java .util .Map .Entry ;
25+ import java .util .Properties ;
2526import java .util .TimeZone ;
2627import java .util .regex .Pattern ;
2728
3031import org .apache .maven .execution .MavenSession ;
3132import org .apache .maven .model .Dependency ;
3233import org .apache .maven .plugin .AbstractMojo ;
34+ import org .apache .maven .plugin .MojoExecutionException ;
3335import org .apache .maven .plugin .MojoFailureException ;
3436import org .apache .maven .plugins .annotations .Component ;
3537import org .apache .maven .plugins .annotations .Parameter ;
@@ -91,7 +93,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
9193 */
9294 @ Parameter (defaultValue = "false" )
9395 protected boolean tychoBuild ;
94-
96+
9597 /**
9698 * Whether to call Maven install goal during the mojo execution.
9799 *
@@ -124,6 +126,13 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
124126 @ Parameter (property = "argLine" )
125127 private String argLine ;
126128
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+
127136 /**
128137 * Whether to make a GPG-signed commit.
129138 *
@@ -149,6 +158,70 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
149158 @ Parameter (property = "versionProperty" )
150159 private String versionProperty ;
151160
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+
152225 /**
153226 * Whether to skip updating version. Useful with {@link #versionProperty} to be
154227 * able to update <code>revision</code> property without modifying version tag.
@@ -171,6 +244,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
171244 */
172245 @ Parameter (property = "mvnExecutable" )
173246 private String mvnExecutable ;
247+
174248 /**
175249 * The path to the Git executable. Defaults to "git".
176250 */
@@ -191,7 +265,7 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
191265
192266 @ Component
193267 protected ProjectBuilder projectBuilder ;
194-
268+
195269 /** Default prompter. */
196270 @ Component
197271 protected Prompter prompter ;
@@ -611,7 +685,7 @@ protected boolean gitCheckTagExists(final String tagName) throws MojoFailureExce
611685 * @throws MojoFailureException
612686 * @throws CommandLineException
613687 */
614- protected void gitCheckout (final String branchName )
688+ private void gitCheckout (final String branchName )
615689 throws MojoFailureException , CommandLineException {
616690 getLog ().info ("Checking out '" + branchName + "' branch." );
617691
@@ -628,7 +702,7 @@ protected void gitCheckout(final String branchName)
628702 * @throws MojoFailureException
629703 * @throws CommandLineException
630704 */
631- protected void gitCreateAndCheckout (final String newBranchName ,
705+ private void gitCreateAndCheckout (final String newBranchName ,
632706 final String fromBranchName ) throws MojoFailureException ,
633707 CommandLineException {
634708 getLog ().info (
@@ -1288,5 +1362,193 @@ public String getError() {
12881362
12891363 public void setArgLine (String argLine ) {
12901364 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 .getUserProperties ());
1485+ setPropertyInProperties (key , value , session .getProjectBuildingRequest ().getUserProperties ());
1486+ }
1487+
1488+ argLine = replacePropertyInArgline (key , value , argLineOrig );
1489+ }
1490+
1491+ /**
1492+ * Sets a property in the <code>Properties</code>.
1493+ * Updates the <code>Properties</code> and manipulate the <code>argLine</code> inside
1494+ * of the <code>Properties</code> as well.
1495+ *
1496+ * @param key
1497+ * The key of the property to set.
1498+ * @param value
1499+ * The value of the property to set, if null, the property gets removed.
1500+ * @param properties
1501+ * The properties where to replace the entry.
1502+ */
1503+ private void setPropertyInProperties (final String key , final String value , final Properties properties ) {
1504+ if (StringUtils .isBlank (key ) || properties == null ) {
1505+ return ;
1506+ }
1507+
1508+ final String argLineFromProperty = properties .getProperty ("argLine" );
1509+ final String replaced = replacePropertyInArgline (key , value , argLineFromProperty );
1510+
1511+ if (replaced == null ) {
1512+ properties .remove ("argLine" );
1513+ } else {
1514+ properties .put ("argLine" , replaced );
1515+ }
1516+
1517+ if (value == null ) {
1518+ properties .remove (key );
1519+ } else {
1520+ properties .put (key , value );
1521+ }
1522+ }
1523+
1524+ /**
1525+ * Replaces/sets a property in a argLine-String.
1526+ *
1527+ * @param key
1528+ * The key of the property to set.
1529+ * @param value
1530+ * The value of the property to set, if null, the property gets removed.
1531+ * @param argLine
1532+ * A argLine-representation used to replace the key.
1533+ * @return a new argLine where the property is replaced/set.
1534+ */
1535+ private String replacePropertyInArgline (final String key , final String value , final String argLine ) {
1536+ final String javaProperty = "-D" + key + "=" ;
1537+ final String argLinePropertyRegex = javaProperty + "\\ S*" ;
1538+ final String argLinePropertyReplacement = (value == null ) ? "" : javaProperty + value ;
1539+
1540+ if (StringUtils .isBlank (argLine ) || !argLine .contains (javaProperty )) {
1541+ // noop: old argLine is empty or does not contain the property and no property to set
1542+ if (StringUtils .isBlank (argLinePropertyReplacement )) {
1543+ return argLine ;
1544+ // append: old argLine is empty or does not contain the property and property to set
1545+ } else {
1546+ final String argLineReadyToAppend = StringUtils .isBlank (argLine ) ? "" : argLine + " " ;
1547+ return argLineReadyToAppend + argLinePropertyReplacement ;
1548+ }
1549+ // replace or remove: old argLine contains property, replacement: new or empty
1550+ } else {
1551+ return argLine .replaceAll (argLinePropertyRegex , argLinePropertyReplacement );
1552+ }
12911553 }
12921554}
0 commit comments