Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-1158] make DependencyCollectorBuilder more configurable #28

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
olamy marked this conversation as resolved.
Show resolved Hide resolved
*
* </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() );
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well TBH this empty ExclusionDependencySelector doesn't have any effect. we could simply remove it and the code not will not pass tru to do nothing :)


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