This is an automated email from the ASF dual-hosted git repository.

rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-enforcer.git


The following commit(s) were added to refs/heads/master by this push:
     new ca73329  [MENFORCER-359] RequirePluginVersions fails when versions are 
inherited
ca73329 is described below

commit ca73329888b925899f4f57419a1d2ed208b1e0c4
Author: rfscholte <[email protected]>
AuthorDate: Sat Jul 17 10:32:38 2021 +0200

    [MENFORCER-359] RequirePluginVersions fails when versions are inherited
---
 .../plugins/enforcer/RequirePluginVersions.java    | 116 ++-------------------
 .../plugins/enforcer/utils/PluginWrapper.java      |  68 ++++++------
 .../enforcer/TestRequirePluginVersions.java        |   4 +-
 .../src/it/mrm/repository/menforcer359-1.0.pom     |  54 ++++++++++
 .../require-plugin-versions_inherit/pom.xml        |  57 ++++++++++
 5 files changed, 154 insertions(+), 145 deletions(-)

diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java
 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java
index 8f26a5e..e9e05ce 100644
--- 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/RequirePluginVersions.java
@@ -23,7 +23,6 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -168,9 +167,6 @@ public class RequirePluginVersions
     /** The local. */
     private ArtifactRepository local;
 
-    /** The remote repositories. */
-    private List<ArtifactRepository> remoteRepositories;
-
     /** The log. */
     private Log log;
 
@@ -202,7 +198,6 @@ public class RequirePluginVersions
             factory = helper.getComponent( ArtifactFactory.class );
             resolver = helper.getComponent( ArtifactResolver.class );
             local = (ArtifactRepository) helper.evaluate( "${localRepository}" 
);
-            remoteRepositories = project.getRemoteArtifactRepositories();
 
             utils = new EnforcerRuleUtils( helper );
 
@@ -234,7 +229,7 @@ public class RequirePluginVersions
             for ( PluginWrapper pluginWrapper : pluginWrappers )
             {
                 log.debug( "pluginWrappers: " + pluginWrapper.getGroupId() + 
":" + pluginWrapper.getArtifactId() + ":"
-                    + pluginWrapper.getVersion() + " source:" + 
pluginWrapper.getSource() );
+                    + pluginWrapper.getVersion() + " source: " + 
pluginWrapper.getSource() );
             }
             // now look for the versions that aren't valid and add to a list.
             List<Plugin> failures = new ArrayList<Plugin>();
@@ -721,17 +716,6 @@ public class RequirePluginVersions
             }
         }
 
-        List<String> mojos = findOptionalMojosForLifecycle( project, lifecycle 
);
-        for ( String value : mojos )
-        {
-            String tokens[] = value.split( ":" );
-
-            Plugin plugin = new Plugin();
-            plugin.setGroupId( tokens[0] );
-            plugin.setArtifactId( tokens[1] );
-            plugins.add( plugin );
-        }
-
         plugins.addAll( project.getBuildPlugins() );
 
         return plugins;
