You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by rf...@apache.org on 2019/09/11 19:42:06 UTC
[maven-dependency-plugin] 03/08: Rewrite go-offline so it resembles
resolve-plugins
This is an automated email from the ASF dual-hosted git repository.
rfscholte pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git
commit 3e39ea46f8caf8b94006dda703bce87d84de03bf
Author: Maarten Mulders <ma...@infosupport.com>
AuthorDate: Sun Sep 1 22:07:27 2019 +0200
Rewrite go-offline so it resembles resolve-plugins
---
.../dependency/resolvers/AbstractResolveMojo.java | 41 +++------
.../ExcludeReactorProjectsArtifactFilter.java | 95 +++++++++++++++++++++
.../dependency/resolvers/GoOfflineMojo.java | 99 +++++++++++++++++++---
.../dependency/resolvers/ResolvePluginsMojo.java | 2 +-
4 files changed, 195 insertions(+), 42 deletions(-)
diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/AbstractResolveMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/AbstractResolveMojo.java
index 6947b59..e63db81 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/AbstractResolveMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/AbstractResolveMojo.java
@@ -27,12 +27,12 @@ import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.dependency.fromDependencies.AbstractDependencyFilterMojo;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
-import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuildingRequest;
import org.apache.maven.shared.artifact.filter.collection.ArtifactIdFilter;
import org.apache.maven.shared.artifact.filter.collection.ClassifierFilter;
import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
import org.apache.maven.shared.artifact.filter.collection.GroupIdFilter;
+import org.apache.maven.shared.artifact.filter.collection.ScopeFilter;
import org.apache.maven.shared.artifact.filter.collection.TypeFilter;
import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResult;
import org.apache.maven.shared.transfer.dependencies.DependableCoordinate;
@@ -72,7 +72,7 @@ public abstract class AbstractResolveMojo
protected boolean appendOutput;
/**
- * Don't resolve plugins that are in the current reactor. Only works for plugins at the moment.
+ * Don't resolve plugins that are in the current reactor.
*
* @since 2.7
*/
@@ -94,40 +94,19 @@ public abstract class AbstractResolveMojo
/**
* @return {@link FilterArtifacts}
*/
- protected FilterArtifacts getPluginArtifactsFilter()
+ protected FilterArtifacts getArtifactsFilter()
{
+ final FilterArtifacts filter = new FilterArtifacts();
+
if ( excludeReactor )
{
- final StringBuilder exAids = new StringBuilder();
- if ( this.excludeArtifactIds != null )
- {
- exAids.append( this.excludeArtifactIds );
- }
-
- for ( final MavenProject rp : reactorProjects )
- {
- if ( !"maven-plugin".equals( rp.getPackaging() ) )
- {
- continue;
- }
-
- if ( exAids.length() > 0 )
- {
- exAids.append( "," );
- }
-
- exAids.append( rp.getArtifactId() );
- }
-
- this.excludeArtifactIds = exAids.toString();
- }
- final FilterArtifacts filter = new FilterArtifacts();
+ filter.addFilter( new ExcludeReactorProjectsArtifactFilter( reactorProjects, getLog() ) );
+
+ }
- //CHECKSTYLE_OFF: LineLength
- filter.addFilter( new org.apache.maven.shared.artifact.filter.collection.ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
- DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
- //CHECKSTYLE_ON: LineLength
+ filter.addFilter( new ScopeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeScope ),
+ DependencyUtil.cleanToBeTokenizedString( this.excludeScope ) ) );
filter.addFilter( new TypeFilter( DependencyUtil.cleanToBeTokenizedString( this.includeTypes ),
DependencyUtil.cleanToBeTokenizedString( this.excludeTypes ) ) );
diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ExcludeReactorProjectsArtifactFilter.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ExcludeReactorProjectsArtifactFilter.java
new file mode 100644
index 0000000..fbeda30
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ExcludeReactorProjectsArtifactFilter.java
@@ -0,0 +1,95 @@
+package org.apache.maven.plugins.dependency.resolvers;
+
+/*
+ * 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 org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.ArtifactUtils;
+import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.shared.artifact.filter.collection.AbstractArtifactsFilter;
+import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
+
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * {@link ArtifactFilter} implementation that excludes artifacts found in the Reactor.
+ *
+ * @author Maarten Mulders
+ */
+public class ExcludeReactorProjectsArtifactFilter extends AbstractArtifactsFilter
+{
+ private final Log log;
+ private final Set<Artifact> reactorArtifacts;
+
+ public ExcludeReactorProjectsArtifactFilter( final List<MavenProject> reactorProjects, final Log log )
+ {
+ this.log = log;
+ this.reactorArtifacts = new HashSet<>( reactorProjects.size() );
+ for ( final MavenProject project : reactorProjects )
+ {
+ this.reactorArtifacts.add( project.getArtifact() );
+ }
+ }
+
+ @Override
+ public Set<Artifact> filter( final Set<Artifact> artifacts ) throws ArtifactFilterException
+ {
+ final Set<Artifact> results = new LinkedHashSet<>( artifacts.size() );
+
+ for ( final Artifact artifact : artifacts )
+ {
+ if ( !isArtifactInReactor( artifact ) )
+ {
+ results.add( artifact );
+ }
+ else
+ {
+ if ( log.isDebugEnabled() )
+ {
+ log.debug( "Skipped artifact "
+ + ArtifactUtils.key( artifact )
+ + " because it is present in the reactor" );
+ }
+ }
+ }
+
+ return results;
+ }
+
+ private boolean isArtifactInReactor( final Artifact artifact )
+ {
+ for ( final Artifact reactorArtifact : reactorArtifacts )
+ {
+ final String reactorArtifactKey = ArtifactUtils.key( reactorArtifact );
+ final String artifactKey = ArtifactUtils.key( artifact );
+
+ // This check only includes GAV. Should we take a look at the types, too?
+ if ( reactorArtifactKey.equals( artifactKey ) )
+ {
+ return true;
+ }
+ }
+ return false;
+ }
+}
diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java
index df74357..408f9ab 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/GoOfflineMojo.java
@@ -22,27 +22,31 @@ package org.apache.maven.plugins.dependency.resolvers;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.dependency.utils.DependencyUtil;
-import org.apache.maven.plugins.annotations.Execute;
import org.apache.maven.plugins.annotations.Mojo;
-import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.DefaultProjectBuildingRequest;
+import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.shared.artifact.filter.collection.ArtifactFilterException;
import org.apache.maven.shared.artifact.filter.collection.ArtifactsFilter;
+import org.apache.maven.shared.artifact.filter.collection.FilterArtifacts;
+import org.apache.maven.shared.transfer.artifact.resolve.ArtifactResolverException;
+import java.util.LinkedHashSet;
import java.util.Set;
/**
* Goal that resolves all project dependencies, including plugins and reports and their dependencies.
*
- * @author <a href="mailto:brianf@apache.org">Brian Fox</a>
+ * @author Maarten Mulders
* @since 2.0
*/
-@Mojo( name = "go-offline", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
-@Execute( goal = "resolve-plugins" )
+@Mojo( name = "go-offline", threadSafe = true )
public class GoOfflineMojo
extends AbstractResolveMojo
{
/**
- * Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
+ * Main entry into mojo. Gets the list of dependencies, resolves all that are not in the Reactor, and iterates
+ * through displaying the resolved versions.
*
* @throws MojoExecutionException with a message if an error occurs.
*/
@@ -50,15 +54,90 @@ public class GoOfflineMojo
protected void doExecute()
throws MojoExecutionException
{
- Set<Artifact> artifacts = getProject().getArtifacts();
- if ( !isSilent() )
+ try
{
- for ( Artifact artifact : artifacts )
+ final Set<Artifact> plugins = resolvePluginArtifacts();
+
+ final Set<Artifact> dependencies = resolveDependencyArtifacts();
+
+ if ( !isSilent() )
{
- this.getLog().info( "Resolved: " + DependencyUtil.getFormattedFileName( artifact, false ) );
+ for ( Artifact artifact : plugins )
+ {
+ this.getLog().info( "Resolved plugin: "
+ + DependencyUtil.getFormattedFileName( artifact, false ) );
+ }
+
+ for ( Artifact artifact : dependencies )
+ {
+ this.getLog().info( "Resolved dependency: "
+ + DependencyUtil.getFormattedFileName( artifact, false ) );
+ }
}
+
+ }
+ catch ( ArtifactFilterException | ArtifactResolverException e )
+ {
+ throw new MojoExecutionException( e.getMessage(), e );
+ }
+
+ }
+
+ /**
+ * This method resolves the dependency artifacts from the project.
+ *
+ * @return set of resolved dependency artifacts.
+ * @throws ArtifactFilterException in case of an error while filtering the artifacts.
+ * @throws ArtifactResolverException in case of an error while resolving the artifacts.
+ */
+ protected Set<Artifact> resolveDependencyArtifacts()
+ throws ArtifactFilterException, ArtifactResolverException
+ {
+ final Set<Artifact> artifacts = getProject().getDependencyArtifacts();
+
+ return resolveFilteredArtifacts( artifacts );
+ }
+
+ /**
+ * This method resolves the plugin artifacts from the project.
+ *
+ * @return set of resolved plugin artifacts.
+ * @throws ArtifactFilterException in case of an error while filtering the artifacts.
+ * @throws ArtifactResolverException in case of an error while resolving the artifacts.
+ */
+ protected Set<Artifact> resolvePluginArtifacts()
+ throws ArtifactFilterException, ArtifactResolverException
+ {
+ final Set<Artifact> plugins = getProject().getPluginArtifacts();
+ final Set<Artifact> reports = getProject().getReportArtifacts();
+
+ final Set<Artifact> artifacts = new LinkedHashSet<>();
+ artifacts.addAll( reports );
+ artifacts.addAll( plugins );
+
+ return resolveFilteredArtifacts( artifacts );
+ }
+
+ protected Set<Artifact> resolveFilteredArtifacts( final Set<Artifact> artifacts )
+ throws ArtifactFilterException, ArtifactResolverException
+ {
+ final FilterArtifacts filter = getArtifactsFilter();
+ final Set<Artifact> filteredArtifacts = filter.filter( artifacts );
+
+ final Set<Artifact> resolvedArtifacts = new LinkedHashSet<>( artifacts.size() );
+ for ( final Artifact artifact : filteredArtifacts )
+ {
+ final ProjectBuildingRequest buildingRequest =
+ new DefaultProjectBuildingRequest( session.getProjectBuildingRequest() );
+
+ // resolve the new artifact
+ final Artifact resolvedArtifact = getArtifactResolver()
+ .resolveArtifact( buildingRequest, artifact ).getArtifact();
+ resolvedArtifacts.add( resolvedArtifact );
}
+
+ return resolvedArtifacts;
}
@Override
diff --git a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
index 0825cbc..643b004 100644
--- a/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
+++ b/src/main/java/org/apache/maven/plugins/dependency/resolvers/ResolvePluginsMojo.java
@@ -181,7 +181,7 @@ public class ResolvePluginsMojo
artifacts.addAll( reports );
artifacts.addAll( plugins );
- final FilterArtifacts filter = getPluginArtifactsFilter();
+ final FilterArtifacts filter = getArtifactsFilter();
artifacts = filter.filter( artifacts );
Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>( artifacts.size() );