Search in sources :

Example 16 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode 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 17 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project byte-buddy by raphw.

the class ClassLoaderResolver method doResolve.

/**
     * Resolves a Maven coordinate to a class loader that can load all of the coordinates classes.
     *
     * @param mavenCoordinate The Maven coordinate to resolve.
     * @return A class loader that references all of the class loader's dependencies and which is a child of this class's class loader.
     * @throws MojoExecutionException If the user configuration results in an error.
     * @throws MojoFailureException   If the plugin application raises an error.
     */
private ClassLoader doResolve(MavenCoordinate mavenCoordinate) throws MojoExecutionException, MojoFailureException {
    List<URL> urls = new ArrayList<URL>();
    log.info("Resolving transformer dependency: " + mavenCoordinate);
    try {
        DependencyNode root = repositorySystem.collectDependencies(repositorySystemSession, new CollectRequest(new Dependency(mavenCoordinate.asArtifact(), "runtime"), remoteRepositories)).getRoot();
        repositorySystem.resolveDependencies(repositorySystemSession, new DependencyRequest().setRoot(root));
        PreorderNodeListGenerator preorderNodeListGenerator = new PreorderNodeListGenerator();
        root.accept(preorderNodeListGenerator);
        for (Artifact artifact : preorderNodeListGenerator.getArtifacts(false)) {
            urls.add(artifact.getFile().toURI().toURL());
        }
    } catch (DependencyCollectionException exception) {
        throw new MojoExecutionException("Could not collect dependencies for " + mavenCoordinate, exception);
    } catch (DependencyResolutionException exception) {
        throw new MojoExecutionException("Could not resolve dependencies for " + mavenCoordinate, exception);
    } catch (MalformedURLException exception) {
        throw new MojoFailureException("Could not resolve file as URL for " + mavenCoordinate, exception);
    }
    return new URLClassLoader(urls.toArray(new URL[urls.size()]), ByteBuddy.class.getClassLoader());
}
Also used : DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ByteBuddy(net.bytebuddy.ByteBuddy) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) URL(java.net.URL) Artifact(org.eclipse.aether.artifact.Artifact) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) URLClassLoader(java.net.URLClassLoader) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException)

Example 18 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project bnd by bndtools.

the class DependencyResolver method resolve.

public Map<File, ArtifactResult> resolve() throws MojoExecutionException {
    if (resolvedDependencies != null) {
        return resolvedDependencies;
    }
    DependencyResolutionRequest request = new DefaultDependencyResolutionRequest(project, session);
    request.setResolutionFilter(new DependencyFilter() {

        @Override
        public boolean accept(DependencyNode node, List<DependencyNode> parents) {
            if (node.getDependency() != null) {
                return scopes.contains(node.getDependency().getScope());
            }
            return false;
        }
    });
    DependencyResolutionResult result;
    try {
        result = resolver.resolve(request);
    } catch (DependencyResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    Map<File, ArtifactResult> dependencies = new HashMap<>();
    DependencyNode dependencyGraph = result.getDependencyGraph();
    if (dependencyGraph != null && !dependencyGraph.getChildren().isEmpty()) {
        List<RemoteRepository> remoteRepositories = new ArrayList<>(project.getRemoteProjectRepositories());
        ArtifactRepository deployRepo = project.getDistributionManagementArtifactRepository();
        if (deployRepo != null) {
            remoteRepositories.add(RepositoryUtils.toRepo(deployRepo));
        }
        discoverArtifacts(dependencies, dependencyGraph.getChildren(), project.getArtifact().getId(), remoteRepositories);
    }
    return resolvedDependencies = dependencies;
}
Also used : DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) File(java.io.File)

Example 19 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project meecrowave by apache.

