Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update snapshots with coresponding releases #218

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.amashchenko.maven.plugin.gitflow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -47,6 +48,8 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
private static final String VERSIONS_MAVEN_PLUGIN_SET_GOAL = "org.codehaus.mojo:versions-maven-plugin:set";
/** A full name of the versions-maven-plugin set-property goal. */
private static final String VERSIONS_MAVEN_PLUGIN_SET_PROPERTY_GOAL = "org.codehaus.mojo:versions-maven-plugin:set-property";
/** A full name of the versions-maven-plugin use-releases goal. */
private static final String VERSIONS_MAVEN_PLUGIN_USE_RELEASES_GOAL = "org.codehaus.mojo:versions-maven-plugin:use-releases";
/** Name of the tycho-versions-plugin set-version goal. */
private static final String TYCHO_VERSIONS_PLUGIN_SET_GOAL = "org.eclipse.tycho:tycho-versions-plugin:set-version";

Expand Down Expand Up @@ -287,6 +290,10 @@ protected void checkUncommittedChanges() throws MojoFailureException,
}
}

protected boolean hasUncommittedChanges() throws CommandLineException, MojoFailureException {
return executeGitHasUncommitted();
}

protected void checkSnapshotDependencies() throws MojoFailureException {
getLog().info("Checking for SNAPSHOT versions in dependencies.");

Expand Down Expand Up @@ -317,6 +324,19 @@ protected void checkSnapshotDependencies() throws MojoFailureException {
}
}

protected void checkSnapshotDependenciesWithoutCorrespondingReleases()
throws MojoFailureException, CommandLineException {

mvnUseReleases();
try {
checkSnapshotDependencies();
} catch (MojoFailureException exception) {
gitReset();
throw exception;
}
gitReset();
}

/**
* Checks if branch name is acceptable.
*
Expand Down Expand Up @@ -738,6 +758,19 @@ protected void gitMergeNoff(final String branchName, final String message,
gitMerge(branchName, false, true, false, message, messageProperties);
}

/**
* Executes git merge --ff-only.
*
* @param branchName
* Branch name to merge.
* @throws MojoFailureException
* @throws CommandLineException
*/
protected void gitMergeFfOnly(final String branchName)
throws MojoFailureException, CommandLineException {
gitMerge(branchName, false, false, true, "", new HashMap<String, String>());
}

/**
* Executes git merge --squash.
*
Expand Down Expand Up @@ -781,6 +814,19 @@ protected void gitTag(final String tagName, String message, boolean gpgSignTag,
}
}

/**
* Executes git reset --hard
*
* @throws MojoFailureException
* @throws CommandLineException
*/
protected void gitReset()
throws MojoFailureException, CommandLineException {
getLog().info("Resetting uncommited changes.");

executeGitCommand("reset", "--hard");
}

/**
* Executes git branch -d.
*
Expand Down Expand Up @@ -1016,6 +1062,17 @@ protected void mvnSetVersions(final String version) throws MojoFailureException,
}
}

/**
* Executes 'use-releases' goal of versions-maven-plugin.
*
* @throws MojoFailureException
* @throws CommandLineException
*/
protected void mvnUseReleases() throws MojoFailureException, CommandLineException {
getLog().info("Searching the pom for all -SNAPSHOT versions which have been released and replacing them with the corresponding release version.");
executeMvnCommand(VERSIONS_MAVEN_PLUGIN_USE_RELEASES_GOAL, "-DgenerateBackupPoms=false");
}

/**
* Executes mvn clean test.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class CommitMessages {
private String updateReleaseToAvoidConflictsMessage;
private String updateReleaseBackPreMergeStateMessage;

private String replaceSnapshotDependenciesMessage;

public CommitMessages() {
featureStartMessage = "Update versions for feature branch";
featureFinishMessage = "Update versions for development branch";
Expand All @@ -77,6 +79,8 @@ public CommitMessages() {

updateReleaseToAvoidConflictsMessage = "Update release to hotfix version to avoid merge conflicts";
updateReleaseBackPreMergeStateMessage = "Update release version back to pre-merge state";

replaceSnapshotDependenciesMessage = "Replace all released -SNAPSHOT dependencies with the corresponding release versions";
}

/**
Expand Down Expand Up @@ -387,4 +391,19 @@ public String getFeatureFinishDevMergeMessage() {
public void setFeatureFinishDevMergeMessage(String featureFinishDevMergeMessage) {
this.featureFinishDevMergeMessage = featureFinishDevMergeMessage;
}

/**
* @return the replaceSnapshotDependenciesMessage
*/
public String getReplaceSnapshotDependenciesMessage() {
return replaceSnapshotDependenciesMessage;
}