@@ -855,51 +839,6 @@ public class RequirePluginVersions
     }
 
     /**
-     * Find optional mojos for lifecycle.
-     *
-     * @param project the project
-     * @param lifecycle the lifecycle
-     * @return the list
-     * @throws LifecycleExecutionException the lifecycle execution exception
-     * @throws PluginNotFoundException the plugin not found exception
-     */
-    private List<String> findOptionalMojosForLifecycle( MavenProject project, 
Lifecycle lifecycle )
-        throws LifecycleExecutionException, PluginNotFoundException
-    {
-        String packaging = project.getPackaging();
-        List<String> optionalMojos = null;
-
-        LifecycleMapping m = (LifecycleMapping) findExtension( project, 
LifecycleMapping.ROLE, packaging,
-                                                               
session.getSettings(), session.getLocalRepository() );
-
-        if ( m != null )
-        {
-            optionalMojos = m.getOptionalMojos( lifecycle.getId() );
-        }
-
-        if ( optionalMojos == null )
-        {
-            try
-            {
-                m = helper.getComponent( LifecycleMapping.class, packaging );
-                optionalMojos = m.getOptionalMojos( lifecycle.getId() );
-            }
-            catch ( ComponentLookupException e )
-            {
-                log.debug( "Error looking up lifecycle mapping to retrieve 
optional mojos. Lifecycle ID: "
-                    + lifecycle.getId() + ". Error: " + e.getMessage(), e );
-            }
-        }
-
-        if ( optionalMojos == null )
-        {
-            optionalMojos = Collections.emptyList();
-        }
-
-        return optionalMojos;
-    }
-
-    /**
      * Find extension.
      *
      * @param project the project
@@ -998,45 +937,18 @@ public class RequirePluginVersions
     protected List<PluginWrapper> getAllPluginEntries( MavenProject project )
         throws ArtifactResolutionException, ArtifactNotFoundException, 
IOException, XmlPullParserException
     {
-        List<Model> models = new ArrayList<>();
-
-        List<MavenProject> sortedProjects = 
session.getProjectDependencyGraph().getSortedProjects();
-
-        if ( !sortedProjects.isEmpty() && sortedProjects.get( 0 ).getParent() 
!= null )
-        {
-            getOriginalModelFromAllParents( models, sortedProjects );
-        }
-
-        for ( MavenProject mavenProject : sortedProjects )
-        {
-            models.add( mavenProject.getOriginalModel() );
-        }
-
         List<PluginWrapper> plugins = new ArrayList<>();
         // now find all the plugin entries, either in
         // build.plugins or build.pluginManagement.plugins, profiles.plugins 
and reporting
-        for ( Model model : models )
-        {
-            getPlugins( plugins, model );
-            getReportingPlugins( plugins, model );
-            getPluginManagementPlugins( plugins, model );
 
-            addPluginsInProfiles( plugins, model );
-        }
+        getPlugins( plugins, project.getModel() );
+        getReportingPlugins( plugins, project.getModel() );
+        getPluginManagementPlugins( plugins, project.getModel() );
+        addPluginsInProfiles( plugins, project.getModel() );
 
         return plugins;
     }
 
-    private void getOriginalModelFromAllParents( List<Model> models, 
List<MavenProject> sortedProjects )
-    {
-        MavenProject parent = sortedProjects.get( 0 ).getParent();
-        do
-        {
-            models.add( parent.getOriginalModel() );
-            parent = parent.getParent();
-        }
-        while ( parent != null );
-    }
 
     private void addPluginsInProfiles( List<PluginWrapper> plugins, Model 
model )
     {
@@ -1054,8 +966,7 @@ public class RequirePluginVersions
         try
         {
             List<Plugin> modelPlugins = 
profile.getBuild().getPluginManagement().getPlugins();
-            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ), model.getId() + "profile["
-                + profile.getId() + "].build.pluginManagement.plugins" ) );
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ) ) );
         }
         catch ( NullPointerException e )
         {
@@ -1069,8 +980,7 @@ public class RequirePluginVersions
         {
             List<ReportPlugin> modelReportPlugins = 
profile.getReporting().getPlugins();
             // add the reporting plugins
-            plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( 
modelReportPlugins ), model.getId()
-                + "profile[" + profile.getId() + "].reporting.plugins" ) );
+            plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( 
modelReportPlugins ) ) );
         }
         catch ( NullPointerException e )
         {
@@ -1083,8 +993,7 @@ public class RequirePluginVersions
         try
         {
             List<Plugin> modelPlugins = profile.getBuild().getPlugins();
-            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ), model.getId()
-                + ".profiles.profile[" + profile.getId() + "].build.plugins" ) 
);
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ) ) );
         }
         catch ( NullPointerException e )
         {
@@ -1097,8 +1006,7 @@ public class RequirePluginVersions
         try
         {
             List<Plugin> modelPlugins = model.getBuild().getPlugins();
-            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ),
-                                                  model.getId() + 
".build.plugins" ) );
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ) ) );
         }
         catch ( NullPointerException e )
         {
@@ -1111,8 +1019,7 @@ public class RequirePluginVersions
         try
         {
             List<Plugin> modelPlugins = 
model.getBuild().getPluginManagement().getPlugins();
-            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ),
-                                                  model.getId() + 
".build.pluginManagement.plugins" ) );
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( 
modelPlugins ) ) );
         }
         catch ( NullPointerException e )
         {
@@ -1126,8 +1033,7 @@ public class RequirePluginVersions
         {
             List<ReportPlugin> modelReportPlugins = 
model.getReporting().getPlugins();
             // add the reporting plugins
-            plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( 
modelReportPlugins ),
-                                                  model.getId() + ".reporting" 
) );
+            plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( 
modelReportPlugins ) ) );
         }
         catch ( NullPointerException e )
         {
diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java
 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java
index 30690c4..b02befa 100644
--- 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java
+++ 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java
@@ -22,6 +22,8 @@ package org.apache.maven.plugins.enforcer.utils;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.maven.model.InputLocation;
+import org.apache.maven.model.InputLocationTracker;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.model.ReportPlugin;
 
@@ -31,15 +33,15 @@ import org.apache.maven.model.ReportPlugin;
  */
 public class PluginWrapper
 {
-    private String groupId;
+    private final String groupId;
 
-    private String artifactId;
+    private final String artifactId;
 
-    private String version;
+    private final String version;
 
-    private String source;
+    private final InputLocationTracker locationTracker;
 
-    public static List<PluginWrapper> addAll( List<?> plugins, String source )
+    public static List<PluginWrapper> addAll( List<?> plugins )
     {
         List<PluginWrapper> results = null;
 
@@ -50,13 +52,13 @@ public class PluginWrapper
             {
                 if ( o instanceof Plugin )
                 {
-                    results.add( new PluginWrapper( (Plugin) o, source ) );
+                    results.add( new PluginWrapper( (Plugin) o ) );
                 }
                 else
                 {
                     if ( o instanceof ReportPlugin )
                     {
-                        results.add( new PluginWrapper( (ReportPlugin) o, 
source ) );
+                        results.add( new PluginWrapper( (ReportPlugin) o ) );
                     }
                 }
 
@@ -65,20 +67,20 @@ public class PluginWrapper
         return results;
     }
 
-    public PluginWrapper( Plugin plugin, String source )
+    private PluginWrapper( Plugin plugin )
     {
-        setGroupId( plugin.getGroupId() );
-        setArtifactId( plugin.getArtifactId() );
-        setVersion( plugin.getVersion() );
-        setSource( source );
+        this.groupId = plugin.getGroupId();
+        this.artifactId = plugin.getArtifactId();
+        this.version = plugin.getVersion();
+        this.locationTracker = plugin;
     }
 
-    public PluginWrapper( ReportPlugin plugin, String source )
+    private PluginWrapper( ReportPlugin plugin )
     {
-        setGroupId( plugin.getGroupId() );
-        setArtifactId( plugin.getArtifactId() );
-        setVersion( plugin.getVersion() );
-        setSource( source );
+        this.groupId = plugin.getGroupId();
+        this.artifactId = plugin.getArtifactId();
+        this.version = plugin.getVersion();
+        this.locationTracker = plugin;
     }
 
     public String getGroupId()
@@ -86,38 +88,28 @@ public class PluginWrapper
         return groupId;
     }
 
-    public void setGroupId( String groupId )
-    {
-        this.groupId = groupId;
-    }
-
     public String getArtifactId()
     {
         return artifactId;
     }
 
-    public void setArtifactId( String artifactId )
-    {
-        this.artifactId = artifactId;
-    }
-
     public String getVersion()
     {
         return version;
     }
 
-    public void setVersion( String version )
-    {
-        this.version = version;
-    }
-
     public String getSource()
     {
-        return source;
-    }
-
-    public void setSource( String source )
-    {
-        this.source = source;
+        InputLocation inputLocation = locationTracker.getLocation( "version" );
+        
+        if ( inputLocation == null )
+        {
+            // most likely super-pom or default-lifecycle-bindings in Maven 
3.6.0 or before (MNG-6593 / MNG-6600)
+            return "unknown";
+        }
+        else
+        {
+            return inputLocation.getSource().getLocation();
+        }
     }
 }
