Search in sources :

Example 1 with PreorderNodeListGenerator

use of org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator 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 2 with PreorderNodeListGenerator

use of org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator in project intellij-community by JetBrains.

the class Maven3ServerEmbedderImpl method resolvePlugin.

@Override
public Collection<MavenArtifact> resolvePlugin(@NotNull final MavenPlugin plugin, @NotNull final List<MavenRemoteRepository> repositories, int nativeMavenProjectId, final boolean transitive) throws RemoteException, MavenServerProcessCanceledException {
    try {
        Plugin mavenPlugin = new Plugin();
        mavenPlugin.setGroupId(plugin.getGroupId());
        mavenPlugin.setArtifactId(plugin.getArtifactId());
        mavenPlugin.setVersion(plugin.getVersion());
        MavenProject project = RemoteNativeMavenProjectHolder.findProjectById(nativeMavenProjectId);
        Plugin pluginFromProject = project.getBuild().getPluginsAsMap().get(plugin.getGroupId() + ':' + plugin.getArtifactId());
        if (pluginFromProject != null) {
            mavenPlugin.setDependencies(pluginFromProject.getDependencies());
        }
        final MavenExecutionRequest request = createRequest(null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList());
        DefaultMaven maven = (DefaultMaven) getComponent(Maven.class);
        RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);
        PluginDependenciesResolver pluginDependenciesResolver = getComponent(PluginDependenciesResolver.class);
        org.eclipse.aether.artifact.Artifact pluginArtifact = pluginDependenciesResolver.resolve(mavenPlugin, project.getRemotePluginRepositories(), repositorySystemSession);
        org.eclipse.aether.graph.DependencyNode node = pluginDependenciesResolver.resolve(mavenPlugin, pluginArtifact, null, project.getRemotePluginRepositories(), repositorySystemSession);
        PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
        node.accept(nlg);
        List<MavenArtifact> res = new ArrayList<MavenArtifact>();
        for (org.eclipse.aether.artifact.Artifact artifact : nlg.getArtifacts(true)) {
            if (!Comparing.equal(artifact.getArtifactId(), plugin.getArtifactId()) || !Comparing.equal(artifact.getGroupId(), plugin.getGroupId())) {
                res.add(MavenModelConverter.convertArtifact(RepositoryUtils.toArtifact(artifact), getLocalRepositoryFile()));
            }
        }
        return res;
    } catch (Exception e) {
        Maven3ServerGlobals.getLogger().info(e);
        return Collections.emptyList();
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) InitializationException(org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException) ModelInterpolationException(org.apache.maven.project.interpolation.ModelInterpolationException) RemoteException(java.rmi.RemoteException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ContextException(org.codehaus.plexus.context.ContextException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) PluginDependenciesResolver(org.apache.maven.plugin.internal.PluginDependenciesResolver) Plugin(org.apache.maven.model.Plugin)

Aggregations

PreorderNodeListGenerator (org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 RemoteException (java.rmi.RemoteException)1 ArrayList (java.util.ArrayList)1 ByteBuddy (net.bytebuddy.ByteBuddy)1 InvalidRepositoryException (org.apache.maven.artifact.InvalidRepositoryException)1 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)1 ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)1 Plugin (org.apache.maven.model.Plugin)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 PluginDependenciesResolver (org.apache.maven.plugin.internal.PluginDependenciesResolver)1 ModelInterpolationException (org.apache.maven.project.interpolation.ModelInterpolationException)1 ComponentLookupException (org.codehaus.plexus.component.repository.exception.ComponentLookupException)1 ContextException (org.codehaus.plexus.context.ContextException)1 InitializationException (org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException)1 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)1 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)1