/**
* @param replaceSnapshotDependenciesMessage
* the replaceSnapshotDependenciesMessage to set
*/
public void setReplaceSnapshotDependenciesMessage(String replaceSnapshotDependenciesMessage) {
this.replaceSnapshotDependenciesMessage = replaceSnapshotDependenciesMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public class GitFlowReleaseMojo extends AbstractGitFlowMojo {
@Parameter(property = "allowSnapshots", defaultValue = "false")
private boolean allowSnapshots = false;

/**
* Whether to replace all released -SNAPSHOT dependencies with the corresponding release versions
*
* @since 1.15.0
*/
@Parameter(property = "updateSnapshotDependencies", defaultValue = "false")
private boolean updateSnapshotDependencies = false;

/**
* Whether to rebase branch or merge. If <code>true</code> then rebase will
* be performed.
Expand Down Expand Up @@ -192,7 +200,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// check snapshots dependencies
if (!allowSnapshots) {
checkSnapshotDependencies();
if (updateSnapshotDependencies) {
checkSnapshotDependenciesWithoutCorrespondingReleases();
} else {
checkSnapshotDependencies();
}
}

if (!skipTestProject) {
Expand Down Expand Up @@ -259,6 +271,14 @@ public void execute() throws MojoExecutionException, MojoFailureException {
gitCommit(commitMessages.getReleaseStartMessage(), messageProperties);
}

if (updateSnapshotDependencies) {
// mvn versions:use-releases ...
mvnUseReleases();
if (hasUncommittedChanges()) {
gitCommit(commitMessages.getReplaceSnapshotDependenciesMessage());
}
}

if (notSameProdDevName()) {
// git checkout master
gitCheckout(gitFlowConfig.getProductionBranch());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public class GitFlowReleaseStartMojo extends AbstractGitFlowMojo {
@Parameter(property = "allowSnapshots", defaultValue = "false")
private boolean allowSnapshots = false;

/**
* Whether to replace all released -SNAPSHOT dependencies with the corresponding release versions
*
* @since 1.15.0
*/
@Parameter(property = "updateSnapshotDependencies", defaultValue = "false")
private boolean updateSnapshotDependencies = false;

/**
* Release version to use instead of the default next release version in non
* interactive mode.
Expand Down Expand Up @@ -181,7 +189,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {

// check snapshots dependencies
if (!allowSnapshots) {
checkSnapshotDependencies();
if (updateSnapshotDependencies) {
checkSnapshotDependenciesWithoutCorrespondingReleases();
} else {
checkSnapshotDependencies();
}
}

if (commitDevelopmentVersionAtStart && !notSameProdDevName()) {
Expand Down Expand Up @@ -212,32 +224,32 @@ public void execute() throws MojoExecutionException, MojoFailureException {
+ " It is better to define it in the project's pom file.");
}

// git checkout -b release/... develop
gitCreateAndCheckout(fullBranchName, startPoint);

// mvn versions:set ...
// git commit -a -m ...
commitProjectVersion(projectVersion, commitMessages.getReleaseStartMessage());

if (updateSnapshotDependencies) {
// mvn versions:use-releases ...
mvnUseReleases();
if (hasUncommittedChanges()) {
gitCommit(commitMessages.getReplaceSnapshotDependenciesMessage());
}
}

if (commitDevelopmentVersionAtStart) {
// mvn versions:set ...
// git commit -a -m ...
commitProjectVersion(projectVersion,
commitMessages.getReleaseStartMessage());
gitCheckout(startPoint);

// git branch release/... develop
gitCreateBranch(fullBranchName, startPoint);
gitMergeFfOnly(fullBranchName);

final String nextSnapshotVersion =
getNextSnapshotVersion(releaseVersion);

// mvn versions:set ...
// git commit -a -m ...
commitProjectVersion(nextSnapshotVersion, commitMessages.getReleaseVersionUpdateMessage());

// git checkout release/...
gitCheckout(fullBranchName);
} else {
// git checkout -b release/... develop
gitCreateAndCheckout(fullBranchName, startPoint);

// mvn versions:set ...
// git commit -a -m ...
commitProjectVersion(projectVersion,
commitMessages.getReleaseStartMessage());
}

if (installProject) {
Expand Down