You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by kh...@apache.org on 2018/09/02 19:38:56 UTC

[maven-enforcer] 01/01: WIP - [MENFORCER-306] - [REGRESSION] RequirePluginVersions fails o Added some debugging output o Added first implementation which is not final yet. o Added simple IT's for this case. o Make it Maven 3.0.5 compatible. o Changed IT's according to parameter definition with List.

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

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

commit 1549f75d48d79f5158e2b5daad2f843eb5f84143
Author: Karl Heinz Marbaise <kh...@apache.org>
AuthorDate: Thu Aug 16 22:31:13 2018 +0200

    WIP - [MENFORCER-306] - [REGRESSION] RequirePluginVersions fails
     o Added some debugging output
     o Added first implementation which is not final yet.
     o Added simple IT's for this case.
     o Make it Maven 3.0.5 compatible.
     o Changed IT's according to parameter definition with List.
---
 .../plugins/enforcer/RequirePluginVersions.java    | 187 ++++++++++++---------
 .../enforcer/TestRequirePluginVersions.java        |   8 -
 maven-enforcer-plugin/pom.xml                      |   3 +
 .../src/it/projects/MENFORCER-306/parent/pom.xml   | 103 ++++++++++++
 .../src/it/projects/MENFORCER-306/pom.xml          |  31 ++++
 .../pom.xml                                        |   4 +-
 .../require-no-repositories-allow-repo/pom.xml     |   4 +-
 .../pom.xml                                        |   5 +-
 .../pom.xml                                        |   4 +-
 9 files changed, 262 insertions(+), 87 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 bae55ec..6277a59 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
@@ -268,7 +268,7 @@ public class RequirePluginVersions
                     + plugin.getVersion() );
                 if ( !hasValidVersionSpecified( helper, plugin, pluginWrappers ) )
                 {
-                    log.debug( "hasValidVersionSpecified(1): " + plugin.getGroupId() + ":" + plugin.getArtifactId()
+                    log.debug( "hasValidVersionSpecified(2): " + plugin.getGroupId() + ":" + plugin.getArtifactId()
                         + ":" + plugin.getVersion() );
                     failures.add( plugin );
                 }
@@ -662,7 +662,7 @@ public class RequirePluginVersions
 
                 if ( StringUtils.isNotEmpty( version ) && !StringUtils.isWhitespace( version ) )
                 {
-                    log.debug( "checking for notEmpty and notIsWhiespace(): " + version );
+                    helper.getLog().debug( "checking for notEmpty and notIsWhiespace(): " + version );
                     if ( banRelease && version.equals( "RELEASE" ) )
                     {
                         return false;
@@ -693,7 +693,7 @@ public class RequirePluginVersions
         }
         if ( !found )
         {
-            log.debug( "plugin " + source.getGroupId() + ":" + source.getArtifactId() + " not found" );
+            helper.getLog().debug( "plugin " + source.getGroupId() + ":" + source.getArtifactId() + " not found" );
         }
         return status;
     }
@@ -1044,6 +1044,7 @@ public class RequirePluginVersions
         return pluginDescriptor;
     }
 
