Skip to content

Commit 637c6df

Browse files
committed
#235 add projectVersionPolicyId parameter to support VersionPolicy implementations
1 parent 14a282f commit 637c6df

File tree

12 files changed

+113
-25
lines changed

12 files changed

+113
-25
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,12 @@
256256
<version>2.5.3</version>
257257
<scope>compile</scope>
258258
</dependency>
259+
<dependency>
260+
<groupId>org.apache.maven.release</groupId>
261+
<artifactId>maven-release-oddeven-policy</artifactId>
262+
<version>2.5.3</version>
263+
<scope>test</scope>
264+
</dependency>
259265
<dependency>
260266
<groupId>junit</groupId>
261267
<artifactId>junit</artifactId>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.maven.project.ProjectBuilder;
3333
import org.apache.maven.project.ProjectBuildingResult;
3434
import org.apache.maven.settings.Settings;
35+
import org.apache.maven.shared.release.policy.version.VersionPolicy;
3536
import org.codehaus.plexus.components.interactivity.Prompter;
3637
import org.codehaus.plexus.util.StringUtils;
3738
import org.codehaus.plexus.util.cli.CommandLineException;
@@ -170,6 +171,17 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
170171
@Parameter(property = "gitExecutable")
171172
private String gitExecutable;
172173

174+
/**
175+
* The role-hint for the {@link org.apache.maven.shared.release.policy.version.VersionPolicy}
176+
* implementation used to calculate the project versions.
177+
* If a policy is set other parameters controlling the generation of version are ignored
178+
* (digitsOnlyDevVersion, versionDigitToIncrement).
179+
*
180+
* @since 1.15.0
181+
*/
182+
@Parameter(property="projectVersionPolicyId")
183+
private String projectVersionPolicyId;
184+
173185
/** Maven session. */
174186
@Parameter(defaultValue = "${session}", readonly = true)
175187
protected MavenSession mavenSession;
@@ -183,6 +195,8 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
183195
/** Maven settings. */
184196
@Parameter(defaultValue = "${settings}", readonly = true)
185197
protected Settings settings;
198+
@Component
199+
protected Map<String,VersionPolicy> versionPolicies;
186200

