Skip to content

Commit

Permalink
Merge pull request #29 from goober/master
Browse files Browse the repository at this point in the history
[JENKINS-11964] Enable single module build for Maven3.
  • Loading branch information
olamy committed Oct 3, 2014
2 parents 299ccd3 + b3f08bd commit eceb028
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 30 deletions.
110 changes: 80 additions & 30 deletions src/main/java/hudson/maven/MavenBuild.java
Expand Up @@ -53,12 +53,16 @@
import hudson.tasks.Publisher;
import hudson.util.ArgumentListBuilder;
import hudson.util.DescribableList;
import jenkins.maven3.agent.Maven31Main;
import org.apache.maven.BuildFailureException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.execution.ReactorManager;
import org.apache.maven.lifecycle.LifecycleExecutionException;
import org.apache.maven.monitor.event.EventDispatcher;
import org.apache.maven.project.MavenProject;
import org.jvnet.hudson.maven3.agent.Maven3Main;
import org.jvnet.hudson.maven3.launcher.Maven31Launcher;
import org.jvnet.hudson.maven3.launcher.Maven3Launcher;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -546,6 +550,7 @@ public class ProxyImpl2 extends ProxyImpl implements MavenBuildProxy2 {
long startTime;
private final OutputStream log;
private final MavenModuleSetBuild parentBuild;
private boolean blockBuildEvents;

ProxyImpl2(MavenModuleSetBuild parentBuild,SplittableBuildListener listener) throws FileNotFoundException {
this.parentBuild = parentBuild;
Expand All @@ -554,7 +559,8 @@ public class ProxyImpl2 extends ProxyImpl implements MavenBuildProxy2 {
}

public void start() {
onStartBuilding();
if(!blockBuildEvents)
onStartBuilding();
startTime = System.currentTimeMillis();
try {
sync();
Expand All @@ -568,7 +574,8 @@ public void start() {
public void end() {
if(result==null)
setResult(Result.SUCCESS);
onEndBuilding();
if(!blockBuildEvents)
onEndBuilding();
duration += System.currentTimeMillis()- startTime;
parentBuild.notifyModuleBuild(MavenBuild.this);
try {
Expand Down Expand Up @@ -735,6 +742,10 @@ private Object writeReplace() {
return Channel.current().export(MavenBuildProxy2.class,
Executor.currentExecutor().newImpersonatingProxy(MavenBuildProxy2.class,this));
}

protected void setBlockBuildEvents(boolean blockBuildEvents) {
this.blockBuildEvents = blockBuildEvents;
}
}


Expand All @@ -758,6 +769,8 @@ protected Lease decideWorkspace(Node n, WorkspaceList wsl) throws InterruptedExc
}

protected Result doRun(BuildListener listener) throws Exception {
SplittableBuildListener slistener = new SplittableBuildListener(listener);

// pick up a list of reporters to run
reporters = getProject().createReporters();
MavenModuleSet mms = getProject().getParent();
Expand All @@ -783,7 +796,8 @@ protected Result doRun(BuildListener listener) throws Exception {
String mavenVersion = mavenInformation.getVersion();

LOGGER.fine(getFullDisplayName()+" is building with mavenVersion " + mavenVersion + " from file " + mavenInformation.getVersionResourcePath());


MavenBuildInformation mavenBuildInformation = new MavenBuildInformation( mavenVersion );

MavenUtil.MavenVersion mavenVersionType = MavenUtil.getMavenVersion( mavenVersion );

Expand All @@ -804,7 +818,7 @@ protected Result doRun(BuildListener listener) throws Exception {

}

ProcessCache.MavenProcess process = MavenBuild.mavenProcessCache.get( launcher.getChannel(), listener, factory);
ProcessCache.MavenProcess process = MavenBuild.mavenProcessCache.get( launcher.getChannel(), slistener, factory);

ArgumentListBuilder margs = new ArgumentListBuilder("-N","-B");
FilePath localRepo = mms.getLocalRepository().locate(MavenBuild.this);
Expand All @@ -824,38 +838,74 @@ protected Result doRun(BuildListener listener) throws Exception {
// backward compatibility
systemProps.put("hudson.build.number",String.valueOf(getNumber()));

if (mavenVersionType == MavenUtil.MavenVersion.MAVEN_3_0_X || mavenVersionType == MavenUtil.MavenVersion.MAVEN_3_1)
{
// FIXME here for maven 3 builds
listener.getLogger().println("Building single Maven modules is not implemented for Maven 3, yet!");
return Result.ABORTED;
}
else
ProxyImpl proxy;
AbstractMavenBuilder builder;
if (mavenBuildInformation.isMaven3OrLater())
{
boolean normalExit = false;
try {
ProxyImpl proxy = new ProxyImpl();
Result r = process.call(new Builder(
ProxyImpl2 proxy2 = new ProxyImpl2(mms.getLastCompletedBuild(), slistener);
proxy2.setBlockBuildEvents(true);
proxy = proxy2;
builder = new Maven3Builder(createRequest(proxy2,
getProject(), margs.toList(), systemProps,
mavenBuildInformation));
}
else {
proxy = new ProxyImpl();
builder = new Builder(
listener, proxy,
getProject(), margs.toList(), systemProps));
proxy.performArchiving(launcher, listener);
normalExit = true;
return r;
} finally {
if(normalExit) process.recycle();
else process.discard();
getProject(), margs.toList(), systemProps);
}
boolean normalExit = false;
try {
Result r = process.call(builder);
proxy.performArchiving(launcher, listener);
normalExit = true;
return r;
} finally {
builder.end(launcher);
if(normalExit) process.recycle();
else process.discard();

// tear down in reverse order
boolean failed=false;
for( int i=buildEnvironments.size()-1; i>=0; i-- ) {
if (!buildEnvironments.get(i).tearDown(MavenBuild.this,listener)) {
failed=true;
}
// tear down in reverse order
boolean failed=false;
for( int i=buildEnvironments.size()-1; i>=0; i-- ) {
if (!buildEnvironments.get(i).tearDown(MavenBuild.this,slistener)) {
failed=true;
}
// WARNING The return in the finally clause will trump any return before
if (failed) return Result.FAILURE;
}
// WARNING The return in the finally clause will trump any return before
if (failed) return Result.FAILURE;
}
}

private Maven3Builder.Maven3BuilderRequest createRequest(ProxyImpl2 buildProxy,
MavenModule module,
List<String> goals,
Map<String,String> systemProps,
MavenBuildInformation mavenBuildInformation) {
String mavenVersion = mavenBuildInformation.getMavenVersion();

Maven3Builder.Maven3BuilderRequest request = new Maven3Builder.Maven3BuilderRequest();
request.listener = buildProxy.listener;
request.modules = Collections.singleton(module);
request.goals = goals;
request.systemProps = systemProps;
request.proxies = new HashMap<ModuleName, ProxyImpl2>();
request.proxies.put(module.getModuleName(), buildProxy);
request.mavenBuildInformation = mavenBuildInformation;
request.supportEventSpy = MavenUtil.supportEventSpy(mavenVersion);

switch(MavenUtil.getMavenVersion(mavenVersion)) {
case MAVEN_3_0_X:
request.maven3MainClass = Maven3Main.class;
request.maven3LauncherClass = Maven3Launcher.class;
break;
default:
request.maven3MainClass = Maven31Main.class;
request.maven3LauncherClass = Maven31Launcher.class;
}

return request;
}

public void post2(BuildListener listener) throws Exception {
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/hudson/maven/AbstractMaven3xBuildTest.java
Expand Up @@ -20,6 +20,7 @@
* under the License.
*/

import hudson.FilePath;
import hudson.Launcher;
import hudson.model.BuildListener;
import hudson.model.ParametersDefinitionProperty;
Expand All @@ -28,6 +29,7 @@
import hudson.tasks.Maven.MavenInstallation;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResultProjectAction;
import jenkins.model.ArtifactManagerConfiguration;
import org.apache.commons.io.FileUtils;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.Email;
Expand All @@ -37,6 +39,9 @@
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.*;

import static org.junit.Assert.assertEquals;

/**
* @author Olivier Lamy
Expand Down Expand Up @@ -250,6 +255,31 @@ public void testTychoEclipseTestResults() throws Exception {
int totalCount = trpa.getTotalCount();
assertEquals(1, totalCount);
}

@Bug(11964)
public void testSingleModuleBuild() throws Exception {
// Given a multi-module build.
MavenInstallation mavenInstallation = configureMaven3x();
MavenModuleSet m = createMavenProject();
m.setMaven( mavenInstallation.getName() );
m.getReporters().add(new TestReporter());
m.setScm(new ExtractResourceSCM(getClass().getResource("maven-multimod.zip")));
m.setGoals("verify");
buildAndAssertSuccess(m);

// When building a single module within the given build.
MavenModule singleModule = m.getModule("org.jvnet.hudson.main.test.multimod$moduleA");
MavenBuild isolated = buildAndAssertSuccess(singleModule);

// Then only the single module should have been built.
Collection<MavenModule> expectedNonBuiltModules = new ArrayList<MavenModule>(m.getModules());
expectedNonBuiltModules.remove(singleModule);

assertEquals(2, isolated.number);
for(MavenModule module : expectedNonBuiltModules) {
assertEquals(1,module.getLastBuild().getNumber());
}
}

private static class TestReporter extends MavenReporter {
@Override
Expand Down

0 comments on commit eceb028

Please sign in to comment.