+    
     /**
      * Gets all plugin entries in build.plugins, build.pluginManagement.plugins, profile.build.plugins, reporting and
      * profile.reporting in this project and all parents
@@ -1058,96 +1059,132 @@ public class RequirePluginVersions
     protected List<PluginWrapper> getAllPluginEntries( MavenProject project )
         throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
     {
-        List<PluginWrapper> plugins = new ArrayList<PluginWrapper>();
-        // get all the pom models
-
         List<Model> models = new ArrayList<Model>();
 
         List<MavenProject> sortedProjects = session.getProjectDependencyGraph().getSortedProjects();
+
+        if ( sortedProjects.size() > 0 && sortedProjects.get( 0 ).getParent() != null )
+        {
+            MavenProject mp = sortedProjects.get( 0 ).getParent();
+            models.add( mp.getOriginalModel() );
+        }
+
         for ( MavenProject mavenProject : sortedProjects )
         {
             models.add( mavenProject.getOriginalModel() );
         }
 
+        List<PluginWrapper> plugins = new ArrayList<PluginWrapper>();
+        
         // now find all the plugin entries, either in
         // build.plugins or build.pluginManagement.plugins, profiles.plugins and reporting
         for ( Model model : models )
         {
-            try
-            {
-                List<Plugin> modelPlugins = model.getBuild().getPlugins();
-                plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ),
-                                                      model.getId() + ".build.plugins" ) );
-            }
-            catch ( NullPointerException e )
-            {
-                // guess there are no plugins here.
-            }
+            getPlugins( plugins, model );
+            getReportingPlugins( plugins, model );
+            getPluginManagementPlugins( plugins, model );
 
-            try
-            {
-                List<ReportPlugin> modelReportPlugins = model.getReporting().getPlugins();
-                // add the reporting plugins
-                plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ),
-                                                      model.getId() + ".reporting" ) );
-            }
-            catch ( NullPointerException e )
-            {
-                // guess there are no plugins here.
-            }
+            addPluginsInProfiles( plugins, model );
+        }
 
-            try
-            {
-                List<Plugin> modelPlugins = model.getBuild().getPluginManagement().getPlugins();
-                plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ),
-                                                      model.getId() + ".build.pluginManagement.plugins" ) );
-            }
-            catch ( NullPointerException e )
-            {
-                // guess there are no plugins here.
-            }
+        return plugins;
+    }
 
-            // Add plugins in profiles
-            List<Profile> profiles = model.getProfiles();
-            for ( Profile profile : profiles )
-            {
-                try
-                {
-                    List<Plugin> modelPlugins = profile.getBuild().getPlugins();
-                    plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), model.getId()
-                        + ".profiles.profile[" + profile.getId() + "].build.plugins" ) );
-                }
-                catch ( NullPointerException e )
-                {
-                    // guess there are no plugins here.
-                }
+    private void addPluginsInProfiles( List<PluginWrapper> plugins, Model model )
+    {
+        List<Profile> profiles = model.getProfiles();
+        for ( Profile profile : profiles )
+        {
+            getProfilePlugins( plugins, model, profile );
+            getProfileReportingPlugins( plugins, model, profile );
+            getProfilePluginManagementPlugins( plugins, model, profile );
+        }
+    }
 
-                try
-                {
-                    List<ReportPlugin> modelReportPlugins = profile.getReporting().getPlugins();
-                    // add the reporting plugins
-                    plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ),
-                                                          model.getId() + "profile[" + profile.getId()
-                                                              + "].reporting.plugins" ) );
-                }
-                catch ( NullPointerException e )
-                {
-                    // guess there are no plugins here.
-                }
-                try
-                {
-                    List<Plugin> modelPlugins = profile.getBuild().getPluginManagement().getPlugins();
-                    plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), model.getId()
-                        + "profile[" + profile.getId() + "].build.pluginManagement.plugins" ) );
-                }
-                catch ( NullPointerException e )
-                {
-                    // guess there are no plugins here.
-                }
-            }
+    private void getProfilePluginManagementPlugins( List<PluginWrapper> plugins, Model model, Profile profile )
+    {
+        try
+        {
+            List<Plugin> modelPlugins = profile.getBuild().getPluginManagement().getPlugins();
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), model.getId() + "profile["
+                + profile.getId() + "].build.pluginManagement.plugins" ) );
         }
+        catch ( NullPointerException e )
+        {
+            // guess there are no plugins here.
+        }
+    }
 
-        return plugins;
+    private void getProfileReportingPlugins( List<PluginWrapper> plugins, Model model, Profile profile )
+    {
+        try
+        {
+            List<ReportPlugin> modelReportPlugins = profile.getReporting().getPlugins();
+            // add the reporting plugins
+            plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ), model.getId()
+                + "profile[" + profile.getId() + "].reporting.plugins" ) );
+        }
+        catch ( NullPointerException e )
+        {
+            // guess there are no plugins here.
+        }
+    }
+
+    private void getProfilePlugins( List<PluginWrapper> plugins, Model model, Profile profile )
+    {
+        try
+        {
+            List<Plugin> modelPlugins = profile.getBuild().getPlugins();
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ), model.getId()
+                + ".profiles.profile[" + profile.getId() + "].build.plugins" ) );
+        }
+        catch ( NullPointerException e )
+        {
+            // guess there are no plugins here.
+        }
+    }
+
+    private void getPlugins( List<PluginWrapper> plugins, Model model )
+    {
+        try
+        {
+            List<Plugin> modelPlugins = model.getBuild().getPlugins();
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ),
+                                                  model.getId() + ".build.plugins" ) );
+        }
+        catch ( NullPointerException e )
+        {
+            // guess there are no plugins here.
+        }
+    }
+
+    private void getPluginManagementPlugins( List<PluginWrapper> plugins, Model model )
+    {
+        try
+        {
+            List<Plugin> modelPlugins = model.getBuild().getPluginManagement().getPlugins();
+            plugins.addAll( PluginWrapper.addAll( utils.resolvePlugins( modelPlugins ),
+                                                  model.getId() + ".build.pluginManagement.plugins" ) );
+        }
+        catch ( NullPointerException e )
+        {
+            // guess there are no plugins here.
+        }
+    }
+
+    private void getReportingPlugins( List<PluginWrapper> plugins, Model model )
+    {
+        try
+        {
+            List<ReportPlugin> modelReportPlugins = model.getReporting().getPlugins();
+            // add the reporting plugins
+            plugins.addAll( PluginWrapper.addAll( utils.resolveReportPlugins( modelReportPlugins ),
+                                                  model.getId() + ".reporting" ) );
+        }
+        catch ( NullPointerException e )
+        {
+            // guess there are no plugins here.
+        }
     }
 
     /**
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 5b9c157..64290af 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
@@ -1,8 +1,5 @@
 package org.apache.maven.plugins.enforcer;
 
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -31,7 +28,6 @@ import java.util.Set;
 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.plugin.testing.AbstractMojoTestCase;
 import org.apache.maven.plugins.enforcer.utils.PluginWrapper;
 
@@ -86,8 +82,6 @@ public class TestRequirePluginVersions
 
 
         EnforcerRuleHelper helper = EnforcerTestUtils.getHelper();
-        Log log = mock( Log.class );
-        when( helper.getLog() ).thenReturn( log );
 
         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
 
@@ -151,8 +145,6 @@ public class TestRequirePluginVersions
         rule.setBanSnapshots( false );
 
         EnforcerRuleHelper helper = EnforcerTestUtils.getHelper( true );
-        Log log = mock( Log.class );
-        when( helper.getLog() ).thenReturn( log );
 
         source.setArtifactId( "a-artifact" );
         assertTrue( rule.hasValidVersionSpecified( helper, source, pluginWrappers ) );
diff --git a/maven-enforcer-plugin/pom.xml b/maven-enforcer-plugin/pom.xml
index 42581d0..db4ac9f 100644
--- a/maven-enforcer-plugin/pom.xml
+++ b/maven-enforcer-plugin/pom.xml
@@ -112,6 +112,9 @@
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-plugin-plugin</artifactId>
+        <configuration>
+          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
+        </configuration>
         <executions>
           <execution>
             <id>descriptor-help</id>
diff --git a/maven-enforcer-plugin/src/it/projects/MENFORCER-306/parent/pom.xml b/maven-enforcer-plugin/src/it/projects/MENFORCER-306/parent/pom.xml
new file mode 100644
index 0000000..d66827e
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/MENFORCER-306/parent/pom.xml
@@ -0,0 +1,103 @@
+<?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>mbenson.test</groupId>
+  <artifactId>test-require-plugin-versions</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>pom</packaging>
+  <properties>
+    <enforcer.version>3.0.0-SNAPSHOT</enforcer.version>
+  </properties>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <version>@project.version@</version>
+        <executions>
+          <execution>
+            <id>enforce-plugin-versions</id>
+            <goals>
+              <goal>enforce</goal>
+            </goals>
+            <configuration>
+              <rules>
+                <requirePluginVersions>
+                  <banSnapshots>false</banSnapshots>
+                </requirePluginVersions>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+    </plugins>
+    <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-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>
+        <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-compiler-plugin</artifactId>
+          <version>3.7.0</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-plugin-plugin</artifactId>
+          <version>3.5.2</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <version>2.21.0</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-jar-plugin</artifactId>
+          <version>3.1.0</version>
+        </plugin>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-resources-plugin</artifactId>
+          <version>3.1.0</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+  </build>
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/MENFORCER-306/pom.xml b/maven-enforcer-plugin/src/it/projects/MENFORCER-306/pom.xml
new file mode 100644
index 0000000..9ad1574
--- /dev/null
+++ b/maven-enforcer-plugin/src/it/projects/MENFORCER-306/pom.xml
@@ -0,0 +1,31 @@
+<?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>
+  <parent>
+    <groupId>mbenson.test</groupId>
+    <artifactId>test-require-plugin-versions</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <relativePath>parent/pom.xml</relativePath>
+  </parent>
+  <artifactId>pom</artifactId>
+  <packaging>pom</packaging>
+</project>
diff --git a/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-plugin-repo/pom.xml b/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-plugin-repo/pom.xml
index ba8a6a7..cc568e2 100644
--- a/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-plugin-repo/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-plugin-repo/pom.xml
@@ -58,7 +58,9 @@
             <configuration>
               <rules>
                 <requireNoRepositories>
-                  <allowedPluginRepositories>plugin-repo</allowedPluginRepositories>
+                  <allowedPluginRepositories>
+                    <allowedPluginRepository>plugin-repo</allowedPluginRepository>
+                  </allowedPluginRepositories>
                 </requireNoRepositories>
               </rules>
             </configuration>
diff --git a/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-repo/pom.xml b/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-repo/pom.xml
index c0af0cc..9c9aa27 100644
--- a/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-repo/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/require-no-repositories-allow-repo/pom.xml
@@ -58,7 +58,9 @@
             <configuration>
               <rules>
                 <requireNoRepositories>
-                  <allowedRepositories>com.asual.maven.public</allowedRepositories>
+                  <allowedRepositories>
+                    <allowedRepository>com.asual.maven.public</allowedRepository>
+                  </allowedRepositories>
                 </requireNoRepositories>
               </rules>
             </configuration>
diff --git a/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_allowed-plugin-repo/pom.xml b/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_allowed-plugin-repo/pom.xml
index 4951265..557a44c 100644
--- a/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_allowed-plugin-repo/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_allowed-plugin-repo/pom.xml
@@ -58,7 +58,10 @@
             <configuration>
               <rules>
                 <requireNoRepositories>
-                  <allowedPluginRepositories>plugin-repo,com.asual.maven.public</allowedPluginRepositories>
+                  <allowedPluginRepositories>
+                    <allowedPluginRepository>plugin-repo</allowedPluginRepository>
+                    <allowedPluginRepository>com.asual.maven.public</allowedPluginRepository>
+                  </allowedPluginRepositories>
                 </requireNoRepositories>
               </rules>
             </configuration>
diff --git a/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_repositories/pom.xml b/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_repositories/pom.xml
index ba8a6a7..cc568e2 100644
--- a/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_repositories/pom.xml
+++ b/maven-enforcer-plugin/src/it/projects/require-no-repositories_failure_repositories/pom.xml
@@ -58,7 +58,9 @@
             <configuration>
               <rules>
                 <requireNoRepositories>
-                  <allowedPluginRepositories>plugin-repo</allowedPluginRepositories>
+                  <allowedPluginRepositories>
+                    <allowedPluginRepository>plugin-repo</allowedPluginRepository>
+                  </allowedPluginRepositories>
                 </requireNoRepositories>
               </rules>
             </configuration>