From b5fbd2340e3f3d87746bfcb95eca48fa3f043bc0 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 20 Oct 2022 09:21:32 +0200 Subject: [PATCH] [MINSTALL-179] Fix installAtEnd when module does not use m-install-p --- src/it/install-at-end-pass/module1/pom.xml | 23 -------- src/it/install-at-end-pass/module2/pom.xml | 39 +++++++++++++ src/it/install-at-end-pass/module3/pom.xml | 24 ++++++++ src/it/install-at-end-pass/module4/pom.xml | 38 +++++++++++++ src/it/install-at-end-pass/pom.xml | 3 + src/it/install-at-end-pass/verify.groovy | 3 + .../invoker.properties | 18 ++++++ src/it/setup-mock-phase-maven-plugin/pom.xml | 57 +++++++++++++++++++ .../resources/META-INF/plexus/components.xml | 40 +++++++++++++ .../maven/plugins/install/InstallMojo.java | 43 ++++++++++++-- .../plugins/install/InstallMojoTest.java | 17 ++++-- 11 files changed, 272 insertions(+), 33 deletions(-) create mode 100644 src/it/install-at-end-pass/module2/pom.xml create mode 100644 src/it/install-at-end-pass/module3/pom.xml create mode 100644 src/it/install-at-end-pass/module4/pom.xml create mode 100644 src/it/setup-mock-phase-maven-plugin/invoker.properties create mode 100644 src/it/setup-mock-phase-maven-plugin/pom.xml create mode 100644 src/it/setup-mock-phase-maven-plugin/src/main/resources/META-INF/plexus/components.xml diff --git a/src/it/install-at-end-pass/module1/pom.xml b/src/it/install-at-end-pass/module1/pom.xml index 904addf4..31279ce7 100644 --- a/src/it/install-at-end-pass/module1/pom.xml +++ b/src/it/install-at-end-pass/module1/pom.xml @@ -21,27 +21,4 @@ 1.0 module1 - - - - - org.apache.maven.plugins - maven-enforcer-plugin - 1.2 - - - enforce - - enforce - - - - - - - - - - - diff --git a/src/it/install-at-end-pass/module2/pom.xml b/src/it/install-at-end-pass/module2/pom.xml new file mode 100644 index 00000000..f78eb0db --- /dev/null +++ b/src/it/install-at-end-pass/module2/pom.xml @@ -0,0 +1,39 @@ + + + + + + 4.0.0 + + + org.apache.maven.its.install.dae.pass + dae + 1.0 + + module2 + + + + + org.apache.maven.plugins + maven-install-plugin + + + default-install + none + + + + + + diff --git a/src/it/install-at-end-pass/module3/pom.xml b/src/it/install-at-end-pass/module3/pom.xml new file mode 100644 index 00000000..2d346dbe --- /dev/null +++ b/src/it/install-at-end-pass/module3/pom.xml @@ -0,0 +1,24 @@ + + + + + + 4.0.0 + + + org.apache.maven.its.install.dae.pass + dae + 1.0 + + module3 + diff --git a/src/it/install-at-end-pass/module4/pom.xml b/src/it/install-at-end-pass/module4/pom.xml new file mode 100644 index 00000000..cb4ca5fd --- /dev/null +++ b/src/it/install-at-end-pass/module4/pom.xml @@ -0,0 +1,38 @@ + + + + + + 4.0.0 + + + org.apache.maven.its.install.dae.pass + dae + 1.0 + + module4 + + + without-install + + + + + org.apache.maven.its.install.mock + mock-phase-maven-plugin + 1.0 + true + + + + diff --git a/src/it/install-at-end-pass/pom.xml b/src/it/install-at-end-pass/pom.xml index 39d1e6da..5149bb7c 100644 --- a/src/it/install-at-end-pass/pom.xml +++ b/src/it/install-at-end-pass/pom.xml @@ -71,6 +71,9 @@ under the License. module1 + module2 + module3 + module4 diff --git a/src/it/install-at-end-pass/verify.groovy b/src/it/install-at-end-pass/verify.groovy index f1b7e513..2d4ea4b6 100644 --- a/src/it/install-at-end-pass/verify.groovy +++ b/src/it/install-at-end-pass/verify.groovy @@ -19,8 +19,11 @@ assert new File( basedir, "../../local-repo/org/apache/maven/its/install/dae/pass/dae/1.0/dae-1.0.pom" ).exists() assert new File( basedir, "../../local-repo/org/apache/maven/its/install/dae/pass/module1/1.0/module1-1.0.pom" ).exists() +assert new File( basedir, "../../local-repo/org/apache/maven/its/install/dae/pass/module3/1.0/module3-1.0.pom" ).exists() File buildLog = new File( basedir, 'build.log' ) assert buildLog.exists() assert buildLog.text.contains( "[INFO] Deferring install for org.apache.maven.its.install.dae.pass:dae:1.0 at end" ) +assert buildLog.text.contains( "[INFO] Deferring install for org.apache.maven.its.install.dae.pass:module1:1.0 at end" ) +assert buildLog.text.contains( "[INFO] Deferring install for org.apache.maven.its.install.dae.pass:module3:1.0 at end" ) diff --git a/src/it/setup-mock-phase-maven-plugin/invoker.properties b/src/it/setup-mock-phase-maven-plugin/invoker.properties new file mode 100644 index 00000000..d5d6ca5d --- /dev/null +++ b/src/it/setup-mock-phase-maven-plugin/invoker.properties @@ -0,0 +1,18 @@ +# 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. + +invoker.goals = install diff --git a/src/it/setup-mock-phase-maven-plugin/pom.xml b/src/it/setup-mock-phase-maven-plugin/pom.xml new file mode 100644 index 00000000..d0d55957 --- /dev/null +++ b/src/it/setup-mock-phase-maven-plugin/pom.xml @@ -0,0 +1,57 @@ + + + + + + 4.0.0 + + org.apache.maven.its.install.mock + mock-phase-maven-plugin + 1.0 + maven-plugin + + + @mavenVersion@ + + + plugin with Maven phase without m-install-p + + + + + + org.apache.maven.plugins + maven-install-plugin + @project.version@ + + + org.apache.maven.plugins + maven-plugin-plugin + 3.6.4 + + true + + + + + + + diff --git a/src/it/setup-mock-phase-maven-plugin/src/main/resources/META-INF/plexus/components.xml b/src/it/setup-mock-phase-maven-plugin/src/main/resources/META-INF/plexus/components.xml new file mode 100644 index 00000000..55eb8926 --- /dev/null +++ b/src/it/setup-mock-phase-maven-plugin/src/main/resources/META-INF/plexus/components.xml @@ -0,0 +1,40 @@ + + + + + + + + org.apache.maven.lifecycle.mapping.LifecycleMapping + without-install + org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping + + + + default + + org.apache.maven.plugins:maven-resources-plugin:resources + + + + + + + diff --git a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java index a6c694b3..5f062616 100644 --- a/src/main/java/org/apache/maven/plugins/install/InstallMojo.java +++ b/src/main/java/org/apache/maven/plugins/install/InstallMojo.java @@ -20,11 +20,14 @@ */ import java.io.File; +import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.maven.RepositoryUtils; import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Plugin; +import org.apache.maven.model.PluginExecution; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -134,9 +137,11 @@ public void execute() } } - if ( allProjectsMarked() ) + List allProjectsUsingPlugin = getAllProjectsUsingPlugin(); + + if ( allProjectsMarked( allProjectsUsingPlugin ) ) { - for ( MavenProject reactorProject : reactorProjects ) + for ( MavenProject reactorProject : allProjectsUsingPlugin ) { State state = getState( reactorProject ); if ( state == State.TO_BE_INSTALLED ) @@ -147,9 +152,9 @@ public void execute() } } - private boolean allProjectsMarked() + private boolean allProjectsMarked( List allProjectsUsingPlugin ) { - for ( MavenProject reactorProject : reactorProjects ) + for ( MavenProject reactorProject : allProjectsUsingPlugin ) { if ( !hasState( reactorProject ) ) { @@ -159,6 +164,36 @@ private boolean allProjectsMarked() return true; } + private List getAllProjectsUsingPlugin() + { + ArrayList result = new ArrayList<>(); + for ( MavenProject reactorProject : reactorProjects ) + { + if ( hasExecution( reactorProject.getPlugin( "org.apache.maven.plugins:maven-install-plugin" ) ) ) + { + result.add( reactorProject ); + } + } + return result; + } + + private boolean hasExecution( Plugin plugin ) + { + if ( plugin == null ) + { + return false; + } + + for ( PluginExecution execution : plugin.getExecutions() ) + { + if ( !execution.getGoals().isEmpty() && !"none".equalsIgnoreCase( execution.getPhase() ) ) + { + return true; + } + } + return false; + } + private void installProject( MavenProject project ) throws MojoExecutionException, MojoFailureException { try diff --git a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java index 57fe471c..04c26e27 100644 --- a/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java +++ b/src/test/java/org/apache/maven/plugins/install/InstallMojoTest.java @@ -19,10 +19,6 @@ * under the License. */ -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - import java.io.File; import java.util.Collections; import java.util.List; @@ -31,8 +27,9 @@ import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.metadata.ArtifactMetadata; import org.apache.maven.execution.MavenSession; +import org.apache.maven.model.Build; +import org.apache.maven.model.Plugin; import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugin.testing.AbstractMojoTestCase; @@ -48,10 +45,13 @@ import org.eclipse.aether.repository.LocalRepository; import org.eclipse.aether.repository.NoLocalRepositoryManagerException; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + /** * @author Allan Ramirez */ - public class InstallMojoTest extends AbstractMojoTestCase { @@ -381,5 +381,10 @@ private void updateMavenProject( MavenProject project ) project.setGroupId( project.getArtifact().getGroupId() ); project.setArtifactId( project.getArtifact().getArtifactId() ); project.setVersion( project.getArtifact().getVersion() ); + + Plugin plugin = new Plugin(); + plugin.setArtifactId( "maven-install-plugin" ); + project.setBuild( new Build() ); + project.getBuild().addPlugin( plugin ); } }