the class MeecrowaveBundleMojo method execute.

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    if (skip) {
        getLog().warn(getClass().getSimpleName() + " skipped");
        return;
    }
    final File distroFolder = new File(buildDirectory, rootName == null ? artifactId + "-distribution" : rootName);
    if (distroFolder.exists()) {
        delete(distroFolder);
    }
    Stream.of("bin", "conf", "logs", "lib").forEach(i -> new File(distroFolder, i).mkdirs());
    // TODO: add .bat support
    for (final String ext : asList("sh", "bat")) {
        try (final BufferedReader reader = new BufferedReader(new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream("bin/meecrowave." + ext)))) {
            write(new File(distroFolder, "bin/meecrowave." + ext), StrSubstitutor.replace(reader.lines().collect(joining("\n")), new HashMap<String, String>() {

                {
                    put("main", main);
                }
            }));
        } catch (final IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    copyProvidedFiles(distroFolder);
    write(new File(distroFolder, "logs/you_can_safely_delete.txt"), DELETE_TEXT);
    final Collection<String> includedArtifacts = project.getArtifacts().stream().filter(this::isIncluded).map(a -> {
        addLib(distroFolder, a.getFile());
        return a.getArtifactId();
    }).collect(toList());
    if (app.exists()) {
        addLib(distroFolder, app);
    }
    if (enforceCommonsCli && !includedArtifacts.contains("commons-cli")) {
        addLib(distroFolder, resolve("commons-cli", "commons-cli", "1.4", ""));
    }
    if (libs != null) {
        libs.forEach(l -> {
            final String[] c = l.split(":");
            if (c.length != 3 && c.length != 4) {
                throw new IllegalArgumentException("libs syntax is groupId:artifactId:version[:classifier]");
            }
            addLib(distroFolder, resolve(c[0], c[1], c[2], c.length == 4 ? c[3] : ""));
        });
    }
    if (enforceMeecrowave && !includedArtifacts.contains("meecrowave-core")) {
        final DependencyResolutionRequest request = new DefaultDependencyResolutionRequest();
        request.setMavenProject(new MavenProject() {

            {
                getDependencies().add(new Dependency() {

                    {
                        setGroupId("org.apache.meecrowave");
                        setArtifactId("meecrowave-core");
                        setVersion(findVersion());
                    }
                });
            }
        });
        request.setRepositorySession(session);
        try {
            dependenciesResolver.resolve(request).getDependencyGraph().accept(new DependencyVisitor() {

                @Override
                public boolean visitEnter(final DependencyNode node) {
                    return true;
                }

                @Override
                public boolean visitLeave(final DependencyNode node) {
                    final org.eclipse.aether.artifact.Artifact artifact = node.getArtifact();
                    if (artifact != null && !includedArtifacts.contains(artifact.getArtifactId())) {
                        addLib(distroFolder, artifact.getFile());
                    }
                    return true;
                }
            });
        } catch (final DependencyResolutionException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    final Path prefix = skipArchiveRootFolder ? distroFolder.toPath() : distroFolder.getParentFile().toPath();
    for (final String format : formats) {
        getLog().info(format + "-ing Custom Meecrowave Distribution");
        final File output = new File(buildDirectory, artifactId + "-meecrowave-distribution." + format);
        switch(format.toLowerCase(ENGLISH)) {
            case "tar.gz":
                try (final TarArchiveOutputStream tarGz = new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(output)))) {
                    tarGz.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
                    for (final String entry : distroFolder.list()) {
                        tarGz(tarGz, new File(distroFolder, entry), prefix);
                    }
                } catch (final IOException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
                break;
            case "zip":
                try (final ZipArchiveOutputStream zos = new ZipArchiveOutputStream(new FileOutputStream(output))) {
                    for (final String entry : distroFolder.list()) {
                        zip(zos, new File(distroFolder, entry), prefix);
                    }
                } catch (final IOException e) {
                    throw new MojoExecutionException(e.getMessage(), e);
                }
                break;
            default:
                throw new IllegalArgumentException(format + " is not supported");
        }
        attach(format, output);
    }
    if (!keepExplodedFolder) {
        delete(distroFolder);
    }
}
Also used : ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) Parameter(org.apache.maven.plugins.annotations.Parameter) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) MavenProject(org.apache.maven.project.MavenProject) Arrays.asList(java.util.Arrays.asList) ProjectDependenciesResolver(org.apache.maven.project.ProjectDependenciesResolver) DependencyVisitor(org.eclipse.aether.graph.DependencyVisitor) Artifact(org.apache.maven.artifact.Artifact) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) Path(java.nio.file.Path) ENGLISH(java.util.Locale.ENGLISH) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) Collection(java.util.Collection) StandardOpenOption(java.nio.file.StandardOpenOption) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Stream(java.util.stream.Stream) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) GZIPOutputStream(java.util.zip.GZIPOutputStream) AbstractMojo(org.apache.maven.plugin.AbstractMojo) RepositorySystem(org.eclipse.aether.RepositorySystem) MavenProjectHelper(org.apache.maven.project.MavenProjectHelper) Dependency(org.apache.maven.model.Dependency) Component(org.apache.maven.plugins.annotations.Component) HashMap(java.util.HashMap) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) StandardCopyOption(java.nio.file.StandardCopyOption) Mojo(org.apache.maven.plugins.annotations.Mojo) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Properties(java.util.Properties) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Log(org.apache.maven.plugin.logging.Log) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) InputStreamReader(java.io.InputStreamReader) File(java.io.File) MojoFailureException(org.apache.maven.plugin.MojoFailureException) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RUNTIME_PLUS_SYSTEM(org.apache.maven.plugins.annotations.ResolutionScope.RUNTIME_PLUS_SYSTEM) Collectors.toList(java.util.stream.Collectors.toList) ArtifactResolver(org.eclipse.aether.impl.ArtifactResolver) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) BufferedReader(java.io.BufferedReader) InputStream(java.io.InputStream) HashMap(java.util.HashMap) MavenProject(org.apache.maven.project.MavenProject) GZIPOutputStream(java.util.zip.GZIPOutputStream) DependencyVisitor(org.eclipse.aether.graph.DependencyVisitor) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ZipArchiveOutputStream(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream) IOException(java.io.IOException) Dependency(org.apache.maven.model.Dependency) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) TarArchiveOutputStream(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream) File(java.io.File)

