You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2021/10/21 15:19:42 UTC

[maven-plugin-tools] branch master updated: [MPLUGIN-377] Upgrade to maven 3.x and avoid using deprecated API (#37)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 73e32d1  [MPLUGIN-377] Upgrade to maven 3.x and avoid using deprecated API (#37)
73e32d1 is described below

commit 73e32d10e5ef6d42d3438fb34f6bb5561bce952c
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Thu Oct 21 17:18:59 2021 +0200

    [MPLUGIN-377] Upgrade to maven 3.x and avoid using deprecated API (#37)
---
 maven-plugin-plugin/pom.xml                        |   5 -
 .../src/it/annotation-with-inheritance/pom.xml     |   5 -
 .../apache/maven/plugin/plugin/PluginReport.java   |  27 ++---
 .../metadata/AddPluginArtifactMetadataMojo.java    |   1 -
 .../plugin/metadata/GroupRepositoryMetadata.java   | 122 +++++++++++++++++++++
 maven-plugin-tools-annotations/pom.xml             |   4 -
 .../JavaAnnotationsMojoDescriptorExtractor.java    |  71 +++++-------
 .../generator/PluginDescriptorGenerator.java       |   1 +
 .../generator/AbstractGeneratorTestCase.java       |   2 +-
 pom.xml                                            |   7 +-
 10 files changed, 164 insertions(+), 81 deletions(-)

diff --git a/maven-plugin-plugin/pom.xml b/maven-plugin-plugin/pom.xml
index 19ce510..807d368 100644
--- a/maven-plugin-plugin/pom.xml
+++ b/maven-plugin-plugin/pom.xml
@@ -50,11 +50,6 @@
       <scope>provided</scope>
     </dependency>
     <dependency>
-      <groupId>org.apache.maven</groupId>
-      <artifactId>maven-compat</artifactId>
-      <scope>provided</scope>
-    </dependency>
-    <dependency>
       <groupId>org.apache.maven.plugin-tools</groupId>
       <artifactId>maven-plugin-tools-api</artifactId>
     </dependency>
diff --git a/maven-plugin-plugin/src/it/annotation-with-inheritance/pom.xml b/maven-plugin-plugin/src/it/annotation-with-inheritance/pom.xml
index 3cb72fe..e9c9113 100644
--- a/maven-plugin-plugin/src/it/annotation-with-inheritance/pom.xml
+++ b/maven-plugin-plugin/src/it/annotation-with-inheritance/pom.xml
@@ -51,11 +51,6 @@ under the License.
       <version>@mavenVersion@</version>
     </dependency>
     <dependency>
-      <groupId>org.apache.maven</groupId>
-      <artifactId>maven-compat</artifactId>
-      <version>@mavenVersion@</version>
-    </dependency>
-    <dependency>
       <groupId>org.apache.maven.plugin-tools</groupId>
       <artifactId>maven-plugin-annotations</artifactId>
       <version>@project.version@</version>
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 95f7381..cb2eee9 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
@@ -31,11 +31,8 @@ import java.util.Map;
 import java.util.ResourceBundle;
 
 import org.apache.maven.artifact.repository.ArtifactRepository;
-import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
-import org.apache.maven.artifact.versioning.VersionRange;
 import org.apache.maven.doxia.sink.Sink;
 import org.apache.maven.doxia.siterenderer.Renderer;
-import org.apache.maven.execution.RuntimeInformation;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
@@ -51,6 +48,7 @@ import org.apache.maven.project.MavenProject;
 import org.apache.maven.reporting.AbstractMavenReport;
 import org.apache.maven.reporting.AbstractMavenReportRenderer;
 import org.apache.maven.reporting.MavenReportException;
+import org.apache.maven.rtinfo.RuntimeInformation;
 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
 import org.apache.maven.tools.plugin.PluginToolsRequest;
 import org.apache.maven.tools.plugin.extractor.ExtractionException;
@@ -341,29 +339,22 @@ public class PluginReport
      * (because of Maven MNG-6109 bug that won't give accurate 'since' info when reading plugin.xml).
      * 
      * @return the proper pluginDescriptorBuilder
-     * @see https://issues.apache.org/jira/browse/MNG-6109
-     * @see https://issues.apache.org/jira/browse/MPLUGIN-319
+     * @see <a href="https://issues.apache.org/jira/browse/MNG-6109">MNG-6109</a>
+     * @see <a href="https://issues.apache.org/jira/browse/MPLUGIN-319">MPLUGIN-319</a>
      */
     private PluginDescriptorBuilder getPluginDescriptorBuilder()
     {
         PluginDescriptorBuilder pluginDescriptorBuilder;
-        try
+
+        if ( rtInfo.isMavenVersion( "(3.3.9,)" ) )
         {
-            VersionRange versionRange = VersionRange.createFromVersionSpec( "(3.3.9,)" );
-            if ( versionRange.containsVersion( rtInfo.getApplicationVersion() ) )
-            {
-                pluginDescriptorBuilder = new PluginDescriptorBuilder();
-            }
-            else
-            {
-                pluginDescriptorBuilder = new MNG6109PluginDescriptorBuilder();
-            }
+            pluginDescriptorBuilder = new PluginDescriptorBuilder();
         }
-        catch ( InvalidVersionSpecificationException e )
+        else
         {
-            return new MNG6109PluginDescriptorBuilder();
+            pluginDescriptorBuilder = new MNG6109PluginDescriptorBuilder();
         }
-        
+
         return pluginDescriptorBuilder;
     }
 
diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/AddPluginArtifactMetadataMojo.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/AddPluginArtifactMetadataMojo.java
index 0711980..630c872 100644
--- a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/AddPluginArtifactMetadataMojo.java
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/AddPluginArtifactMetadataMojo.java
@@ -21,7 +21,6 @@ package org.apache.maven.plugin.plugin.metadata;
 
 import org.apache.maven.artifact.Artifact;
 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
-import org.apache.maven.artifact.repository.metadata.GroupRepositoryMetadata;
 import org.apache.maven.artifact.repository.metadata.Versioning;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
diff --git a/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/GroupRepositoryMetadata.java b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/GroupRepositoryMetadata.java
new file mode 100644
index 0000000..67ea96d
--- /dev/null
+++ b/maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/GroupRepositoryMetadata.java
@@ -0,0 +1,122 @@
+package org.apache.maven.plugin.plugin.metadata;
+
+/*
+ * 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.Iterator;
+import java.util.List;
+
+import org.apache.maven.artifact.repository.ArtifactRepository;
+import org.apache.maven.artifact.repository.metadata.AbstractRepositoryMetadata;
+import org.apache.maven.artifact.repository.metadata.Metadata;
+import org.apache.maven.artifact.repository.metadata.Plugin;
+
+/**
+ * Metadata for the group directory of the repository.
+ *
+ * @author <a href="mailto:brett@apache.org">Brett Porter</a>
+ */
+public class GroupRepositoryMetadata
+    extends AbstractRepositoryMetadata
+{
+    private final String groupId;
+
+    public GroupRepositoryMetadata( String groupId )
+    {
+        super( new Metadata() );
+        this.groupId = groupId;
+    }
+
+    public boolean storedInGroupDirectory()
+    {
+        return true;
+    }
+
+    public boolean storedInArtifactVersionDirectory()
+    {
+        return false;
+    }
+
+    public String getGroupId()
+    {
+        return groupId;
+    }
+
+    public String getArtifactId()
+    {
+        return null;
+    }
+
+    public String getBaseVersion()
+    {
+        return null;
+    }
+
+    public void addPluginMapping( String goalPrefix,
+                                  String artifactId )
+    {
+        addPluginMapping( goalPrefix, artifactId, artifactId );
+    }
+
+    public void addPluginMapping( String goalPrefix,
+                                  String artifactId,
+                                  String name )
+    {
+        List<Plugin> plugins = getMetadata().getPlugins();
+        boolean found = false;
+        for ( Iterator<Plugin> i = plugins.iterator(); i.hasNext() && !found; )
+        {
+            Plugin plugin = i.next();
+            if ( plugin.getPrefix().equals( goalPrefix ) )
+            {
+                found = true;
+            }
+        }
+        if ( !found )
+        {
+            Plugin plugin = new Plugin();
+            plugin.setPrefix( goalPrefix );
+            plugin.setArtifactId( artifactId );
+            plugin.setName( name );
+
+
+            getMetadata().addPlugin( plugin );
+        }
+    }
+
+    public Object getKey()
+    {
+        return groupId;
+    }
+
+    public boolean isSnapshot()
+    {
+        return false;
+    }
+
+    public ArtifactRepository getRepository()
+    {
+        return null;
+    }
+
+    public void setRepository( ArtifactRepository remoteRepository )
+    {
+        // intentionally blank
+    }
+}
diff --git a/maven-plugin-tools-annotations/pom.xml b/maven-plugin-tools-annotations/pom.xml
index 92c9de8..aeb7cb2 100644
--- a/maven-plugin-tools-annotations/pom.xml
+++ b/maven-plugin-tools-annotations/pom.xml
@@ -49,10 +49,6 @@
       <artifactId>maven-artifact</artifactId>
     </dependency>
     <dependency>
-      <groupId>org.apache.maven</groupId>
-      <artifactId>maven-compat</artifactId>
-    </dependency>
-    <dependency>
       <groupId>org.apache.maven.plugin-tools</groupId>
       <artifactId>maven-plugin-tools-api</artifactId>
     </dependency>
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 5e7cfda..ca68e32 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
@@ -37,10 +37,8 @@ import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.apache.maven.artifact.Artifact;
-import org.apache.maven.artifact.factory.ArtifactFactory;
-import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
-import org.apache.maven.artifact.resolver.ArtifactResolutionException;
-import org.apache.maven.artifact.resolver.ArtifactResolver;
+import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
+import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
 import org.apache.maven.plugin.descriptor.DuplicateParameterException;
 import org.apache.maven.plugin.descriptor.InvalidParameterException;
 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
@@ -48,6 +46,7 @@ import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
 import org.apache.maven.plugin.descriptor.Requirement;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.repository.RepositorySystem;
 import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
 import org.apache.maven.tools.plugin.PluginToolsRequest;
 import org.apache.maven.tools.plugin.extractor.ExtractionException;
@@ -60,6 +59,7 @@ import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotated
 import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScanner;
 import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScannerRequest;
 import org.apache.maven.tools.plugin.util.PluginUtils;
+import org.codehaus.plexus.archiver.ArchiverException;
 import org.codehaus.plexus.archiver.UnArchiver;
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 import org.codehaus.plexus.archiver.manager.NoSuchArchiverException;
@@ -90,10 +90,7 @@ public class JavaAnnotationsMojoDescriptorExtractor
     private MojoAnnotationsScanner mojoAnnotationsScanner;
 
     @org.codehaus.plexus.component.annotations.Requirement
-    private ArtifactResolver artifactResolver;
-
-    @org.codehaus.plexus.component.annotations.Requirement
-    private ArtifactFactory artifactFactory;
+    private RepositorySystem repositorySystem;
 
     @org.codehaus.plexus.component.annotations.Requirement
     private ArchiverManager archiverManager;
@@ -203,10 +200,21 @@ public class JavaAnnotationsMojoDescriptorExtractor
         try
         {
             Artifact sourcesArtifact =
-                artifactFactory.createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(),
-                                                              artifact.getVersion(), artifact.getType(), classifier );
-
-            artifactResolver.resolve( sourcesArtifact, request.getRemoteRepos(), request.getLocal() );
+                repositorySystem.createArtifactWithClassifier( artifact.getGroupId(), artifact.getArtifactId(),
+                                                               artifact.getVersion(), artifact.getType(), classifier );
+
+            ArtifactResolutionRequest req = new ArtifactResolutionRequest();
+            req.setArtifact( sourcesArtifact );
+            req.setLocalRepository( request.getLocal() );
+            req.setRemoteRepositories( request.getRemoteRepos() );
+            ArtifactResolutionResult res = repositorySystem.resolve( req );
+            if ( res.hasMissingArtifacts() || res.hasExceptions() )
+            {
+                getLogger().warn(
+                    "Unable to get sources artifact for " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
+                    + artifact.getVersion() + ". Some javadoc tags (@since, @deprecated and comments) won't be used" );
+                return Collections.emptyMap();
+            }
 
             if ( sourcesArtifact.getFile() == null || !sourcesArtifact.getFile().exists() )
             {
@@ -226,23 +234,10 @@ public class JavaAnnotationsMojoDescriptorExtractor
             unArchiver.setDestDirectory( extractDirectory );
             unArchiver.extract();
 
-            return discoverClasses( request.getEncoding(), Arrays.asList( extractDirectory ), 
+            return discoverClasses( request.getEncoding(), Arrays.asList( extractDirectory ),
                                     request.getDependencies() );
         }
-        catch ( ArtifactResolutionException e )
-        {
-            throw new ExtractionException( e.getMessage(), e );
-        }
-        catch ( ArtifactNotFoundException e )
-        {
-            //throw new ExtractionException( e.getMessage(), e );
-            getLogger().debug( "skip ArtifactNotFoundException:" + e.getMessage() );
-            getLogger().warn(
-                "Unable to get sources artifact for " + artifact.getGroupId() + ":" + artifact.getArtifactId() + ":"
-                    + artifact.getVersion() + ". Some javadoc tags (@since, @deprecated and comments) won't be used" );
-            return Collections.emptyMap();
-        }
-        catch ( NoSuchArchiverException e )
+        catch ( ArchiverException | NoSuchArchiverException e )
         {
             throw new ExtractionException( e.getMessage(), e );
         }
@@ -250,9 +245,6 @@ public class JavaAnnotationsMojoDescriptorExtractor
 
     /**
      * from sources scan to get @since and @deprecated and description of classes and fields.
-     *
-     * @param mojoAnnotatedClasses
-     * @param javaClassesMap
      */
     protected void populateDataFromJavadoc( Map<String, MojoAnnotatedClass> mojoAnnotatedClasses,
                                             Map<String, JavaClass> javaClassesMap )
@@ -289,8 +281,7 @@ public class JavaAnnotationsMojoDescriptorExtractor
 
             // populate parameters
             Map<String, ParameterAnnotationContent> parameters =
-                getParametersParentHierarchy( entry.getValue(), new HashMap<String, ParameterAnnotationContent>(),
-                                              mojoAnnotatedClasses );
+                    getParametersParentHierarchy( entry.getValue(), mojoAnnotatedClasses );
             parameters = new TreeMap<>( parameters );
             for ( Map.Entry<String, ParameterAnnotationContent> parameter : parameters.entrySet() )
             {
@@ -387,7 +378,7 @@ public class JavaAnnotationsMojoDescriptorExtractor
     {
         try
         {
-            Map<String, JavaField> rawParams = new TreeMap<String, com.thoughtworks.qdox.model.JavaField>();
+            Map<String, JavaField> rawParams = new TreeMap<>();
 
             // we have to add the parent fields first, so that they will be overwritten by the local fields if
             // that actually happens...
@@ -560,8 +551,7 @@ public class JavaAnnotationsMojoDescriptorExtractor
 
             // Parameter annotations
             Map<String, ParameterAnnotationContent> parameters =
-                getParametersParentHierarchy( mojoAnnotatedClass, new HashMap<String, ParameterAnnotationContent>(),
-                                              mojoAnnotatedClasses );
+                    getParametersParentHierarchy( mojoAnnotatedClass, mojoAnnotatedClasses );
 
             for ( ParameterAnnotationContent parameterAnnotationContent : new TreeSet<>( parameters.values() ) )
             {
@@ -594,8 +584,7 @@ public class JavaAnnotationsMojoDescriptorExtractor
 
             // Component annotations
             Map<String, ComponentAnnotationContent> components =
-                getComponentsParentHierarchy( mojoAnnotatedClass, new HashMap<String, ComponentAnnotationContent>(),
-                                              mojoAnnotatedClasses );
+                    getComponentsParentHierarchy( mojoAnnotatedClass, mojoAnnotatedClasses );
 
             for ( ComponentAnnotationContent componentAnnotationContent : new TreeSet<>( components.values() ) )
             {
@@ -661,8 +650,8 @@ public class JavaAnnotationsMojoDescriptorExtractor
 
 
     protected Map<String, ParameterAnnotationContent> getParametersParentHierarchy(
-        MojoAnnotatedClass mojoAnnotatedClass, Map<String, ParameterAnnotationContent> parameters,
-        Map<String, MojoAnnotatedClass> mojoAnnotatedClasses )
+            MojoAnnotatedClass mojoAnnotatedClass,
+            Map<String, MojoAnnotatedClass> mojoAnnotatedClasses )
     {
         List<ParameterAnnotationContent> parameterAnnotationContents = new ArrayList<>();
 
@@ -699,8 +688,8 @@ public class JavaAnnotationsMojoDescriptorExtractor
     }
 
     protected Map<String, ComponentAnnotationContent> getComponentsParentHierarchy(
-        MojoAnnotatedClass mojoAnnotatedClass, Map<String, ComponentAnnotationContent> components,
-        Map<String, MojoAnnotatedClass> mojoAnnotatedClasses )
+            MojoAnnotatedClass mojoAnnotatedClass,
+            Map<String, MojoAnnotatedClass> mojoAnnotatedClasses )
     {
         List<ComponentAnnotationContent> componentAnnotationContents = new ArrayList<>();
 
diff --git a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
index a3e5162..6e777c0 100644
--- a/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
+++ b/maven-plugin-tools-generators/src/main/java/org/apache/maven/tools/plugin/generator/PluginDescriptorGenerator.java
@@ -179,6 +179,7 @@ public class PluginDescriptorGenerator
      * @param w              not null
      * @param helpDescriptor will clean html content from description fields
      */
+    @SuppressWarnings( "deprecation" )
     protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w, boolean helpDescriptor )
     {
         w.startElement( "mojo" );
diff --git a/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java b/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java
index 0f41386..b138f4c 100644
--- a/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java
+++ b/maven-plugin-tools-generators/src/test/java/org/apache/maven/tools/plugin/generator/AbstractGeneratorTestCase.java
@@ -66,7 +66,7 @@ public abstract class AbstractGeneratorTestCase
         mojoDescriptor.setImplementation( "org.apache.maven.tools.plugin.generator.TestMojo" );
         mojoDescriptor.setDependencyResolutionRequired( "compile" );
 
-        List<Parameter> params = new ArrayList<Parameter>();
+        List<Parameter> params = new ArrayList<>();
 
         Parameter param = new Parameter();
         param.setExpression( "${project.build.directory}" );
diff --git a/pom.xml b/pom.xml
index 5225028..cb56dd8 100644
--- a/pom.xml
+++ b/pom.xml
@@ -93,7 +93,7 @@
     <surefire.version>2.22.2</surefire.version>
     <javaVersion>7</javaVersion>
     <pluginTestingHarnessVersion>1.3</pluginTestingHarnessVersion>
-    <mavenVersion>3.0</mavenVersion>
+    <mavenVersion>3.2.2</mavenVersion>
     <antVersion>1.7.1</antVersion>
     <mavenInvokerPluginVersion>3.2.2</mavenInvokerPluginVersion>
     <maven.site.path>plugin-tools-archives/plugin-tools-LATEST</maven.site.path>
@@ -156,11 +156,6 @@
       </dependency>
       <dependency>
         <groupId>org.apache.maven</groupId>
-        <artifactId>maven-compat</artifactId>
-        <version>${mavenVersion}</version>
-      </dependency>
-      <dependency>
-        <groupId>org.apache.maven</groupId>
         <artifactId>maven-core</artifactId>
         <version>${mavenVersion}</version>
       </dependency>