diff --git a/maven-plugin/src/main/java/hudson/maven/MavenArgumentInterceptorAction.java b/maven-plugin/src/main/java/hudson/maven/MavenArgumentInterceptorAction.java new file mode 100644 index 0000000000..bedd1531ec --- /dev/null +++ b/maven-plugin/src/main/java/hudson/maven/MavenArgumentInterceptorAction.java @@ -0,0 +1,73 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Dominik Bartholdi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package hudson.maven; + +import hudson.model.Action; +import hudson.util.ArgumentListBuilder; + +/** + * Provides a hook to change the arguments passed to the maven execution. This + * enables plugins to transiently change the arguments of a maven build (e.g. + * change the arguments for a release build). + * + * @author Dominik Bartholdi (imod) + * + */ +public interface MavenArgumentInterceptorAction extends Action { + + /** + * Provides maven goals and options to start the build with. This is the + * preferred way to provide other goals then the default ones to a build. + * The goals and options returned by this method will not be persist and do + * not affect the default configuration. + *

+ * This method will be called on one and only one action during a build. If + * there are two actions present in the build, the second will be ignored. + * + * @return the maven goals and options to start maven with. Result is + * ignored if null or empty. Variables will be expanded + * by the caller. + */ + public String getGoalsAndOptions(); + + /** + * Change/add arguments to any needs, but special care has to be taken, as + * the list contains every argument needed for the default execution (e.g. + * -f /path/to/pom.xml or -B).
+ * An easy example would be to add "-DskipTests" to skip the + * test execution on request. + * + *

+ * This method is called on all present MavenArgumentInterceptorAction + * during a build (kind of chaining, each action can add the arguments it + * thinks are missing). + * + * @param mavenargs + * the calculated default maven arguments (never + * null). + * @return the new arguments to be used. + */ + public ArgumentListBuilder intercept(ArgumentListBuilder mavenargs); + +} diff --git a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java index 545fedf48b..1383506f3b 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -2,7 +2,7 @@ * The MIT License * * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, - * Red Hat, Inc., Victor Glushenkov, Alan Harder, Olivier Lamy + * Red Hat, Inc., Victor Glushenkov, Alan Harder, Olivier Lamy, Dominik Bartholdi * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -754,7 +754,24 @@ public class MavenModuleSetBuild extends AbstractMavenBuild argInterceptors = this.getBuild().getActions(MavenArgumentInterceptorAction.class); + for (MavenArgumentInterceptorAction mavenArgInterceptor : argInterceptors) { + final ArgumentListBuilder newMargs = mavenArgInterceptor.intercept(margs); + if (newMargs != null) { + margs = newMargs; + } + } + if (maven3orLater) { diff --git a/test/src/test/java/hudson/maven/MavenArgumentInterceptorTest.java b/test/src/test/java/hudson/maven/MavenArgumentInterceptorTest.java new file mode 100644 index 0000000000..25eabb15b7 --- /dev/null +++ b/test/src/test/java/hudson/maven/MavenArgumentInterceptorTest.java @@ -0,0 +1,171 @@ +package hudson.maven; + +/* + * The MIT License + * + * Copyright (c) 2011, Dominik Bartholdi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +import hudson.Extension; +import hudson.Launcher; +import hudson.model.AbstractBuild; +import hudson.model.AbstractProject; +import hudson.model.BuildListener; +import hudson.model.Result; +import hudson.tasks.BuildWrapper; +import hudson.tasks.BuildWrapperDescriptor; +import hudson.tasks.Maven.MavenInstallation; +import hudson.util.ArgumentListBuilder; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import net.sf.json.JSONObject; + +import org.jvnet.hudson.test.ExtractResourceSCM; +import org.jvnet.hudson.test.HudsonTestCase; +import org.kohsuke.stapler.StaplerRequest; + +/** + * @author Dominik Bartholdi (imod) + */ +public class MavenArgumentInterceptorTest extends HudsonTestCase { + + public void testSimpleMaven3BuildWithArgInterceptor_Goals() throws Exception { + + MavenModuleSet m = createMavenProject(); + MavenInstallation mavenInstallation = configureMaven3(); + m.setMaven(mavenInstallation.getName()); + m.setScm(new ExtractResourceSCM(getClass().getResource("maven3-project.zip"))); + m.setGoals("dummygoal"); // build would fail with this goal + + // add an action to build, redefining the goals and options to be + // executed + m.getBuildWrappersList().add(new TestMvnBuildWrapper("clean")); + + MavenModuleSetBuild b = buildAndAssertSuccess(m); + assertTrue(MavenUtil.maven3orLater(b.getMavenVersionUsed())); + } + + public void testSimpleMaven3BuildWithArgInterceptor_ArgBuilder() throws Exception { + + MavenModuleSet m = createMavenProject(); + MavenInstallation mavenInstallation = configureMaven3(); + m.setMaven(mavenInstallation.getName()); + m.setScm(new ExtractResourceSCM(getClass().getResource("maven-multimodule-unit-failure.zip"))); + m.setGoals("clean install"); // build would fail because of failing unit + // tests + + // add an action to build, adding argument to skip the test execution + m.getBuildWrappersList().add(new TestMvnBuildWrapper(Arrays.asList("-DskipTests"))); + + MavenModuleSetBuild b = buildAndAssertSuccess(m); + assertTrue(MavenUtil.maven3orLater(b.getMavenVersionUsed())); + } + + private static class TestMvnArgInterceptor implements MavenArgumentInterceptorAction { + private String goalsAndOptions; + private List args; + + public TestMvnArgInterceptor(String goalsAndOptions) { + this.goalsAndOptions = goalsAndOptions; + } + + public TestMvnArgInterceptor(List args) { + this.args = args; + } + + public String getIconFileName() { + return null; + } + + public String getDisplayName() { + return null; + } + + public String getUrlName() { + return null; + } + + public String getGoalsAndOptions() { + return goalsAndOptions; + } + + public ArgumentListBuilder intercept(ArgumentListBuilder mavenargs) { + if (args != null) { + for (String arg : this.args) { + mavenargs.add(arg); + } + } + return mavenargs; + } + } + + public static class TestMvnBuildWrapper extends BuildWrapper { + public Result buildResultInTearDown; + private String goalsAndOptions; + private List args; + + public TestMvnBuildWrapper(String goalsAndOptions) { + this.goalsAndOptions = goalsAndOptions; + } + + public TestMvnBuildWrapper(List args) { + this.args = args; + } + + @Override + public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException { + + if (goalsAndOptions != null) { + build.addAction(new TestMvnArgInterceptor(goalsAndOptions)); + } else if (args != null) { + build.addAction(new TestMvnArgInterceptor(args)); + } + + return new BuildWrapper.Environment() { + @Override + public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOException, InterruptedException { + buildResultInTearDown = build.getResult(); + return true; + } + }; + } + + @Extension + public static class TestMvnBuildWrapperDescriptor extends BuildWrapperDescriptor { + @Override + public boolean isApplicable(AbstractProject project) { + return true; + } + + @Override + public BuildWrapper newInstance(StaplerRequest req, JSONObject formData) { + throw new UnsupportedOperationException(); + } + + @Override + public String getDisplayName() { + return this.getClass().getName(); + } + } + } +}