diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java index d4d920ab..6dd7910a 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/AbstractGitFlowMojo.java @@ -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; @@ -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. */ @@ -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". */ @@ -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. * @@ -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(); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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 @@ -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); @@ -938,4 +974,8 @@ public String getError() { return error; } } + + public void setArgLine(String argLine) { + this.argLine = argLine; + } } diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java index ab697e36..1022fa2a 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureFinishMojo.java @@ -67,6 +67,8 @@ public class GitFlowFeatureFinishMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // check uncommitted changes checkUncommittedChanges(); diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java index 1e6145bd..38db136b 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowFeatureStartMojo.java @@ -63,6 +63,8 @@ public class GitFlowFeatureStartMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // set git flow configuration initGitFlowConfig(); diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java index 7a8bbd12..e8bddcb8 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixFinishMojo.java @@ -65,6 +65,8 @@ public class GitFlowHotfixFinishMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // check uncommitted changes checkUncommittedChanges(); diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java index 3fc785ac..63a2586d 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowHotfixStartMojo.java @@ -47,6 +47,8 @@ public class GitFlowHotfixStartMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // set git flow configuration initGitFlowConfig(); diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java index 31f57927..158439b4 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseFinishMojo.java @@ -131,6 +131,8 @@ public class GitFlowReleaseFinishMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // check uncommitted changes checkUncommittedChanges(); diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java index 1615e295..6c299b31 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseMojo.java @@ -128,6 +128,8 @@ public class GitFlowReleaseMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // set git flow configuration initGitFlowConfig(); diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java index 70a0d505..2084c6b3 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowReleaseStartMojo.java @@ -121,6 +121,8 @@ public class GitFlowReleaseStartMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // set git flow configuration initGitFlowConfig(); diff --git a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java index 54f6364e..ef059c4b 100644 --- a/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java +++ b/src/main/java/com/amashchenko/maven/plugin/gitflow/GitFlowSupportStartMojo.java @@ -44,6 +44,8 @@ public class GitFlowSupportStartMojo extends AbstractGitFlowMojo { /** {@inheritDoc} */ @Override public void execute() throws MojoExecutionException, MojoFailureException { + validateConfiguration(); + try { // set git flow configuration initGitFlowConfig(); diff --git a/src/test/java/com/amashchenko/maven/plugin/gitflow/ValidateConfigurationTest.java b/src/test/java/com/amashchenko/maven/plugin/gitflow/ValidateConfigurationTest.java new file mode 100644 index 00000000..d289a773 --- /dev/null +++ b/src/test/java/com/amashchenko/maven/plugin/gitflow/ValidateConfigurationTest.java @@ -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 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(); + } + } + } +}