Search in sources :

Example 1 with ScopeDependencyFilter

use of org.eclipse.aether.util.filter.ScopeDependencyFilter in project sts4 by spring-projects.

the class MavenCore method readDependencyTree.

/**
 * Taken from M2E same named method from MavenModelManager
 *
 * @param repositorySystem
 * @param repositorySession
 * @param mavenProject
 * @param scope
 * @return
 */
private DependencyNode readDependencyTree(org.eclipse.aether.RepositorySystem repositorySystem, RepositorySystemSession repositorySession, MavenProject mavenProject, String scope) {
    DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(repositorySession);
    ConflictResolver transformer = new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver());
    session.setDependencyGraphTransformer(transformer);
    session.setConfigProperty(ConflictResolver.CONFIG_PROP_VERBOSE, Boolean.toString(true));
    session.setConfigProperty(DependencyManagerUtils.CONFIG_PROP_VERBOSE, true);
    ArtifactTypeRegistry stereotypes = session.getArtifactTypeRegistry();
    CollectRequest request = new CollectRequest();
    // $NON-NLS-1$
    request.setRequestContext("project");
    request.setRepositories(mavenProject.getRemoteProjectRepositories());
    for (org.apache.maven.model.Dependency dependency : mavenProject.getDependencies()) {
        request.addDependency(RepositoryUtils.toDependency(dependency, stereotypes));
    }
    DependencyManagement depMngt = mavenProject.getDependencyManagement();
    if (depMngt != null) {
        for (org.apache.maven.model.Dependency dependency : depMngt.getDependencies()) {
            request.addManagedDependency(RepositoryUtils.toDependency(dependency, stereotypes));
        }
    }
    DependencyNode node;
    try {
        node = repositorySystem.collectDependencies(session, request).getRoot();
    } catch (DependencyCollectionException ex) {
        node = ex.getResult().getRoot();
    }
    Collection<String> scopes = new HashSet<String>();
    Collections.addAll(scopes, Artifact.SCOPE_SYSTEM, Artifact.SCOPE_COMPILE, Artifact.SCOPE_PROVIDED, Artifact.SCOPE_RUNTIME, Artifact.SCOPE_TEST);
    if (Artifact.SCOPE_COMPILE.equals(scope)) {
        scopes.remove(Artifact.SCOPE_COMPILE);
        scopes.remove(Artifact.SCOPE_SYSTEM);
        scopes.remove(Artifact.SCOPE_PROVIDED);
    } else if (Artifact.SCOPE_RUNTIME.equals(scope)) {
        scopes.remove(Artifact.SCOPE_COMPILE);
        scopes.remove(Artifact.SCOPE_RUNTIME);
    } else if (Artifact.SCOPE_COMPILE_PLUS_RUNTIME.equals(scope)) {
        scopes.remove(Artifact.SCOPE_COMPILE);
        scopes.remove(Artifact.SCOPE_SYSTEM);
        scopes.remove(Artifact.SCOPE_PROVIDED);
        scopes.remove(Artifact.SCOPE_RUNTIME);
    } else {
        scopes.clear();
    }
    CloningDependencyVisitor cloner = new CloningDependencyVisitor();
    node.accept(new FilteringDependencyVisitor(cloner, new ScopeDependencyFilter(null, scopes)));
    node = cloner.getRootNode();
    return node;
}
Also used : DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) CloningDependencyVisitor(org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor) FilteringDependencyVisitor(org.eclipse.aether.util.graph.visitor.FilteringDependencyVisitor) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) CollectRequest(org.eclipse.aether.collection.CollectRequest) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactTypeRegistry(org.eclipse.aether.artifact.ArtifactTypeRegistry) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) DependencyNode(org.eclipse.aether.graph.DependencyNode) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) DependencyManagement(org.apache.maven.model.DependencyManagement) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 2 with ScopeDependencyFilter

use of org.eclipse.aether.util.filter.ScopeDependencyFilter in project grails-maven by grails.

the class AbstractGrailsMojo method resolveArtifacts.

protected List<File> resolveArtifacts(MavenProject mavenProject, Collection<String> scopes, DependencyFilter filter) throws MojoExecutionException {
    try {
        DefaultDependencyResolutionRequest request = new DefaultDependencyResolutionRequest(mavenProject, repoSession);
        if (filter != null) {
            request.setResolutionFilter(new AndDependencyFilter(new ScopeDependencyFilter(scopes, Collections.<String>emptyList()), filter));
        } else {
            request.setResolutionFilter(new ScopeDependencyFilter(scopes, Collections.<String>emptyList()));
        }
        DependencyResolutionResult result = projectDependenciesResolver.resolve(request);
        List<org.eclipse.aether.graph.Dependency> dependencies = result.getDependencies();
        final List<File> files = new ArrayList<File>();
        for (org.eclipse.aether.graph.Dependency d : dependencies) {
            org.eclipse.aether.artifact.Artifact artifact = d.getArtifact();
            File file = artifact.getFile();
            if (file != null) {
                String name = file.getName();
                if (!name.contains("xml-apis") && !name.contains("commons-logging"))
                    files.add(file);
            }
        }
        return files;
    } catch (DependencyResolutionException e) {
        throw new MojoExecutionException("Dependency resolution failure: " + e.getMessage(), e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Dependency(org.apache.maven.model.Dependency) AndDependencyFilter(org.eclipse.aether.util.filter.AndDependencyFilter) ScopeDependencyFilter(org.eclipse.aether.util.filter.ScopeDependencyFilter) File(java.io.File)

Aggregations

ScopeDependencyFilter (org.eclipse.aether.util.filter.ScopeDependencyFilter)2 File (java.io.File)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Dependency (org.apache.maven.model.Dependency)1 DependencyManagement (org.apache.maven.model.DependencyManagement)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)1 ArtifactTypeRegistry (org.eclipse.aether.artifact.ArtifactTypeRegistry)1 CollectRequest (org.eclipse.aether.collection.CollectRequest)1 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)1 DependencyNode (org.eclipse.aether.graph.DependencyNode)1 AndDependencyFilter (org.eclipse.aether.util.filter.AndDependencyFilter)1 ConflictResolver (org.eclipse.aether.util.graph.transformer.ConflictResolver)1 JavaScopeDeriver (org.eclipse.aether.util.graph.transformer.JavaScopeDeriver)1 JavaScopeSelector (org.eclipse.aether.util.graph.transformer.JavaScopeSelector)1 NearestVersionSelector (org.eclipse.aether.util.graph.transformer.NearestVersionSelector)1 SimpleOptionalitySelector (org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector)1 CloningDependencyVisitor (org.eclipse.aether.util.graph.visitor.CloningDependencyVisitor)1 FilteringDependencyVisitor (org.eclipse.aether.util.graph.visitor.FilteringDependencyVisitor)1