Example 20 with DependencyNode

use of org.eclipse.aether.graph.DependencyNode in project fabric8 by jboss-fuse.

the class AetherBasedResolver method resolveDependencies.

protected DependencyNode resolveDependencies(RepositorySystemSession session, List<RemoteRepository> repos, DependencyNode pomNode, Dependency dependency, final Filter<Dependency> shouldExclude) throws FailedToResolveDependency {
    if (!DependencyFilters.matches(dependency, shouldExclude)) {
        CollectRequest cr = new CollectRequest(dependency, repos);
        // request.setRequestContext("runtime");
        try {
            DependencyNode node = m_repoSystem.collectDependencies(session, cr).getRoot();
            DependencyFilter filter = new DependencyFilter() {

                public boolean accept(DependencyNode node, List<DependencyNode> parents) {
                    return !DependencyFilters.matches(node, shouldExclude);
                }
            };
            DependencyRequest request = new DependencyRequest(cr, filter);
            m_repoSystem.resolveDependencies(session, request);
            return node;
        } catch (DependencyResolutionException | DependencyCollectionException e) {
            handleDependencyResolveFailure(pomNode, dependency, e);
        }
    }
    return null;
}
Also used : DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) CollectRequest(org.eclipse.aether.collection.CollectRequest)

Aggregations

DependencyNode (org.eclipse.aether.graph.DependencyNode)28 Artifact (org.eclipse.aether.artifact.Artifact)14 CollectRequest (org.eclipse.aether.collection.CollectRequest)14 Dependency (org.eclipse.aether.graph.Dependency)13 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)12 ArrayList (java.util.ArrayList)11 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)10 File (java.io.File)8 List (java.util.List)8 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)8 DependencyResolutionException (org.eclipse.aether.resolution.DependencyResolutionException)8 IOException (java.io.IOException)7 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)7 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)7 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)6 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)6 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)5 PreorderNodeListGenerator (org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator)5 MalformedURLException (java.net.MalformedURLException)4 RepositorySystem (org.eclipse.aether.RepositorySystem)4