Skip to content

Commit

Permalink
allow to run additional maven goals before and after merge to product…
Browse files Browse the repository at this point in the history
…ion in the release-finish goal
  • Loading branch information
aleksandr-m committed Apr 11, 2017
1 parent 30d9636 commit 1e26c36
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,17 @@ protected void mvnCleanInstall() throws MojoFailureException,
executeMvnCommand("clean", "install");
}

protected void mvnRun(final String goals) throws MojoFailureException,
CommandLineException {
if (goals != null && ConfigurationValidator.isAllowedMavenGoal(goals)) {
getLog().info("Running Maven goals: " + goals);

executeMvnCommand(goals.split(" "));
} else {
getLog().warn("Maven goals '" + goals + "' aren't allowed to run.");
}
}

/**
* Executes Git command and returns output.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2014-2017 Aleksandr Mashchenko.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.amashchenko.maven.plugin.gitflow;

import java.util.regex.Pattern;

public class ConfigurationValidator {
private static final String LS = System.getProperty("line.separator");

private static final Pattern MAVEN_ALLOWED_GOALS = Pattern
.compile("^[a-zA-Z0-9 .,=:_-]+?$");

public static boolean isAllowedMavenGoal(final String goal) {
return MAVEN_ALLOWED_GOALS.matcher(goal).matches();
}

public static String validateGoalConfig(GoalConfig goalConfig) {
StringBuilder errors = new StringBuilder();
if (goalConfig != null) {
if (goalConfig.getPreProductionMergeGoals() != null
&& !isAllowedMavenGoal(goalConfig
.getPreProductionMergeGoals())) {
errors.append("preProductionMergeGoals value doesn't match allowed pattern: "
+ MAVEN_ALLOWED_GOALS);
errors.append(LS);
}
if (goalConfig.getPostProductionMergeGoals() != null
&& !isAllowedMavenGoal(goalConfig
.getPostProductionMergeGoals())) {
errors.append("postProductionMergeGoals value doesn't match allowed pattern: "
+ MAVEN_ALLOWED_GOALS);
errors.append(LS);
}
}
return errors.length() == 0 ? null : errors.toString();
}

private ConfigurationValidator() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,19 @@ public class GitFlowReleaseFinishMojo extends AbstractGitFlowMojo {
@Parameter(property = "releaseMergeFFOnly", defaultValue = "false")
private boolean releaseMergeFFOnly = false;

@Parameter
private GoalConfig releaseFinish;

/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
String errors = ConfigurationValidator
.validateGoalConfig(releaseFinish);
if (errors != null) {
throw new MojoFailureException(
"The are some errors in releaseFinish configuration. "
+ errors);
}
try {
// check uncommitted changes
checkUncommittedChanges();
Expand Down Expand Up @@ -124,12 +134,29 @@ public void execute() throws MojoExecutionException, MojoFailureException {
mvnCleanTest();
}

// additional pre production merge Maven goals
if (releaseFinish != null
&& StringUtils.isNotBlank(releaseFinish
.getPreProductionMergeGoals())) {
// git checkout release/...
gitCheckout(releaseBranch);

mvnRun(releaseFinish.getPreProductionMergeGoals());
}

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

gitMerge(releaseBranch, releaseRebase, releaseMergeNoFF,
releaseMergeFFOnly);

// additional post production merge Maven goals
if (releaseFinish != null
&& StringUtils.isNotBlank(releaseFinish
.getPostProductionMergeGoals())) {
mvnRun(releaseFinish.getPostProductionMergeGoals());
}

// get current project version from pom
final String currentVersion = getCurrentProjectVersion();

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/amashchenko/maven/plugin/gitflow/GoalConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2014-2017 Aleksandr Mashchenko.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.amashchenko.maven.plugin.gitflow;

public class GoalConfig {
private String preProductionMergeGoals;
private String postProductionMergeGoals;

/**
* @return the preProductionMergeGoals
*/
public String getPreProductionMergeGoals() {
return preProductionMergeGoals;
}

/**
* @return the postProductionMergeGoals
*/
public String getPostProductionMergeGoals() {
return postProductionMergeGoals;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2014-2017 Aleksandr Mashchenko.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.amashchenko.maven.plugin.gitflow;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class AllowedMavenGoalTest {
private final String goals;
private final boolean expected;

public AllowedMavenGoalTest(final String goals, final boolean expected) {
this.goals = goals;
this.expected = expected;
}

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { "clean install", true },
{ "clean deploy -DsomeArg1=true", true },
{ "clean deploy -Prelease,deploy", true },
{ "groupId:artifactId:123.456.7890", true },
{ "clean & clean", false }, { "clean \\clean", false },
{ "clean .// clean", false } });
}

@Test
public void testIsAllowedMavenGoal() throws Exception {
Assert.assertEquals(expected,
ConfigurationValidator.isAllowedMavenGoal(goals));
}
}

0 comments on commit 1e26c36

Please sign in to comment.