187201
/**
188202
* Initializes command line executables.
@@ -1223,4 +1237,16 @@ public String getError() {
12231237
public void setArgLine(String argLine) {
12241238
this.argLine = argLine;
12251239
}
1240+
1241+
protected VersionPolicy getVersionPolicy() {
1242+
if (StringUtils.isNotBlank(projectVersionPolicyId)) {
1243+
VersionPolicy versionPolicy = versionPolicies.get(projectVersionPolicyId);
1244+
if (versionPolicy == null) {
1245+
throw new IllegalArgumentException("No implementation found for projectVersionPolicyId: " + projectVersionPolicyId);
1246+
}
1247+
return versionPolicy;
1248+
}
1249+
return null;
1250+
}
1251+
12261252
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
130130
// get current project version from pom
131131
final String currentVersion = getCurrentProjectVersion();
132132

133-
final String version = new GitFlowVersionInfo(currentVersion)
133+
final String version = new GitFlowVersionInfo(currentVersion, getVersionPolicy())
134134
.featureVersion(featureBranchName);
135135

136136
if (StringUtils.isNotBlank(version)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,12 +272,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
272272
}
273273
} else if (!skipMergeDevBranch) {
274274
GitFlowVersionInfo developVersionInfo = new GitFlowVersionInfo(
275-
currentVersion);
275+
currentVersion, getVersionPolicy());
276276
if (notSameProdDevName()) {
277277
// git checkout develop
278278
gitCheckout(gitFlowConfig.getDevelopmentBranch());
279279

280-
developVersionInfo = new GitFlowVersionInfo(getCurrentProjectVersion());
280+
developVersionInfo = new GitFlowVersionInfo(getCurrentProjectVersion(), getVersionPolicy());
281281

282282
// set version to avoid merge conflict
283283
mvnSetVersions(currentVersion);
@@ -291,7 +291,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
291291

292292
// which version to increment
293293
GitFlowVersionInfo hotfixVersionInfo = new GitFlowVersionInfo(
294-
currentVersion);
294+
currentVersion, getVersionPolicy());
295295
if (developVersionInfo
296296
.compareTo(hotfixVersionInfo) < 0) {
297297
developVersionInfo = hotfixVersionInfo;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
154154
final String currentVersion = getCurrentProjectVersion();
155155

156156
// get default hotfix version
157-
final String defaultVersion = new GitFlowVersionInfo(currentVersion)
157+
final String defaultVersion = new GitFlowVersionInfo(currentVersion, getVersionPolicy())
158158
.hotfixVersion(tychoBuild);
159159

160160
if (defaultVersion == null) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
321321
nextSnapshotVersion = developmentVersion;
322322
} else {
323323
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(
324-
currentVersion);
324+
currentVersion, getVersionPolicy());
325325
if (digitsOnlyDevVersion) {
326326
versionInfo = versionInfo.digitsVersionInfo();
327327
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
208208
defaultVersion = currentVersion;
209209
} else {
210210
// get default release version
211-
defaultVersion = new GitFlowVersionInfo(currentVersion)
211+
defaultVersion = new GitFlowVersionInfo(currentVersion, getVersionPolicy())
212212
.getReleaseVersionString();
213213
}
214214

@@ -296,7 +296,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
296296
&& StringUtils.isNotBlank(developmentVersion)) {
297297
nextSnapshotVersion = developmentVersion;
298298
} else {
299-
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(version);
299+
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(version, getVersionPolicy());
300300
if (digitsOnlyDevVersion) {
301301
versionInfo = versionInfo.digitsVersionInfo();
302302
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private String getNextSnapshotVersion(String currentVersion) throws MojoFailureE
267267
nextSnapshotVersion = developmentVersion;
268268
} else {
269269
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(
270-
currentVersion);
270+
currentVersion, getVersionPolicy());
271271
if (digitsOnlyDevVersion) {
272272
versionInfo = versionInfo.digitsVersionInfo();
273273
}
@@ -292,7 +292,7 @@ private String getReleaseVersion() throws MojoFailureException, VersionParseExce
292292
defaultVersion = currentVersion;
293293
} else {
294294
// get default release version
295-
defaultVersion = new GitFlowVersionInfo(currentVersion)
295+
defaultVersion = new GitFlowVersionInfo(currentVersion, getVersionPolicy())
296296
.getReleaseVersionString();
297297
}
298298

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
import java.util.List;
1919

2020
import org.apache.maven.artifact.Artifact;
21+
import org.apache.maven.shared.release.policy.PolicyException;
22+
import org.apache.maven.shared.release.policy.version.VersionPolicy;
23+
import org.apache.maven.shared.release.policy.version.VersionPolicyRequest;
2124
import org.apache.maven.shared.release.versions.DefaultVersionInfo;
22-
import org.apache.maven.shared.release.versions.VersionInfo;
2325
import org.apache.maven.shared.release.versions.VersionParseException;
2426
import org.codehaus.plexus.util.StringUtils;
2527

@@ -28,10 +30,13 @@
2830
*
2931
*/
3032
public class GitFlowVersionInfo extends DefaultVersionInfo {
33+
34+
private final VersionPolicy versionPolicy;
3135

32-
public GitFlowVersionInfo(final String version)
36+
public GitFlowVersionInfo(final String version, final VersionPolicy versionPolicy)
3337
throws VersionParseException {
3438
super(version);
39+
this.versionPolicy = versionPolicy;
3540
}
3641

3742
/**
@@ -41,7 +46,7 @@ public GitFlowVersionInfo(final String version)
4146
* @throws VersionParseException
4247
*/
4348
public GitFlowVersionInfo digitsVersionInfo() throws VersionParseException {
44-
return new GitFlowVersionInfo(joinDigitString(getDigits()));
49+
return new GitFlowVersionInfo(joinDigitString(getDigits()), versionPolicy);
4550
}
4651

4752
/**
@@ -58,6 +63,21 @@ public static boolean isValidVersion(final String version) {
5863
.matcher(version).matches());
5964
}
6065

66+
@Override
67+
public String getReleaseVersionString() {
68+
if (versionPolicy != null) {
69+
try {
70+
VersionPolicyRequest request = new VersionPolicyRequest().setVersion(this.toString());
71+
return versionPolicy.getReleaseVersion(request).getVersion();
72+
} catch (PolicyException ex) {
73+
throw new RuntimeException("Unable to get release version from policy.", ex);
74+
} catch (VersionParseException ex) {
75+
throw new RuntimeException("Unable to get release version from policy.", ex);
76+
}
77+
}
78+
return super.getReleaseVersionString();
79+
}
80+
6181
/**
6282
* Gets next SNAPSHOT version.
6383
*
@@ -76,6 +96,17 @@ public String nextSnapshotVersion() {
7696
* @return Next SNAPSHOT version.
7797
*/
7898
public String nextSnapshotVersion(final Integer index) {
99+
if (versionPolicy != null) {
100+
try {
101+
VersionPolicyRequest request = new VersionPolicyRequest().setVersion(this.toString());
102+
return versionPolicy.getDevelopmentVersion(request).getVersion();
103+
} catch (PolicyException ex) {
104+
throw new RuntimeException("Unable to get development version from policy.", ex);
105+
} catch (VersionParseException ex) {
106+
throw new RuntimeException("Unable to get development version from policy.", ex);
107+
}
108+
}
109+
79110
List<String> digits = getDigits();
80111

81112
String nextVersion = null;

src/test/java/com/amashchenko/maven/plugin/gitflow/FeatureVersionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public static Collection<Object[]> data() {
5151
@Test
5252
public void testFeatureVersion() throws Exception {
5353
Assert.assertEquals(expectedVersion,
54-
new GitFlowVersionInfo(version).featureVersion(featureName));
54+
new GitFlowVersionInfo(version, null).featureVersion(featureName));
5555
}
5656
}

0 commit comments

Comments
 (0)