You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by cs...@apache.org on 2021/04/28 12:20:35 UTC

[maven-plugin-tools] 01/01: [MPLUGIN-372] Descriptor mojo fails if super class is provided scope

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

cstamas pushed a commit to branch MPLUGIN-372
in repository https://gitbox.apache.org/repos/asf/maven-plugin-tools.git

commit e4c5808900f3e7e66749a3b52f73cc6140ed05b7
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Wed Apr 28 14:19:22 2021 +0200

    [MPLUGIN-372] Descriptor mojo fails if super class is provided scope
    
    Changes:
    - Bug: adjust descriptor mojo resolution scope to include provided scope as well
    - Sanitized generator and report mojos (injects both project and project.artifacts, just one is completely enough)
    - GeneratorUtils using Artifacts not Dependencies (as they were not needed)
    - JavaAnnotationsMojoDescriptorExtractor error reporting: report failed class at least
---
 .../maven/plugin/plugin/AbstractGeneratorMojo.java | 14 +---
 .../plugin/plugin/DescriptorGeneratorMojo.java     |  2 +-
 .../apache/maven/plugin/plugin/PluginReport.java   | 15 +----
 .../JavaAnnotationsMojoDescriptorExtractor.java    | 74 +++++++++++++---------
 .../tools/plugin/generator/GeneratorUtils.java     |  7 +-
 5 files changed, 56 insertions(+), 56 deletions(-)

diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java
index f10bda4..ff97614 100644
--- a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/AbstractGeneratorMojo.java
@@ -127,14 +127,6 @@ public abstract class AbstractGeneratorMojo
     protected boolean skip;
 
     /**
-     * The set of dependencies for the current project
-     *
-     * @since 3.0
-     */
-    @Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true )
-    protected Set<Artifact> dependencies;
-    
-    /**
      * Specify the dependencies as {@code groupId:artifactId} containing (abstract) Mojos, to filter
      * dependencies scanned at runtime and focus on dependencies that are really useful to Mojo analysis.
      * By default, the value is {@code null} and all dependencies are scanned (as before this parameter was added).
@@ -250,7 +242,7 @@ public abstract class AbstractGeneratorMojo
 
         try
         {
-            List<ComponentDependency> deps = GeneratorUtils.toComponentDependencies( project.getRuntimeDependencies() );
+            List<ComponentDependency> deps = GeneratorUtils.toComponentDependencies( project.getArtifacts() );
             pluginDescriptor.setDependencies( deps );
 
             PluginToolsRequest request = new DefaultPluginToolsRequest( project, pluginDescriptor );
@@ -313,7 +305,7 @@ public abstract class AbstractGeneratorMojo
         Set<Artifact> filteredDependencies;
         if ( mojoDependencies == null )
         {
-            filteredDependencies = dependencies;
+            filteredDependencies = new LinkedHashSet<>( project.getArtifacts() );
         }
         else if ( mojoDependencies.size() == 0 )
         {
@@ -325,7 +317,7 @@ public abstract class AbstractGeneratorMojo
             
             ArtifactFilter filter = new IncludesArtifactFilter( mojoDependencies );
 
-            for ( Artifact artifact : dependencies )
+            for ( Artifact artifact : project.getArtifacts() )
             {
                 if ( filter.include( artifact ) )
                 {
diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java
index 89aa247..9827a77 100644
--- a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/DescriptorGeneratorMojo.java
@@ -43,7 +43,7 @@ import org.apache.maven.tools.plugin.generator.PluginDescriptorGenerator;
  * @since 2.0
  */
 @Mojo( name = "descriptor", defaultPhase = LifecyclePhase.PROCESS_CLASSES,
-       requiresDependencyResolution = ResolutionScope.RUNTIME, threadSafe = true )
+       requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, threadSafe = true )
 public class DescriptorGeneratorMojo
     extends AbstractGeneratorMojo
 {
diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/PluginReport.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/PluginReport.java
index 886951f..95f7381 100644
--- a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/PluginReport.java
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/PluginReport.java
@@ -24,13 +24,12 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.ResourceBundle;
-import java.util.Set;
 
-import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
 import org.apache.maven.artifact.versioning.VersionRange;
@@ -174,14 +173,6 @@ public class PluginReport
     private boolean skipReport;
 
     /**
-     * The set of dependencies for the current project
-     *
-     * @since 3.0
-     */
-    @Parameter( defaultValue = "${project.artifacts}", required = true, readonly = true )
-    protected Set<Artifact> dependencies;
-
-    /**
      * List of Remote Repositories used by the resolver
      *
      * @since 3.0
@@ -316,13 +307,13 @@ public class PluginReport
 
         try
         {
-            List<ComponentDependency> deps = GeneratorUtils.toComponentDependencies( project.getRuntimeDependencies() );
+            List<ComponentDependency> deps = GeneratorUtils.toComponentDependencies( project.getArtifacts() );
             pluginDescriptor.setDependencies( deps );
 
             PluginToolsRequest request = new DefaultPluginToolsRequest( project, pluginDescriptor );
             request.setEncoding( encoding );
             request.setSkipErrorNoDescriptorsFound( true );
-            request.setDependencies( dependencies );
+            request.setDependencies( new LinkedHashSet<>( project.getArtifacts() ) );
             request.setLocal( this.local );
             request.setRemoteRepos( this.remoteRepos );
 
diff --git a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java
index aae5922..5e7cfda 100644
--- a/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java
+++ b/maven-plugin-tools-annotations/src/main/java/org/apache/maven/tools/plugin/extractor/annotations/JavaAnnotationsMojoDescriptorExtractor.java
@@ -353,19 +353,27 @@ public class JavaAnnotationsMojoDescriptorExtractor
      */
     private DocletTag findInClassHierarchy( JavaClass javaClass, String tagName )
     {
-        DocletTag tag = javaClass.getTagByName( tagName );
-
-        if ( tag == null )
+        try
         {
-            JavaClass superClass = javaClass.getSuperJavaClass();
+            DocletTag tag = javaClass.getTagByName( tagName );
 
-            if ( superClass != null )
+            if ( tag == null )
             {
-                tag = findInClassHierarchy( superClass, tagName );
+                JavaClass superClass = javaClass.getSuperJavaClass();
+
+                if ( superClass != null )
+                {
+                    tag = findInClassHierarchy( superClass, tagName );
+                }
             }
-        }
 
-        return tag;
+            return tag;
+        }
+        catch ( NoClassDefFoundError e )
+        {
+            getLogger().warn( "Failed extracting tag '" + tagName + "' from class " + javaClass );
+            throw e;
+        }
     }
 
     /**
@@ -377,37 +385,45 @@ public class JavaAnnotationsMojoDescriptorExtractor
     private Map<String, JavaField> extractFieldParameterTags( JavaClass javaClass,
                                                               Map<String, JavaClass> javaClassesMap )
     {
-        Map<String, JavaField> rawParams = new TreeMap<String, com.thoughtworks.qdox.model.JavaField>();
+        try
+        {
+            Map<String, JavaField> rawParams = new TreeMap<String, com.thoughtworks.qdox.model.JavaField>();
 
-        // we have to add the parent fields first, so that they will be overwritten by the local fields if
-        // that actually happens...
-        JavaClass superClass = javaClass.getSuperJavaClass();
+            // we have to add the parent fields first, so that they will be overwritten by the local fields if
+            // that actually happens...
+            JavaClass superClass = javaClass.getSuperJavaClass();
 
-        if ( superClass != null )
-        {
-            if ( superClass.getFields().size() > 0 )
+            if ( superClass != null )
             {
-                rawParams = extractFieldParameterTags( superClass, javaClassesMap );
+                if ( superClass.getFields().size() > 0 )
+                {
+                    rawParams = extractFieldParameterTags( superClass, javaClassesMap );
+                }
+                // maybe sources comes from scan of sources artifact
+                superClass = javaClassesMap.get( superClass.getFullyQualifiedName() );
+                if ( superClass != null )
+                {
+                    rawParams = extractFieldParameterTags( superClass, javaClassesMap );
+                }
             }
-            // maybe sources comes from scan of sources artifact
-            superClass = javaClassesMap.get( superClass.getFullyQualifiedName() );
-            if ( superClass != null )
+            else
             {
-                rawParams = extractFieldParameterTags( superClass, javaClassesMap );
+
+                rawParams = new TreeMap<>();
             }
-        }
-        else
-        {
 
-            rawParams = new TreeMap<>();
-        }
+            for ( JavaField field : javaClass.getFields() )
+            {
+                rawParams.put( field.getName(), field );
+            }
 
-        for ( JavaField field : javaClass.getFields() )
+            return rawParams;
+        }
+        catch ( NoClassDefFoundError e )
         {
-            rawParams.put( field.getName(), field );
+            getLogger().warn( "Failed extracting parameters from " + javaClass );
+            throw e;
         }
-        
-        return rawParams;
     }
 
     protected Map<String, JavaClass> discoverClasses( final PluginToolsRequest request )
diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java
index 547e430..f0f13dd 100644
--- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java
+++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/GeneratorUtils.java
@@ -31,6 +31,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -44,8 +45,8 @@ import javax.swing.text.html.HTML;
 import javax.swing.text.html.HTMLEditorKit;
 import javax.swing.text.html.parser.ParserDelegator;
 
+import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.DependencyResolutionRequiredException;
-import org.apache.maven.model.Dependency;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.project.MavenProject;
@@ -123,11 +124,11 @@ public final class GeneratorUtils
      * @param dependencies not null list of <code>Dependency</code>
      * @return list of component dependencies
      */
-    public static List<ComponentDependency> toComponentDependencies( List<Dependency> dependencies )
+    public static List<ComponentDependency> toComponentDependencies( Collection<Artifact> dependencies )
     {
         List<ComponentDependency> componentDeps = new LinkedList<>();
 
-        for ( Dependency dependency : dependencies )
+        for ( Artifact dependency : dependencies )
         {
             ComponentDependency cd = new ComponentDependency();