Skip to content

Commit

Permalink
Merge pull request #236 from stefanseifert/feature/issue235-version-p…
Browse files Browse the repository at this point in the history
…olicy

Fixes #235 add projectVersionPolicyId parameter to support VersionPolicy implementations
  • Loading branch information
aleksandr-m authored Jan 7, 2022
2 parents f3492cf + 4d26f89 commit c0b13b5
Show file tree
Hide file tree
Showing 20 changed files with 231 additions and 28 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@
<version>2.5.3</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.release</groupId>
<artifactId>maven-release-oddeven-policy</artifactId>
<version>2.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
27 changes: 27 additions & 0 deletions src/it/release-start-it-version-policy/expected-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.amashchenko.maven.plugin</groupId>
<artifactId>gitflow-maven-test</artifactId>
<packaging>pom</packaging>
<version>0.0.4</version>

<build>
<plugins>
<plugin>
<groupId>com.amashchenko.maven.plugin</groupId>
<artifactId>gitflow-maven-plugin</artifactId>
<configuration>
<projectVersionPolicyId>OddEvenVersionPolicy</projectVersionPolicyId>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.release</groupId>
<artifactId>maven-release-oddeven-policy</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
5 changes: 5 additions & 0 deletions src/it/release-start-it-version-policy/gitignorefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
build.log
expected-pom.xml
invoker.properties
init.bsh
verify.bsh
25 changes: 25 additions & 0 deletions src/it/release-start-it-version-policy/init.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
try {
new File(basedir, "gitignorefile").renameTo(new File(basedir, ".gitignore"));

Process p = Runtime.getRuntime().exec("git --git-dir=" + basedir + "/.git --work-tree=" + basedir + " init");
p.waitFor();

Process p = Runtime.getRuntime().exec("git --git-dir=" + basedir + "/.git --work-tree=" + basedir + " config user.email 'a@a.aa'");
p.waitFor();
Process p = Runtime.getRuntime().exec("git --git-dir=" + basedir + "/.git --work-tree=" + basedir + " config user.name 'a'");
p.waitFor();

p = Runtime.getRuntime().exec("git --git-dir=" + basedir + "/.git --work-tree=" + basedir + " add .");
p.waitFor();

p = Runtime.getRuntime().exec("git --git-dir=" + basedir + "/.git --work-tree=" + basedir + " commit -m init");
p.waitFor();

p = Runtime.getRuntime().exec("git --git-dir=" + basedir + "/.git --work-tree=" + basedir + " checkout -b develop");
p.waitFor();

} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
3 changes: 3 additions & 0 deletions src/it/release-start-it-version-policy/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invoker.goals=${project.groupId}:${project.artifactId}:${project.version}:release-start -B

invoker.description=Non-interactive simple release-start.
27 changes: 27 additions & 0 deletions src/it/release-start-it-version-policy/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.amashchenko.maven.plugin</groupId>
<artifactId>gitflow-maven-test</artifactId>
<packaging>pom</packaging>
<version>0.0.3-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>com.amashchenko.maven.plugin</groupId>
<artifactId>gitflow-maven-plugin</artifactId>
<configuration>
<projectVersionPolicyId>OddEvenVersionPolicy</projectVersionPolicyId>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.release</groupId>
<artifactId>maven-release-oddeven-policy</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

</project>
27 changes: 27 additions & 0 deletions src/it/release-start-it-version-policy/verify.bsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import org.codehaus.plexus.util.FileUtils;