diff --git 
a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java
 
b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java
index 41f5494..c2947ad 100644
--- 
a/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java
+++ 
b/enforcer-rules/src/test/java/org/apache/maven/plugins/enforcer/TestRequirePluginVersions.java
@@ -80,7 +80,7 @@ public class TestRequirePluginVersions
         plugins.add( EnforcerTestUtils.newPlugin( "group", "g-artifact", 
"1.0-12345678.123456-1" ) );
 
 
-        List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, 
"unit" );
+        List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins );
 
         RequirePluginVersions rule = new RequirePluginVersions();
         rule.setBanLatest( false );
@@ -145,7 +145,7 @@ public class TestRequirePluginVersions
         plugins.add( EnforcerTestUtils.newPlugin( "group", "e-artifact", "${}" 
) );
         plugins.add( EnforcerTestUtils.newPlugin( "group", "f-artifact", "${   
}" ) );
 
-        List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins, 
"unit" );
+        List<PluginWrapper> pluginWrappers = PluginWrapper.addAll( plugins );
 
         RequirePluginVersions rule = new RequirePluginVersions();
         rule.setBanLatest( false );
diff --git a/maven-enforcer-plugin/src/it/mrm/repository/menforcer359-1.0.pom 
b/maven-enforcer-plugin/src/it/mrm/repository/menforcer359-1.0.pom
new file mode 100644
index 0000000..38b9e01
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/mrm/repository/menforcer359-1.0.pom
@@ -0,0 +1,54 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  * 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. 
+  *
+-->
+<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/xsd/maven-4.0.0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.apache.maven.plugins.enforcer.its</groupId>
+  <artifactId>menforcer359</artifactId>
+  <version>1.0</version>
+  
+  <build>
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-clean-plugin</artifactId>
+          <version>3.1.0</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-deploy-plugin</artifactId>
+          <version>2.8.2</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-install-plugin</artifactId>
+          <version>2.5.2</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-site-plugin</artifactId>
+          <version>3.7.1</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+  
+</project>
\ No newline at end of file
diff --git 
a/maven-enforcer-plugin/src/it/projects/require-plugin-versions_inherit/pom.xml 
b/maven-enforcer-plugin/src/it/projects/require-plugin-versions_inherit/pom.xml
new file mode 100644
index 0000000..6208ba2
--- /dev/null
+++ 
b/maven-enforcer-plugin/src/it/projects/require-plugin-versions_inherit/pom.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+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.
+-->
+
+<project>
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.its.enforcer</groupId>
+  <artifactId>test</artifactId>
+  <version>1.0</version>
+  <packaging>pom</packaging>
+  
+  <url>https://issues.apache.org/jira/browse/MENFORCER-359</url>
+
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>test</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requirePluginVersions>
+                  <banSnapshots>false</banSnapshots> <!-- 
maven-enforcer-plugin -->
+                  
<unCheckedPluginList>org.apache.maven.plugins:maven-wrapper-plugin</unCheckedPluginList>
+                </requirePluginVersions>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+  </build>
+</project>

Reply via email to