Permalink
Browse files

KARAF-424 add option to include top level provided scope dependencies…

… in dependency scan results

git-svn-id: https://svn.apache.org/repos/asf/karaf/trunk@1102132 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
David Jencks
David Jencks committed May 11, 2011
1 parent e5dc319 commit 816bf83355d2cdcc71e21642eec63252dfaa25b1
@@ -20,7 +20,6 @@
package org.apache.karaf.tooling.features;
-import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -29,20 +28,23 @@
import org.apache.maven.RepositoryUtils;
import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.logging.Log;
-import org.apache.maven.plugin.logging.SystemStreamLog;
import org.apache.maven.project.MavenProject;
import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.collection.CollectRequest;
import org.sonatype.aether.collection.CollectResult;
+import org.sonatype.aether.collection.DependencyCollectionContext;
import org.sonatype.aether.collection.DependencyCollectionException;
import org.sonatype.aether.collection.DependencyGraphTransformer;
+import org.sonatype.aether.collection.DependencySelector;
import org.sonatype.aether.graph.Dependency;
import org.sonatype.aether.graph.DependencyNode;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.util.DefaultRepositorySystemSession;
+import org.sonatype.aether.util.graph.selector.AndDependencySelector;
+import org.sonatype.aether.util.graph.selector.OptionalDependencySelector;
+import org.sonatype.aether.util.graph.selector.ScopeDependencySelector;
import org.sonatype.aether.util.graph.transformer.ChainedDependencyGraphTransformer;
import org.sonatype.aether.util.graph.transformer.ConflictMarker;
import org.sonatype.aether.util.graph.transformer.JavaDependencyContextRefiner;
@@ -90,18 +92,21 @@
*/
private final List<RemoteRepository> pluginRepos;
+ private boolean includeTopLevelProvidedScopeDependencies;
+
//dependencies we are interested in
protected Map<Artifact, String> localDependencies;
//log of what happened during search
protected String treeListing;
- public DependencyHelper(List<RemoteRepository> pluginRepos, List<RemoteRepository> projectRepos, RepositorySystemSession repoSession, RepositorySystem repoSystem) {
+ public DependencyHelper(List<RemoteRepository> pluginRepos, List<RemoteRepository> projectRepos, RepositorySystemSession repoSession, RepositorySystem repoSystem, boolean includeTopLevelProvidedScopeDependencies) {
this.pluginRepos = pluginRepos;
this.projectRepos = projectRepos;
this.repoSession = repoSession;
this.repoSystem = repoSystem;
+ this.includeTopLevelProvidedScopeDependencies = includeTopLevelProvidedScopeDependencies;
}
public Map<Artifact, String> getLocalDependencies() {
@@ -126,9 +131,12 @@ public void getDependencies(MavenProject project, boolean useTransitiveDependenc
private DependencyNode getDependencyTree(Artifact artifact) throws MojoExecutionException {
try {
- List<org.sonatype.aether.graph.Dependency> managedArtifacts = new ArrayList<Dependency>();
- CollectRequest collectRequest = new CollectRequest(new org.sonatype.aether.graph.Dependency(artifact, "compile"), null, projectRepos);
+ CollectRequest collectRequest = new CollectRequest(new Dependency(artifact, "compile"), null, projectRepos);
DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(repoSession);
+ if (includeTopLevelProvidedScopeDependencies) {
+ session.setDependencySelector(new AndDependencySelector(new OptionalDependencySelector(),
+ new ScopeDependencySelectorProvider()));
+ }
DependencyGraphTransformer transformer = new ChainedDependencyGraphTransformer(new ConflictMarker(),
new JavaEffectiveScopeCalculator(),
new JavaDependencyContextRefiner());
@@ -140,7 +148,18 @@ private DependencyNode getDependencyTree(Artifact artifact) throws MojoExecution
}
}
+ private static class ScopeDependencySelectorProvider implements DependencySelector {
+
+ private DependencySelector child = new ScopeDependencySelector("test", "provided");
+
+ public boolean selectDependency(Dependency dependency) {
+ throw new IllegalStateException("this does not appear to be called");
+ }
+ public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
+ return child;
+ }
+ }
private static class Scanner {
private static enum Accept {
@@ -185,8 +204,7 @@ private void scan(DependencyNode dependencyNode, Accept parentAccept, boolean us
// Artifact artifact = getArtifact(rootNode);
Accept accept = accept(dependencyNode, parentAccept);
- if (accept.isContinue()) {
- List<DependencyNode> children = dependencyNode.getChildren();
+ if (accept.isLocal()) {
if (isFromFeature) {
if (!isFeature(dependencyNode)) {
log.append(indent).append("from feature:").append(dependencyNode).append("\n");
@@ -206,8 +224,11 @@ private void scan(DependencyNode dependencyNode, Accept parentAccept, boolean us
isFromFeature = true;
}
}
- for (DependencyNode child : children) {
- scan(child, accept, useTransitiveDependencies, isFromFeature, indent + " ");
+ if (accept.isContinue()) {
+ List<DependencyNode> children = dependencyNode.getChildren();
+ for (DependencyNode child : children) {
+ scan(child, accept, useTransitiveDependencies, isFromFeature, indent + " ");
+ }
}
}
}
@@ -222,6 +243,9 @@ private Accept accept(DependencyNode dependency, Accept previous) {
if (scope == null || "runtime".equalsIgnoreCase(scope) || "compile".equalsIgnoreCase(scope)) {
return previous;
}
+ if ("provided".equalsIgnoreCase(scope)) {
+ return Accept.PROVIDED;
+ }
return Accept.STOP;
}
@@ -209,7 +209,7 @@
public void execute() throws MojoExecutionException, MojoFailureException {
try {
- DependencyHelper dependencyHelper = new DependencyHelper(pluginRepos, projectRepos, repoSession, repoSystem);
+ DependencyHelper dependencyHelper = new DependencyHelper(pluginRepos, projectRepos, repoSession, repoSystem, false);
dependencyHelper.getDependencies(project, true);
this.localDependencies = dependencyHelper.getLocalDependencies();
this.treeListing = dependencyHelper.getTreeListing();

0 comments on commit 816bf83

Please sign in to comment.