From a8b40f518b3586d8a5e312dad68677a63068bbb2 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 8 Nov 2022 10:22:51 +1000 Subject: [PATCH 1/2] [SHARED-1158] DependencyCollectorBuilder should be more configurable Signed-off-by: Olivier Lamy --- .../graph/DependencyCollectorBuilder.java | 19 +- .../graph/DependencyCollectorRequest.java | 162 ++++++++++++++++++ .../DefaultDependencyCollectorBuilder.java | 34 ++-- 3 files changed, 190 insertions(+), 25 deletions(-) create mode 100644 src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorBuilder.java b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorBuilder.java index 9ece96f..48c0a77 100644 --- a/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorBuilder.java +++ b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorBuilder.java @@ -41,7 +41,22 @@ public interface DependencyCollectorBuilder * @return the raw dependency tree * @throws DependencyCollectorBuilderException if some of the dependencies could not be collected. */ - DependencyNode collectDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter ) - throws DependencyCollectorBuilderException; + default DependencyNode collectDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter ) + throws DependencyCollectorBuilderException + { + return collectDependencyGraph( new DependencyCollectorRequest( buildingRequest, filter ) ); + } + + /** + * collect the project's raw dependency graph, with information to allow the API client to reason on its own about + * dependencies. + * + * @param dependencyCollectorRequest the request with different paramaters. + * @return the raw dependency tree + * @throws DependencyCollectorBuilderException if some of the dependencies could not be collected. + * @since 3.2.1 + */ + DependencyNode collectDependencyGraph( DependencyCollectorRequest dependencyCollectorRequest ) + throws DependencyCollectorBuilderException; } diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java new file mode 100644 index 0000000..a3e932d --- /dev/null +++ b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java @@ -0,0 +1,162 @@ +package org.apache.maven.shared.dependency.graph; + +/* + * 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.resolver.filter.ArtifactFilter; +import org.apache.maven.project.ProjectBuildingRequest; +import org.apache.maven.shared.dependency.graph.internal.DirectScopeDependencySelector; +import org.apache.maven.shared.dependency.graph.internal.VerboseJavaScopeSelector; +import org.eclipse.aether.collection.DependencyGraphTransformer; +import org.eclipse.aether.collection.DependencySelector; +import org.eclipse.aether.util.artifact.JavaScopes; +import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; +import org.eclipse.aether.util.graph.selector.AndDependencySelector; +import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector; +import org.eclipse.aether.util.graph.selector.OptionalDependencySelector; +import org.eclipse.aether.util.graph.transformer.ConflictResolver; +import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; +import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; +import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + *
+ * This class will carry various options used by + * {@link DependencyCollectorBuilder#collectDependencyGraph(DependencyCollectorRequest)} + *
+ *
+ * There is a set of default values such: + *
+ *
+ * DependencySelector + *
+ *  new AndDependencySelector(
+ *             new DirectScopeDependencySelector( JavaScopes.TEST ),
+ *             new DirectScopeDependencySelector( JavaScopes.PROVIDED ),
+ *             new OptionalDependencySelector(),
+ *             new ExclusionDependencySelector() );
+ * 
+ *
+ *
+ * DependencyGraphTransformer + *
+ * new ConflictResolver(
+ *             new NearestVersionSelector(),
+ *             new VerboseJavaScopeSelector(),
+ *             new SimpleOptionalitySelector(),
+ *             new JavaScopeDeriver() );
+ * 
+ *
+ *
+ * configProperties have 2 default values + *
+ *   ConflictResolver.CONFIG_PROP_VERBOSE, true
+ *   DependencyManagerUtils.CONFIG_PROP_VERBOSE, true
+ * 
+ * + *
+ * @since 3.2.1 + */ +public class DependencyCollectorRequest +{ + + private final ProjectBuildingRequest buildingRequest; + + private ArtifactFilter filter; + + private Map configProperties = new HashMap<>(); + + private DependencySelector dependencySelector = new AndDependencySelector( + new DirectScopeDependencySelector( JavaScopes.TEST ), + new DirectScopeDependencySelector( JavaScopes.PROVIDED ), + new OptionalDependencySelector(), + new ExclusionDependencySelector() ); + + private DependencyGraphTransformer dependencyGraphTransformer = new ConflictResolver( + new NearestVersionSelector(), + new VerboseJavaScopeSelector(), + new SimpleOptionalitySelector(), + new JavaScopeDeriver() ); + + public DependencyCollectorRequest( ProjectBuildingRequest buildingRequest ) + { + this( buildingRequest, null ); + } + + public DependencyCollectorRequest( ProjectBuildingRequest buildingRequest, ArtifactFilter filter ) + { + Objects.requireNonNull( buildingRequest, "ProjectBuildingRequest cannot be null" ); + this.buildingRequest = buildingRequest; + this.filter = filter; + configProperties.put( ConflictResolver.CONFIG_PROP_VERBOSE, true ); + configProperties.put( DependencyManagerUtils.CONFIG_PROP_VERBOSE, true ); + } + + public ProjectBuildingRequest getBuildingRequest() + { + return buildingRequest; + } + + public ArtifactFilter getFilter() + { + return filter; + } + + public DependencySelector getDependencySelector() + { + return dependencySelector; + } + + public DependencyCollectorRequest dependencySelector( DependencySelector dependencySelector ) + { + this.dependencySelector = dependencySelector; + return this; + } + + public DependencyGraphTransformer getDependencyGraphTransformer() + { + return dependencyGraphTransformer; + } + + public DependencyCollectorRequest dependencyGraphTransformer( + DependencyGraphTransformer dependencyGraphTransformer ) + { + this.dependencyGraphTransformer = dependencyGraphTransformer; + return this; + } + + public Map getConfigProperties() + { + return this.configProperties; + } + + public void addConfigProperty( String key, Object value ) + { + this.configProperties.put( key, value ); + } + + public void removeConfigProperty( String key ) + { + this.configProperties.remove( key ); + } +} diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyCollectorBuilder.java b/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyCollectorBuilder.java index 5b0218d..b30f782 100644 --- a/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyCollectorBuilder.java +++ b/src/main/java/org/apache/maven/shared/dependency/graph/internal/DefaultDependencyCollectorBuilder.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Map; import javax.inject.Inject; import javax.inject.Named; @@ -35,6 +36,7 @@ import org.apache.maven.project.ProjectBuildingRequest; import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilder; import org.apache.maven.shared.dependency.graph.DependencyCollectorBuilderException; +import org.apache.maven.shared.dependency.graph.DependencyCollectorRequest; import org.apache.maven.shared.dependency.graph.DependencyNode; import org.eclipse.aether.DefaultRepositorySystemSession; import org.eclipse.aether.RepositorySystem; @@ -43,19 +45,10 @@ import org.eclipse.aether.collection.CollectRequest; import org.eclipse.aether.collection.CollectResult; import org.eclipse.aether.collection.DependencyCollectionException; -import org.eclipse.aether.collection.DependencyGraphTransformer; -import org.eclipse.aether.collection.DependencySelector; import org.eclipse.aether.graph.DependencyVisitor; import org.eclipse.aether.graph.Exclusion; -import org.eclipse.aether.util.artifact.JavaScopes; import org.eclipse.aether.util.graph.manager.DependencyManagerUtils; -import org.eclipse.aether.util.graph.selector.AndDependencySelector; -import org.eclipse.aether.util.graph.selector.ExclusionDependencySelector; -import org.eclipse.aether.util.graph.selector.OptionalDependencySelector; import org.eclipse.aether.util.graph.transformer.ConflictResolver; -import org.eclipse.aether.util.graph.transformer.JavaScopeDeriver; -import org.eclipse.aether.util.graph.transformer.NearestVersionSelector; -import org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector; import org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor; import org.eclipse.aether.version.VersionConstraint; import org.slf4j.Logger; @@ -82,12 +75,13 @@ public DefaultDependencyCollectorBuilder( RepositorySystem repositorySystem ) } @Override - public DependencyNode collectDependencyGraph( ProjectBuildingRequest buildingRequest, ArtifactFilter filter ) + public DependencyNode collectDependencyGraph( DependencyCollectorRequest dependencyCollectorRequest ) throws DependencyCollectorBuilderException { DefaultRepositorySystemSession session = null; try { + ProjectBuildingRequest buildingRequest = dependencyCollectorRequest.getBuildingRequest(); MavenProject project = buildingRequest.getProject(); Artifact projectArtifact = project.getArtifact(); @@ -97,20 +91,14 @@ public DependencyNode collectDependencyGraph( ProjectBuildingRequest buildingReq session = new DefaultRepositorySystemSession( repositorySession ); - DependencyGraphTransformer transformer = - new ConflictResolver( new NearestVersionSelector(), new VerboseJavaScopeSelector(), - new SimpleOptionalitySelector(), new JavaScopeDeriver() ); - session.setDependencyGraphTransformer( transformer ); + session.setDependencyGraphTransformer( dependencyCollectorRequest.getDependencyGraphTransformer() ); - DependencySelector depFilter = - new AndDependencySelector( new DirectScopeDependencySelector( JavaScopes.TEST ), - new DirectScopeDependencySelector( JavaScopes.PROVIDED ), - new OptionalDependencySelector(), - new ExclusionDependencySelector() ); - session.setDependencySelector( depFilter ); + session.setDependencySelector( dependencyCollectorRequest.getDependencySelector() ); - session.setConfigProperty( ConflictResolver.CONFIG_PROP_VERBOSE, true ); - session.setConfigProperty( DependencyManagerUtils.CONFIG_PROP_VERBOSE, true ); + for ( Map.Entry entry : dependencyCollectorRequest.getConfigProperties().entrySet() ) + { + session.setConfigProperty( entry.getKey(), entry.getValue() ); + } org.eclipse.aether.artifact.Artifact aetherArtifact = RepositoryUtils.toArtifact( projectArtifact ); @@ -134,7 +122,7 @@ public DependencyNode collectDependencyGraph( ProjectBuildingRequest buildingReq logTree( rootNode ); } - return buildDependencyNode( null, rootNode, projectArtifact, filter ); + return buildDependencyNode( null, rootNode, projectArtifact, dependencyCollectorRequest.getFilter() ); } catch ( DependencyCollectionException e ) { From 8f4c9f63bf985d858e5e11456f612e9479d561cc Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Tue, 8 Nov 2022 21:27:59 +1000 Subject: [PATCH 2/2] add link to more resolver configuration Signed-off-by: Olivier Lamy --- .../shared/dependency/graph/DependencyCollectorRequest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java index a3e932d..450650e 100644 --- a/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java +++ b/src/main/java/org/apache/maven/shared/dependency/graph/DependencyCollectorRequest.java @@ -73,7 +73,7 @@ * ConflictResolver.CONFIG_PROP_VERBOSE, true * DependencyManagerUtils.CONFIG_PROP_VERBOSE, true * - * + * Move Resolver configuration properties. * * @since 3.2.1 */