Skip to content

Commit d04a867

Browse files
committed
Add support for changelist property per branch for maven ci friendly versioning - closes #314
1 parent f1adadc commit d04a867

10 files changed

+304
-29
lines changed

src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java

Lines changed: 265 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Map.Entry;
25+
import java.util.Properties;
2526
import java.util.TimeZone;
2627
import java.util.regex.Pattern;
2728

@@ -30,6 +31,7 @@
3031
import org.apache.maven.execution.MavenSession;
3132
import org.apache.maven.model.Dependency;
3233
import org.apache.maven.plugin.AbstractMojo;
34+
import org.apache.maven.plugin.MojoExecutionException;
3335
import org.apache.maven.plugin.MojoFailureException;
3436
import org.apache.maven.plugins.annotations.Component;
3537
import 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,192 @@ 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.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+
}
12911552
}
12921553
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.amashchenko.maven.plugin.gitflow;
2+
3+
public enum BranchType {
4+
PRODUCTION, DEVELOPMENT, FEATURE, RELEASE, HOTFIX, SUPPORT;
5+
}

src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
157157

158158
if (!skipTestProject) {
159159
// git checkout feature/...
160-
gitCheckout(featureBranchName);
160+
checkoutAndSetConfigForBranch(BranchType.FEATURE, featureBranchName);
161161

162162
// mvn clean test
163163
mvnCleanTest();
@@ -204,7 +204,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
204204
}
205205

206206
// git checkout develop
207-
gitCheckout(gitFlowConfig.getDevelopmentBranch());
207+
checkoutAndSetConfigForBranch(BranchType.DEVELOPMENT, gitFlowConfig.getDevelopmentBranch());
208208

209209
if (featureSquash) {
210210
// git merge --squash feature/...
@@ -229,7 +229,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
229229
}
230230

231231
if (keepBranch) {
232-
gitCheckout(featureBranchName);
232+
checkoutAndSetConfigForBranch(BranchType.FEATURE, featureBranchName);
233233

234234
mvnSetVersions(keptFeatureVersion);
235235

src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ public void execute() throws MojoExecutionException, MojoFailureException {
120120
}
121121

122122
// git checkout -b ... develop
123-
gitCreateAndCheckout(
123+
createAndCheckoutAndSetConfigForBranch(
124+
BranchType.FEATURE,
124125
gitFlowConfig.getFeatureBranchPrefix() + featureBranchName,
125126
gitFlowConfig.getDevelopmentBranch());
126127

0 commit comments

Comments
 (0)