Skip to content

Commit

Permalink
[MNG-8245][MNG-8246] Warn when calling before: or after: phases
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Dec 13, 2024
1 parent a8ed59d commit 585bb12
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.Lifecycle;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.LifecycleNotFoundException;
import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
Expand All @@ -39,6 +40,8 @@
import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException;
import org.apache.maven.plugin.version.PluginVersionResolutionException;
import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Objects.requireNonNull;

Expand All @@ -53,6 +56,8 @@
@Named
@Singleton
public class DefaultLifecycleTaskSegmentCalculator implements LifecycleTaskSegmentCalculator {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLifecycleTaskSegmentCalculator.class);

private final MojoDescriptorCreator mojoDescriptorCreator;

private final LifecyclePluginResolver lifecyclePluginResolver;
Expand Down Expand Up @@ -95,6 +100,11 @@ public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String
TaskSegment currentSegment = null;

for (String task : tasks) {
if (isBeforeOrAfterPhase(task)) {
String prevTask = task;
task = PhaseId.of(task).phase();
LOGGER.warn("Illegal call to phase '{}'. The main phase '{}' will be used instead.", prevTask, task);
}
if (isGoalSpecification(task)) {
// "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"

Expand Down Expand Up @@ -139,6 +149,10 @@ public boolean requiresProject(MavenSession session) {
return false;
}

private boolean isBeforeOrAfterPhase(String task) {
return task.startsWith(Lifecycle.BEFORE) || task.startsWith(Lifecycle.AFTER);
}

private boolean isGoalSpecification(String task) {
return task.indexOf(':') >= 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.maven.api.Lifecycle;
import org.apache.maven.execution.ExecutionEvent;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.lifecycle.DefaultLifecycles;
Expand All @@ -38,6 +39,7 @@
import org.apache.maven.lifecycle.internal.LifecycleStarter;
import org.apache.maven.lifecycle.internal.LifecycleTask;
import org.apache.maven.lifecycle.internal.MojoDescriptorCreator;
import org.apache.maven.lifecycle.internal.PhaseId;
import org.apache.maven.lifecycle.internal.ReactorBuildStatus;
import org.apache.maven.lifecycle.internal.ReactorContext;
import org.apache.maven.lifecycle.internal.TaskSegment;
Expand Down Expand Up @@ -138,6 +140,11 @@ public List<TaskSegment> calculateTaskSegments(MavenSession session, List<String
TaskSegment currentSegment = null;

for (String task : tasks) {
if (isBeforeOrAfterPhase(task)) {
String prevTask = task;
task = PhaseId.of(task).phase();
logger.warn("Illegal call to phase '{}'. The main phase '{}' will be used instead.", prevTask, task);
}
if (isGoalSpecification(task)) {
// "pluginPrefix[:version]:goal" or "groupId:artifactId[:version]:goal"

Expand Down Expand Up @@ -185,6 +192,10 @@ private boolean requiresProject(MavenSession session) {
return false;
}

private boolean isBeforeOrAfterPhase(String task) {
return task.startsWith(Lifecycle.BEFORE) || task.startsWith(Lifecycle.AFTER);
}

private boolean isGoalSpecification(String task) {
return task.indexOf(':') >= 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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;

/**
* This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-8245">MNG-8245</a>
* and <a href="https://issues.apache.org/jira/browse/MNG-8246">MNG-8246</a>.
*/
class MavenITmng8245BeforePhaseCliTest extends AbstractMavenIntegrationTestCase {

MavenITmng8245BeforePhaseCliTest() {
super("[4.0.0-rc-2,)");
}

/**
* Verify phase before:clean spits a warning and calls clean
*/
@Test
void testPhaseBeforeCleanAllWihConcurrentBuilder() throws Exception {
File testDir = extractResources("/mng-8245-before-after-phase-all");

Verifier verifier = newVerifier(testDir.getAbsolutePath());
verifier.setLogFileName("before-clean-concurrent.txt");
verifier.addCliArguments("-b", "concurrent", "before:clean");
verifier.execute();

verifier.verifyTextInLog("Illegal call to phase 'before:clean'. The main phase 'clean' will be used instead.");
verifier.verifyTextInLog("Hallo 'before:clean' phase.");
verifier.verifyTextInLog("Hallo 'after:clean' phase.");
}

/**
* Verify phase before:clean spits a warning and calls clean
*/
@Test
void testPhaseBeforeCleanAllWithLegacyBuilder() throws Exception {
File testDir = extractResources("/mng-8245-before-after-phase-all");

Verifier verifier = newVerifier(testDir.getAbsolutePath());
verifier.setLogFileName("before-clean-legacy.txt");
verifier.addCliArguments("before:clean");
verifier.execute();

verifier.verifyTextInLog("Illegal call to phase 'before:clean'. The main phase 'clean' will be used instead.");
verifier.verifyTextInLog("Hallo 'before:clean' phase.");
verifier.verifyTextInLog("Hallo 'after:clean' phase.");
}

/**
* Verify phase after:clean spits a warning and calls clean
*/
@Test
void testPhaseAfterCleanAllWihConcurrentBuilder() throws Exception {
File testDir = extractResources("/mng-8245-before-after-phase-all");

Verifier verifier = newVerifier(testDir.getAbsolutePath());
verifier.setLogFileName("after-clean-concurrent.txt");
verifier.addCliArguments("-b", "concurrent", "after:clean");
verifier.execute();

verifier.verifyTextInLog("Illegal call to phase 'after:clean'. The main phase 'clean' will be used instead.");
verifier.verifyTextInLog("Hallo 'before:clean' phase.");
verifier.verifyTextInLog("Hallo 'after:clean' phase.");
}

/**
* Verify phase after:clean spits a warning and calls clean
*/
@Test
void testPhaseAfterCleanAllWithLegacyBuilder() throws Exception {
File testDir = extractResources("/mng-8245-before-after-phase-all");

Verifier verifier = newVerifier(testDir.getAbsolutePath());
verifier.setLogFileName("after-clean-legacy.txt");
verifier.addCliArguments("after:clean");
verifier.execute();

verifier.verifyTextInLog("Illegal call to phase 'after:clean'. The main phase 'clean' will be used instead.");
verifier.verifyTextInLog("Hallo 'before:clean' phase.");
verifier.verifyTextInLog("Hallo 'after:clean' phase.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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(MavenITmng8245BeforePhaseCliTest.class);
suite.addTestSuite(MavenITmng8244PhaseAllTest.class);
suite.addTestSuite(MavenITmng8421MavenEncryptionTest.class);
suite.addTestSuite(MavenITmng8400CanonicalMavenHomeTest.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" root="true" xsi:schemaLocation="http://maven.apache.org/POM/4.1.0 https://maven.apache.org/xsd/maven-4.1.0.xsd">

<groupId>org.apahce.maven.its</groupId>
<artifactId>mng-8245</artifactId>
<version>1.0.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>echo-maven-plugin</artifactId>
<version>0.5.0</version>
<executions>
<execution>
<id>before-clean</id>
<goals>
<goal>echo</goal>
</goals>
<phase>before:clean</phase>
<configuration>
<echos>
<echo>Hallo 'before:clean' phase.</echo>
</echos>
</configuration>
</execution>
<execution>
<id>after-clean</id>
<goals>
<goal>echo</goal>
</goals>
<phase>after:clean</phase>
<configuration>
<echos>
<echo>Hallo 'after:clean' phase.</echo>
</echos>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>

</project>

0 comments on commit 585bb12

Please sign in to comment.