diff --git a/maven-plugin/src/main/java/hudson/maven/MavenBuild.java b/maven-plugin/src/main/java/hudson/maven/MavenBuild.java index ba37e241b6..8ad4846911 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenBuild.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenBuild.java @@ -30,6 +30,7 @@ import hudson.maven.reporters.SurefireArchiver; import hudson.model.AbstractBuild; import hudson.model.BuildListener; import hudson.model.Computer; +import hudson.model.Descriptor; import hudson.model.Environment; import hudson.model.Executor; import jenkins.model.Jenkins; @@ -44,7 +45,9 @@ import hudson.slaves.WorkspaceList; import hudson.slaves.WorkspaceList.Lease; import hudson.tasks.BuildWrapper; import hudson.tasks.Maven.MavenInstallation; +import hudson.tasks.Publisher; import hudson.util.ArgumentListBuilder; +import hudson.util.DescribableList; import hudson.util.IOUtils; import hudson.util.ReflectionUtils; @@ -544,23 +547,32 @@ public class MavenBuild extends AbstractMavenBuild { public void cleanUp(BuildListener listener) { } }); - - + } + + rememberModulesToBuildAgainNextTime(); + } + + private void rememberModulesToBuildAgainNextTime() { + MavenModuleSetBuild moduleSetBuild = getModuleSetBuild(); + + if(hasntStartedYet()) { // record modules which have not been build though they should have - i.e. because they // have SCM changes. // see JENKINS-5764 - if (getParentBuild().getParent().isIncrementalBuild() && getParentBuild().getResult() == Result.FAILURE) { - UnbuiltModuleAction action = getParentBuild().getAction(UnbuiltModuleAction.class); + if (moduleSetBuild.getParent().isIncrementalBuild() + && moduleSetBuild.getResult() != Result.SUCCESS + && moduleSetBuild.getResult() != Result.UNSTABLE) { + UnbuiltModuleAction action = moduleSetBuild.getAction(UnbuiltModuleAction.class); if (action == null) { action = new UnbuiltModuleAction(); - getParentBuild().getActions().add(action); + moduleSetBuild.getActions().add(action); } action.addUnbuiltModule(getParent().getModuleName()); } } else { // mark that this module has been built now, if it has previously been remembered as unbuilt // JENKINS-5764 - MavenModuleSetBuild previousParentBuild = getParentBuild().getPreviousBuild(); + MavenModuleSetBuild previousParentBuild = moduleSetBuild.getPreviousBuild(); if (previousParentBuild != null) { UnbuiltModuleAction unbuiltModuleAction = previousParentBuild.getAction(UnbuiltModuleAction.class); if (unbuiltModuleAction != null) { @@ -572,6 +584,32 @@ public class MavenBuild extends AbstractMavenBuild { } } } + + if (moduleSetBuild.getParent().isIncrementalBuild() && + (moduleSetBuild.getResult() != Result.SUCCESS)) { + + // JENKINS-5121: maybe module needs to be deployed on next build over the deployment threshold + MavenModuleSet mavenModuleSet = moduleSetBuild.getParent(); + boolean isDeploying = false; + Result deploymentThreshold = Result.SUCCESS; + DescribableList> publishers = mavenModuleSet.getPublishersList(); + for (Publisher publisher : publishers) { + if (publisher instanceof RedeployPublisher) { + isDeploying = true; + deploymentThreshold = ((RedeployPublisher)publisher).getTreshold(); + break; + } + } + + if (isDeploying && moduleSetBuild.getResult().isWorseThan(deploymentThreshold)) { + UnbuiltModuleAction action = moduleSetBuild.getAction(UnbuiltModuleAction.class); + if (action == null) { + action = new UnbuiltModuleAction(); + moduleSetBuild.getActions().add(action); + } + action.addUnbuiltModule(getParent().getModuleName()); + } + } } } diff --git a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java index 9d09938bc7..c41d9435fb 100644 --- a/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java +++ b/maven-plugin/src/main/java/hudson/maven/MavenModuleSetBuild.java @@ -796,6 +796,11 @@ public class MavenModuleSetBuild extends AbstractMavenBuild getUnbuildModulesSinceLastSuccessfulBuild() { Collection unbuiltModules = new ArrayList(); MavenModuleSetBuild previousSuccessfulBuild = getPreviousSuccessfulBuild(); + if (previousSuccessfulBuild == null) { + // no successful build, yet. Just take the 1st build + previousSuccessfulBuild = getParent().getFirstBuild(); + } + if (previousSuccessfulBuild != null) { MavenModuleSetBuild previousBuild = previousSuccessfulBuild; do { diff --git a/test/src/test/java/hudson/maven/MavenMultiModuleTest.java b/test/src/test/java/hudson/maven/MavenMultiModuleTest.java index 1cdd6c650d..06d614b587 100644 --- a/test/src/test/java/hudson/maven/MavenMultiModuleTest.java +++ b/test/src/test/java/hudson/maven/MavenMultiModuleTest.java @@ -7,8 +7,12 @@ import org.jvnet.hudson.test.ExtractResourceWithChangesSCM; import org.jvnet.hudson.test.ExtractChangeLogSet; import hudson.Launcher; +import hudson.model.AbstractBuild; import hudson.model.BuildListener; import hudson.model.Result; +import hudson.scm.ChangeLogSet; +import hudson.scm.ChangeLogSet.Entry; +import hudson.scm.NullSCM; import hudson.tasks.Maven.MavenInstallation; import java.io.IOException; @@ -195,37 +199,123 @@ public class MavenMultiModuleTest extends HudsonTestCase { public void testIncrementalMultiModWithErrorsMaven() throws Exception { configureDefaultMaven("apache-maven-2.2.1", MavenInstallation.MAVEN_21); MavenModuleSet m = createMavenProject(); + m.setIncrementalBuild(true); m.getReporters().add(new TestReporter()); m.setScm(new ExtractResourceWithChangesSCM(getClass().getResource("maven-multimod-incr.zip"), getClass().getResource("maven-multimod-changes.zip"))); assertBuildStatus(Result.UNSTABLE, m.scheduleBuild2(0).get()); + MavenModuleSetBuild pBuild = m.getLastBuild(); + + for (MavenBuild modBuild : pBuild.getModuleLastBuilds().values()) { + String parentModuleName = modBuild.getParent().getModuleName().toString(); + if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleA")) { + assertEquals("moduleA should have Result.UNSTABLE", Result.UNSTABLE, modBuild.getResult()); + } + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleB")) { + assertEquals("moduleB should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleC")) { + assertEquals("moduleC should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleD")) { + assertEquals("moduleD should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + } // Now run a second build with the changes. - m.setIncrementalBuild(true); assertBuildStatus(Result.UNSTABLE, m.scheduleBuild2(0).get()); - MavenModuleSetBuild pBuild = m.getLastBuild(); + pBuild = m.getLastBuild(); ExtractChangeLogSet changeSet = (ExtractChangeLogSet) pBuild.getChangeSet(); assertFalse("ExtractChangeLogSet should not be empty.", changeSet.isEmptySet()); + // changelog contains a change for module B assertEquals("Parent build should have Result.UNSTABLE", Result.UNSTABLE, pBuild.getResult()); for (MavenBuild modBuild : pBuild.getModuleLastBuilds().values()) { String parentModuleName = modBuild.getParent().getModuleName().toString(); + // A must be build again, because it was UNSTABLE before if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleA")) { assertEquals("moduleA should have Result.UNSTABLE", Result.UNSTABLE, modBuild.getResult()); } + // B must be build, because it has changes else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleB")) { assertEquals("moduleB should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); } + // C must be build, because it depends on B else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleC")) { assertEquals("moduleC should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); } + // D must not be build else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleD")) { assertEquals("moduleD should have Result.NOT_BUILT", Result.NOT_BUILT, modBuild.getResult()); } - } + } + } + + /** + * If "deploy modules" is checked and aggregator build failed + * then all modules build this time, have to be build next time, again. + */ + @Bug(5121) + public void testIncrementalRedeployAfterAggregatorError() throws Exception { + configureDefaultMaven("apache-maven-2.2.1", MavenInstallation.MAVEN_21); + MavenModuleSet m = createMavenProject(); + m.setIncrementalBuild(true); + m.getReporters().add(new TestReporter()); + m.getPublishers().add(new DummyRedeployPublisher()); + m.setScm(new ExtractResourceWithChangesSCM(getClass().getResource("maven-multimod-incr.zip"), + getClass().getResource("maven-multimod-changes.zip"))); + + assertBuildStatus(Result.UNSTABLE, m.scheduleBuild2(0).get()); + MavenModuleSetBuild pBuild = m.getLastBuild(); + + for (MavenBuild modBuild : pBuild.getModuleLastBuilds().values()) { + String parentModuleName = modBuild.getParent().getModuleName().toString(); + if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleA")) { + assertEquals("moduleA should have Result.UNSTABLE", Result.UNSTABLE, modBuild.getResult()); + } + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleB")) { + assertEquals("moduleB should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleC")) { + assertEquals("moduleC should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleD")) { + assertEquals("moduleD should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + } + + // Now run a second build. + assertBuildStatus(Result.UNSTABLE, m.scheduleBuild2(0).get()); + + pBuild = m.getLastBuild(); + ExtractChangeLogSet changeSet = (ExtractChangeLogSet) pBuild.getChangeSet(); + + assertFalse("ExtractChangeLogSet should not be empty.", changeSet.isEmptySet()); + // changelog contains a change for module B + assertEquals("Parent build should have Result.UNSTABLE", Result.UNSTABLE, pBuild.getResult()); + + for (MavenBuild modBuild : pBuild.getModuleLastBuilds().values()) { + String parentModuleName = modBuild.getParent().getModuleName().toString(); + // A must be build again, because it was UNSTABLE before + if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleA")) { + assertEquals("moduleA should have Result.UNSTABLE", Result.UNSTABLE, modBuild.getResult()); + } + // B must be build, because it has changes + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleB")) { + assertEquals("moduleB should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + // C must be build, because it depends on B + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleC")) { + assertEquals("moduleC should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + // D must be build again, because it needs to be deployed now + else if (parentModuleName.equals("org.jvnet.hudson.main.test.multimod.incr:moduleD")) { + assertEquals("moduleD should have Result.SUCCESS", Result.SUCCESS, modBuild.getResult()); + } + } } /** @@ -329,4 +419,17 @@ public class MavenMultiModuleTest extends HudsonTestCase { return true; } } + + private static class DummyRedeployPublisher extends RedeployPublisher { + public DummyRedeployPublisher() { + super("", "", false, false); + } + + @Override + public boolean perform(AbstractBuild build, Launcher launcher, + BuildListener listener) throws InterruptedException, + IOException { + return true; + } + } }