Skip to content

Commit

Permalink
add ability to run custom maven goals before and after release and ho…
Browse files Browse the repository at this point in the history
…tfix - closes 13, 29, 54
  • Loading branch information
aleksandr-m committed Oct 9, 2017
1 parent 913a676 commit 9e57bb0
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,27 @@ private void initExecutables() {
* Validates plugin configuration. Throws exception if configuration is not
* valid.
*
* @param params
* Configuration parameters to validate.
* @throws MojoFailureException
* If configuration is not valid.
*/
protected void validateConfiguration() throws MojoFailureException {
protected void validateConfiguration(String... params)
throws MojoFailureException {
if (StringUtils.isNotBlank(argLine)
&& MAVEN_DISALLOWED_PATTERN.matcher(argLine).find()) {
throw new MojoFailureException(
"The argLine doesn't match allowed pattern.");
}
if (params != null && params.length > 0) {
for (String p : params) {
if (StringUtils.isNotBlank(p)
&& MAVEN_DISALLOWED_PATTERN.matcher(p).find()) {
throw new MojoFailureException("The '" + p
+ "' value doesn't match allowed pattern.");
}
}
}
}

/**
Expand Down Expand Up @@ -826,6 +838,19 @@ protected void mvnCleanInstall() throws MojoFailureException,
executeMvnCommand("clean", "install");
}

/**
* Executes Maven goals.
*
* @param goals
* The goals to execute.
* @throws Exception
*/
protected void mvnRun(final String goals) throws Exception {
getLog().info("Running Maven goals: " + goals);

executeMvnCommand(CommandLineUtils.translateCommandline(goals));
}

/**
* Executes Git command and returns output.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.shared.release.versions.VersionParseException;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;

/**
* The git flow hotfix finish mojo.
Expand Down Expand Up @@ -62,10 +60,27 @@ public class GitFlowHotfixFinishMojo extends AbstractGitFlowMojo {
@Parameter(property = "pushRemote", defaultValue = "true")
private boolean pushRemote;

/**
* Maven goals to execute in the hotfix branch before merging into the
* production or support branch.
*
* @since 1.8.0
*/
@Parameter(property = "preHotfixGoals")
private String preHotfixGoals;

/**
* Maven goals to execute in the release or support branch after the hotfix.
*
* @since 1.8.0
*/
@Parameter(property = "postHotfixGoals")
private String postHotfixGoals;

/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();
validateConfiguration(preHotfixGoals, postHotfixGoals);

try {
// check uncommitted changes
Expand Down Expand Up @@ -148,6 +163,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
mvnCleanTest();
}

// maven goals before merge
if (StringUtils.isNotBlank(preHotfixGoals)) {
gitCheckout(hotfixBranchName);

mvnRun(preHotfixGoals);
}

if (supportBranchName != null) {
gitCheckout(supportBranchName);
} else {
Expand All @@ -171,6 +193,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
commitMessages.getTagHotfixMessage());
}

// maven goals after merge
if (StringUtils.isNotBlank(postHotfixGoals)) {
mvnRun(postHotfixGoals);
}

// check whether release branch exists
// git for-each-ref --count=1 --format="%(refname:short)"
// refs/heads/release/*
Expand Down Expand Up @@ -258,9 +285,7 @@ && notSameProdDevName()) {
gitPushDelete(hotfixBranchName);
}
}
} catch (CommandLineException e) {
getLog().error(e);
} catch (VersionParseException e) {
} catch (Exception e) {
getLog().error(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.shared.release.versions.VersionParseException;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;

/**
* The git flow release finish mojo.
Expand Down Expand Up @@ -128,10 +126,27 @@ public class GitFlowReleaseFinishMojo extends AbstractGitFlowMojo {
@Parameter(property = "commitDevelopmentVersionAtStart", defaultValue = "false")
private boolean commitDevelopmentVersionAtStart;

/**
* Maven goals to execute in the release branch before merging into the
* production branch.
*
* @since 1.8.0
*/
@Parameter(property = "preReleaseGoals")
private String preReleaseGoals;

/**
* Maven goals to execute in the production branch after the release.
*
* @since 1.8.0
*/
@Parameter(property = "postReleaseGoals")
private String postReleaseGoals;

/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();
validateConfiguration(preReleaseGoals, postReleaseGoals);

try {
// check uncommitted changes
Expand Down Expand Up @@ -184,6 +199,13 @@ public void execute() throws MojoExecutionException, MojoFailureException {
mvnCleanTest();
}

// maven goals before merge
if (StringUtils.isNotBlank(preReleaseGoals)) {
gitCheckout(releaseBranch);

mvnRun(preReleaseGoals);
}

// git checkout master
gitCheckout(gitFlowConfig.getProductionBranch());

Expand All @@ -205,6 +227,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
commitMessages.getTagReleaseMessage());
}

// maven goals after merge
if (StringUtils.isNotBlank(postReleaseGoals)) {
mvnRun(postReleaseGoals);
}

if (notSameProdDevName()) {
// git checkout develop
gitCheckout(gitFlowConfig.getDevelopmentBranch());
Expand Down Expand Up @@ -270,9 +297,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
gitPushDelete(releaseBranch);
}
}
} catch (CommandLineException e) {
getLog().error(e);
} catch (VersionParseException e) {
} catch (Exception e) {
getLog().error(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.shared.release.versions.VersionParseException;
import org.codehaus.plexus.components.interactivity.PrompterException;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;

/**
* The git flow release mojo.
Expand Down Expand Up @@ -125,10 +123,26 @@ public class GitFlowReleaseMojo extends AbstractGitFlowMojo {
@Parameter(property = "versionDigitToIncrement")
private Integer versionDigitToIncrement;

/**
* Maven goals to execute before the release.
*
* @since 1.8.0
*/
@Parameter(property = "preReleaseGoals")
private String preReleaseGoals;

/**
* Maven goals to execute after the release.
*
* @since 1.8.0
*/
@Parameter(property = "postReleaseGoals")
private String postReleaseGoals;

/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();
validateConfiguration(preReleaseGoals, postReleaseGoals);

try {
// set git flow configuration
Expand Down Expand Up @@ -219,6 +233,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
version = defaultVersion;
}

// maven goals before release
if (StringUtils.isNotBlank(preReleaseGoals)) {
mvnRun(preReleaseGoals);
}

// execute if version changed
if (!version.equals(currentVersion)) {
// mvn set version
Expand Down Expand Up @@ -250,6 +269,11 @@ public void execute() throws MojoExecutionException, MojoFailureException {
commitMessages.getTagReleaseMessage());
}

// maven goals after release
if (StringUtils.isNotBlank(postReleaseGoals)) {
mvnRun(postReleaseGoals);
}

if (notSameProdDevName()) {
// git checkout develop
gitCheckout(gitFlowConfig.getDevelopmentBranch());
Expand Down Expand Up @@ -295,9 +319,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
gitPush(gitFlowConfig.getDevelopmentBranch(), !skipTag);
}
}
} catch (CommandLineException e) {
getLog().error(e);
} catch (VersionParseException e) {
} catch (Exception e) {
getLog().error(e);
}
}
Expand Down

0 comments on commit 9e57bb0

Please sign in to comment.