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

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


The following commit(s) were added to refs/heads/MENFORCER-388 by this push:
     new 9c60aeb  Fix for Maven 3.6.0 and before
9c60aeb is described below

commit 9c60aeb3bba917eeb971bd7d94f8b9a1ed7a35b9
Author: rfscholte <[email protected]>
AuthorDate: Thu Jul 22 12:38:13 2021 +0200

    Fix for Maven 3.6.0 and before
---
 .../plugins/enforcer/RequirePluginVersions.java    |  11 +-
 .../plugins/enforcer/utils/PluginWrapper.java      |  44 +++++--
 .../plugins/enforcer/utils/PluginWrapper.java.bak  | 134 ---------------------
 3 files changed, 42 insertions(+), 147 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 5820fa5..c098348 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
@@ -300,13 +300,18 @@ public class RequirePluginVersions
                 {
                     newMsg.append( currentPlugin.getVersion() );
 
-                    if ( PluginWrapper.isVersionFromDefaultLifecycleBindings( 
currentPlugin ) )
+                    if ( PluginWrapper.isVersionFromDefaultLifecycleBindings( 
currentPlugin ).orElse( false ) )
                     {
                         newMsg.append( " via default lifecycle bindings" );
                     }
-                    else if ( PluginWrapper.isVersionFromSuperpom( 
currentPlugin ) )
+                    else 
                     {
-                        newMsg.append( " via super POM" );
+                        String msg = PluginWrapper.isVersionFromSuperpom( 
currentPlugin )
+                                        .filter( b -> b )
+                                        .map( t -> " via super POM" )
+                                        // for Maven 3.6.0 or before (MNG-6593 
/ MNG-6600)
+                                        .orElse( " via super POM or default 
lifecycle bindings" );
+                        newMsg.append( msg );
                     }
                 }
             }
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 3d2db6a..70d2f11 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
@@ -21,6 +21,7 @@ package org.apache.maven.plugins.enforcer.utils;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Optional;
 
 import org.apache.maven.model.InputLocation;
 import org.apache.maven.model.InputLocationTracker;
@@ -50,7 +51,9 @@ public class PluginWrapper
             results = new ArrayList<>( plugins.size() );
             for ( InputLocationTracker o : plugins )
             {
-                if ( banMavenDefaults && ( 
isVersionFromDefaultLifecycleBindings( o ) || isVersionFromSuperpom( o ) ) )
+                // null or true means it is most assumed a Maven default
+                if ( banMavenDefaults && ( 
isVersionFromDefaultLifecycleBindings( o ).orElse( true )
+                    || isVersionFromSuperpom( o ).orElse( true ) ) )
                 {
                     continue;
                 }
@@ -70,20 +73,41 @@ public class PluginWrapper
         }
         return results;
     }
-
-    public static boolean isVersionFromDefaultLifecycleBindings( 
InputLocationTracker o )
+    
+    /**
+     * 
+     * @param o either Plugin or ReportPlugin
+     * @return   
+     */
+    public static Optional<Boolean> isVersionFromDefaultLifecycleBindings( 
InputLocationTracker o )
     {
-        String modelId = o.getLocation( "version" ).getSource().getModelId();
+        InputLocation versionLocation = o.getLocation( "version" );
+        if ( versionLocation == null )
+        {
+            return Optional.empty();
+        }
 
-        return modelId.startsWith( "org.apache.maven:maven-core:" )
-            && modelId.endsWith( ":default-lifecycle-bindings" );
+        String modelId = versionLocation.getSource().getModelId();
+        return Optional.of( modelId.startsWith( "org.apache.maven:maven-core:" 
)
+                            && modelId.endsWith( ":default-lifecycle-bindings" 
) );
     }
 
-    public static boolean isVersionFromSuperpom( InputLocationTracker o )
+    /**
+     * 
+     * @param o either Plugin or ReportPlugin
+     * @return null if untraceable, otherwise its matching value  
+     */
+    public static Optional<Boolean> isVersionFromSuperpom( 
InputLocationTracker o )
     {
-        String modelId = o.getLocation( "version" ).getSource().getModelId();
-
-        return modelId.startsWith( "org.apache.maven:maven-model-builder:" ) 
&& modelId.endsWith( ":super-pom" );
+        InputLocation versionLocation = o.getLocation( "version" );
+        if ( versionLocation == null )
+        {
+            return Optional.empty();
+        }
+        
+        String modelId = versionLocation.getSource().getModelId();
+        return Optional.of( modelId.startsWith( 
"org.apache.maven:maven-model-builder:" )
+            && modelId.endsWith( ":super-pom" ) );
     }
 
     private PluginWrapper( Plugin plugin )
diff --git 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java.bak
 
b/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java.bak
deleted file mode 100644
index e3aee64..0000000
--- 
a/enforcer-rules/src/main/java/org/apache/maven/plugins/enforcer/utils/PluginWrapper.java.bak
+++ /dev/null
@@ -1,134 +0,0 @@
-package org.apache.maven.plugins.enforcer.utils;
-
-/*
- * 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 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;
-
-/**
- * @author Brian Fox
- *
- */
-public class PluginWrapper
-{
-    private final String groupId;
-
-    private final String artifactId;
-
-    private final String version;
-
-    private final InputLocationTracker locationTracker;
-
-    public static List<PluginWrapper> addAll( List<? extends 
InputLocationTracker> plugins, boolean banMavenDefaults )
-    {
-        List<PluginWrapper> results = null;
-
-        if ( !plugins.isEmpty() )
-        {
-            results = new ArrayList<>( plugins.size() );
-            for ( InputLocationTracker o : plugins )
-            {
-                if ( banMavenDefaults && ( 
isVersionFromDefaultLifecycleBindings( o ) || isVersionFromSuperpom( o ) ) )
-                {
-                    continue;
-                }
-
-                if ( o instanceof Plugin )
-                {
-                    results.add( new PluginWrapper( (Plugin) o ) );
-                }
-                else
-                {
-                    if ( o instanceof ReportPlugin )
-                    {
-                        results.add( new PluginWrapper( (ReportPlugin) o ) );
-                    }
-                }
-            }
-        }
-        return results;
-    }
-
-    public static boolean isVersionFromDefaultLifecycleBindings( 
InputLocationTracker o )
-    {
-        String modelId = o.getLocation( "version" ).getSource().getModelId();
-
-        return modelId.startsWith( "org.apache.maven:maven-core:" )
-            && modelId.endsWith( ":default-lifecycle-bindings" );
-    }
-
-    public static boolean isVersionFromSuperpom( InputLocationTracker o )
-    {
-        String modelId = o.getLocation( "version" ).getSource().getModelId();
-
-        return modelId.startsWith( "org.apache.maven:maven-model-builder:" ) 
&& modelId.endsWith( ":super-pom" );
-    }
-
-    public PluginWrapper( Plugin plugin )
-    {
-        this.groupId = plugin.getGroupId();
-        this.artifactId = plugin.getArtifactId();
-        this.version = plugin.getVersion();
-        this.locationTracker = plugin;
-    }
-
-    private PluginWrapper( ReportPlugin plugin )
-    {
-        this.groupId = plugin.getGroupId();
-        this.artifactId = plugin.getArtifactId();
-        this.version = plugin.getVersion();
-        this.locationTracker = plugin;
-    }
-
-    public String getGroupId()
-    {
-        return groupId;
-    }
-
-    public String getArtifactId()
-    {
-        return artifactId;
-    }
-
-    public String getVersion()
-    {
-        return version;
-    }
-
-    public String getSource()
-    {
-        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();
-        }
-    }
-}

Reply via email to