提交 74d2d437 编写于 作者: I imod

add extension to intercept maven arguments

上级 28b50276
/*
* 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.
* <p>
* 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 <code>null</code> 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). <br />
* An easy example would be to add "<code>-DskipTests</code>" to skip the
* test execution on request.
*
* <p>
* 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
* <code>null</code>).
* @return the new arguments to be used.
*/
public ArgumentListBuilder intercept(ArgumentListBuilder mavenargs);
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* The MIT License * The MIT License
* *
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, * 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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
...@@ -754,7 +754,24 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven ...@@ -754,7 +754,24 @@ public class MavenModuleSetBuild extends AbstractMavenBuild<MavenModuleSet,Maven
} }
} }
margs.addTokenized(envVars.expand(project.getGoals())); // find the correct maven goals and options, there might by an action overruling the defaults
// only one action is allowed to overwrite the hole "goals and options" string
final MavenArgumentInterceptorAction interceptorAction = this.getBuild().getAction(MavenArgumentInterceptorAction.class);
final String goals = interceptorAction != null && StringUtils.isNotBlank(interceptorAction.getGoalsAndOptions()) ? interceptorAction
.getGoalsAndOptions() : project.getGoals();
margs.addTokenized(envVars.expand(goals));
// enable the interceptors to change the whole command argument list
// all available interceptors are allowed to modify the argument list
final List<MavenArgumentInterceptorAction> argInterceptors = this.getBuild().getActions(MavenArgumentInterceptorAction.class);
for (MavenArgumentInterceptorAction mavenArgInterceptor : argInterceptors) {
final ArgumentListBuilder newMargs = mavenArgInterceptor.intercept(margs);
if (newMargs != null) {
margs = newMargs;
}
}
if (maven3orLater) if (maven3orLater)
{ {
......
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<String> args;
public TestMvnArgInterceptor(String goalsAndOptions) {
this.goalsAndOptions = goalsAndOptions;
}
public TestMvnArgInterceptor(List<String> 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<String> args;
public TestMvnBuildWrapper(String goalsAndOptions) {
this.goalsAndOptions = goalsAndOptions;
}
public TestMvnBuildWrapper(List<String> 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();
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册