From bc3cdccb68ba5af11c074730991abf7e9c69f140 Mon Sep 17 00:00:00 2001 From: Martin Kanters Date: Sat, 24 Oct 2020 11:59:41 +0200 Subject: [PATCH] [MNG-6566] Plugins that require a certain phase should not fork goals that are already in the execution plan. This resolves #74 --- .../apache/maven/it/IntegrationTestSuite.java | 1 + ...AnnotationShouldNotReExecuteGoalsTest.java | 102 ++++++++++++++++++ .../consumer/pom.xml | 54 ++++++++++ .../plugin/pom.xml | 52 +++++++++ .../testmojo/RequiresCompilePhaseMojo.java | 37 +++++++ 5 files changed, 246 insertions(+) create mode 100644 its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6566ExecuteAnnotationShouldNotReExecuteGoalsTest.java create mode 100644 its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/consumer/pom.xml create mode 100644 its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/pom.xml create mode 100644 its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/src/main/java/testmojo/RequiresCompilePhaseMojo.java diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java b/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java index 8238b152d9..5c78f9ab4c 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java @@ -107,6 +107,7 @@ public static Test suite() // ------------------------------------------------------------------------------------------------------------- // suite.addTestSuite( MavenIT0108SnapshotUpdateTest.class ); -- MNG-3137 + suite.addTestSuite( MavenITmng6566ExecuteAnnotationShouldNotReExecuteGoalsTest.class ); suite.addTestSuite( MavenITmng6754TimestampInMultimoduleProject.class ); suite.addTestSuite( MavenITmng6981ProjectListShouldIncludeChildrenTest.class ); suite.addTestSuite( MavenITmng6972AllowAccessToGraphPackageTest.class ); diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6566ExecuteAnnotationShouldNotReExecuteGoalsTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6566ExecuteAnnotationShouldNotReExecuteGoalsTest.java new file mode 100644 index 0000000000..ef88b1160d --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng6566ExecuteAnnotationShouldNotReExecuteGoalsTest.java @@ -0,0 +1,102 @@ +package org.apache.maven.it; + +/* + * 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. + */ + +import org.apache.maven.it.util.ResourceExtractor; + +import java.io.File; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +public class MavenITmng6566ExecuteAnnotationShouldNotReExecuteGoalsTest + extends AbstractMavenIntegrationTestCase +{ + private static final String RESOURCE_PATH = "/mng-6566-execute-annotation-should-not-re-execute-goals"; + private static final String PLUGIN_KEY = "org.apache.maven.its.mng6566:plugin:1.0-SNAPSHOT"; + + private File testDir; + + public MavenITmng6566ExecuteAnnotationShouldNotReExecuteGoalsTest() + { + super( "[4.0.0-alpha-1,)" ); + } + + public void setUp() + throws Exception + { + testDir = ResourceExtractor.simpleExtractResources( getClass(), RESOURCE_PATH ); + + File pluginDir = new File( testDir, "plugin" ); + Verifier verifier = newVerifier( pluginDir.getAbsolutePath(), "remote" ); + verifier.executeGoal( "install" ); + verifier.resetStreams(); + verifier.verifyErrorFreeLog(); + } + + public void testRunsCompileGoalOnceWithDirectPluginInvocation() + throws Exception + { + File consumerDir = new File( testDir, "consumer" ); + + Verifier verifier = newVerifier( consumerDir.getAbsolutePath() ); + verifier.setLogFileName( "log-direct-plugin-invocation.txt" ); + verifier.executeGoal( PLUGIN_KEY + ":require-compile-phase" ); + verifier.resetStreams(); + verifier.verifyErrorFreeLog(); + + assertCompiledOnce( verifier ); + verifier.verifyTextInLog( "MNG-6566 plugin require-compile-phase goal executed" ); + } + + /** + * This test uses the
require-compile-phase
goal of the test plugin. + */ + public void testRunsCompileGoalOnceWithPhaseExecution() + throws Exception + { + File consumerDir = new File( testDir, "consumer" ); + + Verifier verifier = newVerifier( consumerDir.getAbsolutePath() ); + verifier.setLogFileName( "log-phase-execution.txt" ); + verifier.executeGoal( "compile" ); + verifier.resetStreams(); + verifier.verifyErrorFreeLog(); + + assertCompiledOnce( verifier ); + verifier.verifyTextInLog( "MNG-6566 plugin require-compile-phase goal executed" ); + } + + private void assertCompiledOnce( Verifier verifier ) + throws VerificationException + { + List lines = verifier.loadFile( verifier.getBasedir(), verifier.getLogFileName(), false ); + int counter = 0; + for ( String line : lines ) + { + if ( line.contains( "maven-compiler-plugin:0.1-stub-SNAPSHOT:compile") ) + { + counter++; + } + } + assertThat( "Compile goal was expected to run once", counter, is( 1 ) ); + } +} diff --git a/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/consumer/pom.xml b/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/consumer/pom.xml new file mode 100644 index 0000000000..07579f561f --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/consumer/pom.xml @@ -0,0 +1,54 @@ + + + + + + 4.0.0 + + org.apache.maven.its.mng6566 + consumer + 1.0-SNAPSHOT + + + + + org.apache.maven.its.mng6566 + plugin + 1.0-SNAPSHOT + + + + require-compile-phase + + compile + + + + + + + + maven-compiler-plugin + 0.1-stub-SNAPSHOT + + + + + \ No newline at end of file diff --git a/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/pom.xml b/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/pom.xml new file mode 100644 index 0000000000..7971a94f08 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/pom.xml @@ -0,0 +1,52 @@ + + + + + + 4.0.0 + + org.apache.maven.its.mng6566 + plugin + 1.0-SNAPSHOT + maven-plugin + + + 3.6.0 + 1.7 + 1.7 + + + + + org.apache.maven.plugin-tools + maven-plugin-annotations + ${maven-version} + provided + + + org.apache.maven + maven-plugin-api + ${maven-version} + provided + + + + \ No newline at end of file diff --git a/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/src/main/java/testmojo/RequiresCompilePhaseMojo.java b/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/src/main/java/testmojo/RequiresCompilePhaseMojo.java new file mode 100644 index 0000000000..d469e93bc3 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-6566-execute-annotation-should-not-re-execute-goals/plugin/src/main/java/testmojo/RequiresCompilePhaseMojo.java @@ -0,0 +1,37 @@ +package testmojo; + +/* + * 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. + */ + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Execute; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; + +@Mojo( name = "require-compile-phase" ) +@Execute( phase = LifecyclePhase.COMPILE ) +public class RequiresCompilePhaseMojo extends AbstractMojo +{ + public void execute() throws MojoExecutionException, MojoFailureException + { + getLog().info( "MNG-6566 plugin require-compile-phase goal executed" ); + } +}