try {
File gitRef = new File(basedir, ".git/refs/heads/release/0.0.4");
if (!gitRef.exists()) {
System.out.println("release-start .git/refs/heads/release/0.0.4 doesn't exist");
return false;
}

File file = new File(basedir, "pom.xml");
File expectedFile = new File(basedir, "expected-pom.xml");

String actual = FileUtils.fileRead(file, "UTF-8");
String expected = FileUtils.fileRead(expectedFile, "UTF-8");

actual = actual.replaceAll("\\r?\\n", "");
expected = expected.replaceAll("\\r?\\n", "");

if (!expected.equals(actual)) {
System.out.println("release-start expected: " + expected + " actual was:" + actual);
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingResult;
import org.apache.maven.settings.Settings;
import org.apache.maven.shared.release.policy.version.VersionPolicy;
import org.codehaus.plexus.components.interactivity.Prompter;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;
Expand Down Expand Up @@ -195,6 +196,17 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
@Parameter(property = "gitExecutable")
private String gitExecutable;

/**
* The role-hint for the {@link org.apache.maven.shared.release.policy.version.VersionPolicy}
* implementation used to calculate the project versions.
* If a policy is set other parameters controlling the generation of version are ignored
* (digitsOnlyDevVersion, versionDigitToIncrement).
*
* @since 1.18.0
*/
@Parameter(property="projectVersionPolicyId")
private String projectVersionPolicyId;

/** Maven session. */
@Parameter(defaultValue = "${session}", readonly = true)
protected MavenSession mavenSession;
Expand All @@ -208,6 +220,8 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
/** Maven settings. */
@Parameter(defaultValue = "${settings}", readonly = true)
protected Settings settings;
@Component
protected Map<String,VersionPolicy> versionPolicies;

/**
* Initializes command line executables.
Expand Down Expand Up @@ -1380,4 +1394,16 @@ public String getError() {
public void setArgLine(String argLine) {
this.argLine = argLine;
}

protected VersionPolicy getVersionPolicy() {
if (StringUtils.isNotBlank(projectVersionPolicyId)) {
VersionPolicy versionPolicy = versionPolicies.get(projectVersionPolicyId);
if (versionPolicy == null) {
throw new IllegalArgumentException("No implementation found for projectVersionPolicyId: " + projectVersionPolicyId);
}
return versionPolicy;
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ public void execute() throws MojoExecutionException, MojoFailureException {
if (incrementVersionAtFinish) {
// prevent incrementing feature name which can hold numbers
String ver = featureVersion.replaceFirst("-" + featName, "");
GitFlowVersionInfo nextVersionInfo = new GitFlowVersionInfo(ver);
GitFlowVersionInfo nextVersionInfo = new GitFlowVersionInfo(ver, getVersionPolicy());
ver = nextVersionInfo.nextSnapshotVersion();
GitFlowVersionInfo featureVersionInfo = new GitFlowVersionInfo(ver);
GitFlowVersionInfo featureVersionInfo = new GitFlowVersionInfo(ver, getVersionPolicy());
featureVersion = featureVersionInfo.featureVersion(featName);

mvnSetVersions(featureVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
// get current project version from pom
final String currentVersion = getCurrentProjectVersion();

final String version = new GitFlowVersionInfo(currentVersion)
final String version = new GitFlowVersionInfo(currentVersion, getVersionPolicy())
.featureVersion(featureBranchName);

if (StringUtils.isNotBlank(version)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,12 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
} else if (!skipMergeDevBranch) {
GitFlowVersionInfo developVersionInfo = new GitFlowVersionInfo(
currentVersion);
currentVersion, getVersionPolicy());
if (notSameProdDevName()) {
// git checkout develop
gitCheckout(gitFlowConfig.getDevelopmentBranch());

developVersionInfo = new GitFlowVersionInfo(getCurrentProjectVersion());
developVersionInfo = new GitFlowVersionInfo(getCurrentProjectVersion(), getVersionPolicy());

// set version to avoid merge conflict
mvnSetVersions(currentVersion);
Expand All @@ -307,7 +307,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// which version to increment
GitFlowVersionInfo hotfixVersionInfo = new GitFlowVersionInfo(
currentVersion);
currentVersion, getVersionPolicy());
if (developVersionInfo
.compareTo(hotfixVersionInfo) < 0) {
developVersionInfo = hotfixVersionInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
final String currentVersion = getCurrentProjectVersion();

// get default hotfix version
final String defaultVersion = new GitFlowVersionInfo(currentVersion).hotfixVersion(tychoBuild, hotfixVersionDigitToIncrement);
final String defaultVersion = new GitFlowVersionInfo(currentVersion, getVersionPolicy()).hotfixVersion(tychoBuild, hotfixVersionDigitToIncrement);

if (defaultVersion == null) {
throw new MojoFailureException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(
currentVersion);
currentVersion, getVersionPolicy());
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
defaultVersion = currentVersion;
} else {
// get default release version
defaultVersion = new GitFlowVersionInfo(currentVersion)
defaultVersion = new GitFlowVersionInfo(currentVersion, getVersionPolicy())
.getReleaseVersionString();
}

Expand Down Expand Up @@ -304,7 +304,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
&& StringUtils.isNotBlank(developmentVersion)) {
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(version);
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(version, getVersionPolicy());
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ private String getNextSnapshotVersion(String currentVersion) throws MojoFailureE
nextSnapshotVersion = developmentVersion;
} else {
GitFlowVersionInfo versionInfo = new GitFlowVersionInfo(
currentVersion);
currentVersion, getVersionPolicy());
if (digitsOnlyDevVersion) {
versionInfo = versionInfo.digitsVersionInfo();
}
Expand All @@ -292,7 +292,7 @@ private String getReleaseVersion() throws MojoFailureException, VersionParseExce
defaultVersion = currentVersion;
} else {
// get default release version
defaultVersion = new GitFlowVersionInfo(currentVersion)
defaultVersion = new GitFlowVersionInfo(currentVersion, getVersionPolicy())
.getReleaseVersionString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.shared.release.policy.PolicyException;
import org.apache.maven.shared.release.policy.version.VersionPolicy;
import org.apache.maven.shared.release.policy.version.VersionPolicyRequest;
import org.apache.maven.shared.release.versions.DefaultVersionInfo;
import org.apache.maven.shared.release.versions.VersionInfo;
import org.apache.maven.shared.release.versions.VersionParseException;
import org.codehaus.plexus.util.StringUtils;

Expand All @@ -28,10 +30,13 @@
*
*/
public class GitFlowVersionInfo extends DefaultVersionInfo {

private final VersionPolicy versionPolicy;

public GitFlowVersionInfo(final String version)
public GitFlowVersionInfo(final String version, final VersionPolicy versionPolicy)
throws VersionParseException {
super(version);
this.versionPolicy = versionPolicy;
}

/**
Expand All @@ -42,7 +47,7 @@ public GitFlowVersionInfo(final String version)
* If version parsing fails.
*/
public GitFlowVersionInfo digitsVersionInfo() throws VersionParseException {
return new GitFlowVersionInfo(joinDigitString(getDigits()));
return new GitFlowVersionInfo(joinDigitString(getDigits()), versionPolicy);
}

/**
Expand All @@ -59,6 +64,19 @@ public static boolean isValidVersion(final String version) {
.matcher(version).matches());
}

@Override
public String getReleaseVersionString() {
if (versionPolicy != null) {
try {
VersionPolicyRequest request = new VersionPolicyRequest().setVersion(this.toString());
return versionPolicy.getReleaseVersion(request).getVersion();
} catch (PolicyException | VersionParseException ex) {
throw new RuntimeException("Unable to get release version from policy.", ex);
}
}
return super.getReleaseVersionString();
}

/**
* Gets next SNAPSHOT version.
*
Expand Down Expand Up @@ -90,6 +108,20 @@ public String nextSnapshotVersion(final Integer index) {
* @return Next version.
*/
private String nextVersion(final Integer index, boolean snapshot) {
if (versionPolicy != null) {
try {
VersionPolicyRequest request = new VersionPolicyRequest().setVersion(this.toString());
if (snapshot) {
return versionPolicy.getDevelopmentVersion(request).getVersion();
}
else {
return versionPolicy.getReleaseVersion(request).getVersion();
}
} catch (PolicyException | VersionParseException ex) {
throw new RuntimeException("Unable to get development version from policy.", ex);
}
}

List<String> digits = getDigits();

String nextVersion = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ public static Collection<Object[]> data() {
@Test
public void testFeatureVersion() throws Exception {
Assert.assertEquals(expectedVersion,
new GitFlowVersionInfo(version).featureVersion(featureName));
new GitFlowVersionInfo(version, null).featureVersion(featureName));
}
}
Loading

0 comments on commit c0b13b5

Please sign in to comment.