Skip to content

Commit

Permalink
[MSHARED-1158] make DependencyCollectorBuilder nore configurable (#28)
Browse files Browse the repository at this point in the history
* [SHARED-1158] DependencyCollectorBuilder should be more configurable

Signed-off-by: Olivier Lamy <olamy@apache.org>
  • Loading branch information
olamy authored Nov 11, 2022
1 parent fa81a70 commit 2ef02ce
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
Original file line number Diff line number Diff line change
@@ -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;

/**
* <div>
* This class will carry various options used by
* {@link DependencyCollectorBuilder#collectDependencyGraph(DependencyCollectorRequest)}
* </div>
* <div>
* There is a set of default values such:
* </div>
* <div>
* DependencySelector
* <pre>
* new AndDependencySelector(
* new DirectScopeDependencySelector( JavaScopes.TEST ),
* new DirectScopeDependencySelector( JavaScopes.PROVIDED ),
* new OptionalDependencySelector(),
* new ExclusionDependencySelector() );
* </pre>
* </div>
* <div>
* DependencyGraphTransformer
* <pre>
* new ConflictResolver(
* new NearestVersionSelector(),
* new VerboseJavaScopeSelector(),
* new SimpleOptionalitySelector(),
* new JavaScopeDeriver() );
* </pre>
* </div>
* <div>
* configProperties have 2 default values
* <pre>
* ConflictResolver.CONFIG_PROP_VERBOSE, true
* DependencyManagerUtils.CONFIG_PROP_VERBOSE, true
* </pre>
* <a href="https://maven.apache.org/resolver/configuration.html">Move Resolver configuration properties</a>.
* </div>
* @since 3.2.1
*/
public class DependencyCollectorRequest
{

private final ProjectBuildingRequest buildingRequest;

private ArtifactFilter filter;

private Map<String, Object> 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<String, Object> 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 );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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<String, Object> entry : dependencyCollectorRequest.getConfigProperties().entrySet() )
{
session.setConfigProperty( entry.getKey(), entry.getValue() );
}

org.eclipse.aether.artifact.Artifact aetherArtifact = RepositoryUtils.toArtifact( projectArtifact );

Expand All @@ -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 )
{
Expand Down

0 comments on commit 2ef02ce

Please sign in to comment.