Skip to content

Commit

Permalink
add ability to allow to pass arguments to underlying Maven commands - c…
Browse files Browse the repository at this point in the history
…loses #53
  • Loading branch information
aleksandr-m committed Oct 2, 2017
1 parent f4f5d69 commit 913a676
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Pattern;

import org.apache.maven.artifact.ArtifactUtils;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -54,6 +55,10 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
/** Success exit code. */
private static final int SUCCESS_EXIT_CODE = 0;

/** Pattern of disallowed characters in Maven commands. */
private static final Pattern MAVEN_DISALLOWED_PATTERN = Pattern
.compile("[&|;]");

/** Command line for Git executable. */
private final Commandline cmdGit = new Commandline();
/** Command line for Maven executable. */
Expand Down Expand Up @@ -103,6 +108,14 @@ public abstract class AbstractGitFlowMojo extends AbstractMojo {
@Parameter(property = "verbose", defaultValue = "false")
private boolean verbose = false;

/**
* Command line arguments to pass to the underlying Maven commands.
*
* @since 1.8.0
*/
@Parameter(property = "argLine")
private String argLine;

/**
* The path to the Maven executable. Defaults to "mvn".
*/
Expand Down Expand Up @@ -143,6 +156,21 @@ private void initExecutables() {
}
}

/**
* Validates plugin configuration. Throws exception if configuration is not
* valid.
*
* @throws MojoFailureException
* If configuration is not valid.
*/
protected void validateConfiguration() throws MojoFailureException {
if (StringUtils.isNotBlank(argLine)
&& MAVEN_DISALLOWED_PATTERN.matcher(argLine).find()) {
throw new MojoFailureException(
"The argLine doesn't match allowed pattern.");
}
}

/**
* Gets current project version from pom.xml file.
*
Expand Down Expand Up @@ -809,7 +837,7 @@ protected void mvnCleanInstall() throws MojoFailureException,
*/
private String executeGitCommandReturn(final String... args)
throws CommandLineException, MojoFailureException {
return executeCommand(cmdGit, true, args).getOut();
return executeCommand(cmdGit, true, null, args).getOut();
}

/**
Expand All @@ -823,7 +851,7 @@ private String executeGitCommandReturn(final String... args)
*/
private CommandResult executeGitCommandExitCode(final String... args)
throws CommandLineException, MojoFailureException {
return executeCommand(cmdGit, false, args);
return executeCommand(cmdGit, false, null, args);
}

/**
Expand All @@ -836,7 +864,7 @@ private CommandResult executeGitCommandExitCode(final String... args)
*/
private void executeGitCommand(final String... args)
throws CommandLineException, MojoFailureException {
executeCommand(cmdGit, true, args);
executeCommand(cmdGit, true, null, args);
}

/**
Expand All @@ -849,7 +877,7 @@ private void executeGitCommand(final String... args)
*/
private void executeMvnCommand(final String... args)
throws CommandLineException, MojoFailureException {
executeCommand(cmdMvn, true, args);
executeCommand(cmdMvn, true, argLine, args);
}

/**
Expand All @@ -859,6 +887,8 @@ private void executeMvnCommand(final String... args)
* Command line.
* @param failOnError
* Whether to throw exception on NOT success exit code.
* @param argStr
* Command line arguments as a string.
* @param args
* Command line arguments.
* @return {@link CommandResult} instance holding command exit code, output
Expand All @@ -869,19 +899,25 @@ private void executeMvnCommand(final String... args)
* exit code is NOT equals to 0.
*/
private CommandResult executeCommand(final Commandline cmd,
final boolean failOnError, final String... args)
throws CommandLineException, MojoFailureException {
final boolean failOnError, final String argStr,
final String... args) throws CommandLineException,
MojoFailureException {
// initialize executables
initExecutables();

if (getLog().isDebugEnabled()) {
getLog().debug(
cmd.getExecutable() + " " + StringUtils.join(args, " "));
cmd.getExecutable() + " " + StringUtils.join(args, " ")
+ (argStr == null ? "" : " " + argStr));
}

cmd.clearArgs();
cmd.addArguments(args);

if (StringUtils.isNotBlank(argStr)) {
cmd.createArg().setLine(argStr);
}

final StringBufferStreamConsumer out = new StringBufferStreamConsumer(
verbose);

Expand Down Expand Up @@ -938,4 +974,8 @@ public String getError() {
return error;
}
}

public void setArgLine(String argLine) {
this.argLine = argLine;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class GitFlowFeatureFinishMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// check uncommitted changes
checkUncommittedChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class GitFlowFeatureStartMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// set git flow configuration
initGitFlowConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class GitFlowHotfixFinishMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// check uncommitted changes
checkUncommittedChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public class GitFlowHotfixStartMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// set git flow configuration
initGitFlowConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public class GitFlowReleaseFinishMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// check uncommitted changes
checkUncommittedChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public class GitFlowReleaseMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// set git flow configuration
initGitFlowConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class GitFlowReleaseStartMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// set git flow configuration
initGitFlowConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class GitFlowSupportStartMojo extends AbstractGitFlowMojo {
/** {@inheritDoc} */
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
validateConfiguration();

try {
// set git flow configuration
initGitFlowConfig();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.apache.maven.plugin.MojoFailureException;
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 ValidateConfigurationTest {
private final String argLine;
private final boolean expected;

public ValidateConfigurationTest(String argLine, boolean expected) {
this.argLine = argLine;
this.expected = expected;
}

@Parameters(name = "{0}->{1}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] { { "-X -e", true },
{ "-DsomeArg1=true", true }, { null, true },
{ "", true }, { "-DsomeArg & clean", false },
{ "-DsomeArg && clean", false },
{ "-DsomeArg | clean", false },
{ "-DsomeArg || clean", false },
{ "-DsomeArg ; clean", false } });
}

@Test
public void testValidateConfiguration() throws Exception {
GitFlowReleaseStartMojo mojo = new GitFlowReleaseStartMojo();
mojo.setArgLine(argLine);

try {
mojo.validateConfiguration();
} catch (MojoFailureException e) {
if (expected) {
Assert.fail();
}
}
}
}

0 comments on commit 913a676

Please sign in to comment.