From 289146cb1969f05b1f3621edfca5bf5ef784824d Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Fri, 1 Oct 2021 14:13:43 +0200 Subject: [PATCH] Upgrade to maven 3.x and avoid using deprecated API --- maven-plugin-plugin/pom.xml | 4 - .../it/annotation-with-inheritance/pom.xml | 5 - .../maven/plugin/plugin/PluginReport.java | 27 ++-- .../AddPluginArtifactMetadataMojo.java | 1 - .../metadata/GroupRepositoryMetadata.java | 122 ++++++++++++++++++ maven-plugin-tools-annotations/pom.xml | 4 - ...avaAnnotationsMojoDescriptorExtractor.java | 71 +++++----- .../generator/PluginDescriptorGenerator.java | 1 + .../generator/AbstractGeneratorTestCase.java | 2 +- pom.xml | 7 +- 10 files changed, 164 insertions(+), 80 deletions(-) create mode 100644 maven-plugin-plugin/src/main/java/org/apache/maven/plugin/plugin/metadata/GroupRepositoryMetadata.java diff --git a/maven-plugin-plugin/pom.xml b/maven-plugin-plugin/pom.xml index 1377acbe0..084d3c73e 100644 --- a/maven-plugin-plugin/pom.xml +++ b/maven-plugin-plugin/pom.xml @@ -48,10 +48,6 @@ org.apache.maven maven-core - - org.apache.maven - maven-compat - org.apache.maven.plugin-tools maven-plugin-tools-api 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 4921b29bc..dd60439d6 100644 --- a/maven-plugin-plugin/src/it/annotation-with-inheritance/pom.xml +++ b/maven-plugin-plugin/src/it/annotation-with-inheritance/pom.xml @@ -45,11 +45,6 @@ under the License. maven-plugin-api 2.0 - - org.apache.maven - maven-compat - @mavenVersion@ - org.apache.maven.plugin-tools maven-plugin-annotations 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 95f738120..cb2eee93c 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.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.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 @@ private PluginDescriptor extractPluginDescriptor() * (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 MNG-6109 + * @see MPLUGIN-319 */ 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 0711980ad..630c872e2 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 @@ 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 000000000..67ea96d8f --- /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 Brett Porter + */ +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 plugins = getMetadata().getPlugins(); + boolean found = false; + for ( Iterator 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 92c9de85c..aeb7cb2ae 100644 --- a/maven-plugin-tools-annotations/pom.xml +++ b/maven-plugin-tools-annotations/pom.xml @@ -48,10 +48,6 @@ org.apache.maven maven-artifact - - org.apache.maven - maven-compat - org.apache.maven.plugin-tools maven-plugin-tools-api 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 5e7cfda28..ca68e321b 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.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.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.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 @@ protected Map discoverClassesFromSourcesJar( Artifact artifac 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 @@ protected Map discoverClassesFromSourcesJar( Artifact artifac 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 @@ protected Map discoverClassesFromSourcesJar( Artifact artifac /** * from sources scan to get @since and @deprecated and description of classes and fields. - * - * @param mojoAnnotatedClasses - * @param javaClassesMap */ protected void populateDataFromJavadoc( Map mojoAnnotatedClasses, Map javaClassesMap ) @@ -289,8 +281,7 @@ protected void populateDataFromJavadoc( Map mojoAnno // populate parameters Map parameters = - getParametersParentHierarchy( entry.getValue(), new HashMap(), - mojoAnnotatedClasses ); + getParametersParentHierarchy( entry.getValue(), mojoAnnotatedClasses ); parameters = new TreeMap<>( parameters ); for ( Map.Entry parameter : parameters.entrySet() ) { @@ -387,7 +378,7 @@ private Map extractFieldParameterTags( JavaClass javaClass, { try { - Map rawParams = new TreeMap(); + Map 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 @@ private List toMojoDescriptors( Map // Parameter annotations Map parameters = - getParametersParentHierarchy( mojoAnnotatedClass, new HashMap(), - mojoAnnotatedClasses ); + getParametersParentHierarchy( mojoAnnotatedClass, mojoAnnotatedClasses ); for ( ParameterAnnotationContent parameterAnnotationContent : new TreeSet<>( parameters.values() ) ) { @@ -594,8 +584,7 @@ private List toMojoDescriptors( Map // Component annotations Map components = - getComponentsParentHierarchy( mojoAnnotatedClass, new HashMap(), - mojoAnnotatedClasses ); + getComponentsParentHierarchy( mojoAnnotatedClass, mojoAnnotatedClasses ); for ( ComponentAnnotationContent componentAnnotationContent : new TreeSet<>( components.values() ) ) { @@ -661,8 +650,8 @@ protected ExecuteAnnotationContent findExecuteInParentHierarchy( MojoAnnotatedCl protected Map getParametersParentHierarchy( - MojoAnnotatedClass mojoAnnotatedClass, Map parameters, - Map mojoAnnotatedClasses ) + MojoAnnotatedClass mojoAnnotatedClass, + Map mojoAnnotatedClasses ) { List parameterAnnotationContents = new ArrayList<>(); @@ -699,8 +688,8 @@ protected List getParametersParent( MojoAnnotatedCla } protected Map getComponentsParentHierarchy( - MojoAnnotatedClass mojoAnnotatedClass, Map components, - Map mojoAnnotatedClasses ) + MojoAnnotatedClass mojoAnnotatedClass, + Map mojoAnnotatedClasses ) { List 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 a3e516226..6e777c087 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 @@ protected void processMojoDescriptor( MojoDescriptor mojoDescriptor, XMLWriter w * @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 0f41386d4..b138f4c94 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 void testGenerator() mojoDescriptor.setImplementation( "org.apache.maven.tools.plugin.generator.TestMojo" ); mojoDescriptor.setDependencyResolutionRequired( "compile" ); - List params = new ArrayList(); + List params = new ArrayList<>(); Parameter param = new Parameter(); param.setExpression( "${project.build.directory}" ); diff --git a/pom.xml b/pom.xml index 522502883..cb56dd877 100644 --- a/pom.xml +++ b/pom.xml @@ -93,7 +93,7 @@ 2.22.2 7 1.3 - 3.0 + 3.2.2 1.7.1 3.2.2 plugin-tools-archives/plugin-tools-LATEST @@ -154,11 +154,6 @@ maven-plugin-api ${mavenVersion} - - org.apache.maven - maven-compat - ${mavenVersion} - org.apache.maven maven-core