Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,10 @@ private void executePlan() {
} catch (Exception e) {
step.status.compareAndSet(SCHEDULED, FAILED);
global.stop();

// Find and execute all pending after:* phases for this project
executeAfterPhases(step);

handleBuildError(reactorContext, session, step.project, e, global);
}
});
Expand All @@ -352,6 +356,43 @@ private void executePlan() {
}
}

private void executeAfterPhases(BuildStep failedStep) {
if (failedStep == null || failedStep.project == null) {
return;
}

lock.readLock().lock();
try {
// Find all after:* phases that should be executed
plan.steps(failedStep.project)
.filter(step -> step.name != null && step.name.startsWith(AFTER))
.filter(step -> step.status.get() == CREATED)
.filter(step -> {
// Only execute after:xxx if before:xxx has been executed or failed
String phaseName = step.name.substring(AFTER.length());
return plan.step(failedStep.project, BEFORE + phaseName)
.map(s -> {
int status = s.status.get();
return status == EXECUTED || status == FAILED;
})
.orElse(false);
})
.filter(step -> step.status.compareAndSet(CREATED, SCHEDULED))
.forEach(afterStep -> {
try {
executeStep(afterStep);
afterStep.status.compareAndSet(SCHEDULED, EXECUTED);
} catch (Exception e) {
// Log but don't fail - we're already in error handling
logger.error("Error executing cleanup phase " + afterStep.name, e);
afterStep.status.compareAndSet(SCHEDULED, FAILED);
}
});
} finally {
lock.readLock().unlock();
}
}

private void executeStep(BuildStep step) throws IOException, LifecycleExecutionException {
Clock clock = getClock(step.project);
switch (step.name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.maven.it;

import java.io.File;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* This is a test for MNG-5668:
* Verifies that after:xxx phases are executed even when the build fails
*/
class MavenITmng5668AfterPhaseExecutionTest extends AbstractMavenIntegrationTestCase {

MavenITmng5668AfterPhaseExecutionTest() {
super("[4.0.0-rc-4,)"); // test is only relevant for Maven 4.0+
}

@Test
void testAfterPhaseExecutionOnFailure() throws Exception {
File testDir = extractResources("/mng-5668-after-phase-execution");

Verifier verifier = newVerifier(testDir.getAbsolutePath());
verifier.setAutoclean(false);
verifier.deleteDirectory("target");

try {
verifier.addCliArgument("-b");
verifier.addCliArgument("concurrent");
verifier.addCliArgument("verify");
verifier.execute();
fail("Build should have failed");
} catch (VerificationException e) {
// expected
}

// Verify that marker files were created in the expected order
verifier.verifyFilePresent("target/before-verify.txt");
verifier.verifyFilePresent("target/verify-failed.txt");
verifier.verifyFilePresent("target/after-verify.txt");

// Verify the execution order through timestamps
long beforeTime = new File(testDir, "target/before-verify.txt").lastModified();
long failTime = new File(testDir, "target/verify-failed.txt").lastModified();
long afterTime = new File(testDir, "target/after-verify.txt").lastModified();

assertTrue(beforeTime <= failTime);
assertTrue(failTime <= afterTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public TestSuiteOrdering() {
* the tests are to finishing. Newer tests are also more likely to fail, so this is
* a fail fast technique as well.
*/
suite.addTestSuite(MavenITmng5668AfterPhaseExecutionTest.class);
suite.addTestSuite(MavenITmng8648ProjectStartedEventsTest.class);
suite.addTestSuite(MavenITmng8645ConsumerPomDependencyManagementTest.class);
suite.addTestSuite(MavenITmng8594AtFileTest.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.mng5668</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<name>Test</name>
<description>Test that verifies after:xxx phase execution when build fails</description>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>before-verify</id>
<goals>
<goal>run</goal>
</goals>
<phase>before:verify</phase>
<configuration>
<target>
<touch file="${project.build.directory}/before-verify.txt" />
</target>
</configuration>
</execution>
<execution>
<id>verify</id>
<goals>
<goal>run</goal>
</goals>
<phase>verify</phase>
<configuration>
<target>
<touch file="${project.build.directory}/verify-failed.txt" />
<fail message="Intentionally failing the verify phase" />
</target>
</configuration>
</execution>
<execution>
<id>after-verify</id>
<goals>
<goal>run</goal>
</goals>
<phase>after:verify</phase>
<configuration>
<target>
<touch file="${project.build.directory}/after-verify.txt" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>