Search in sources :

Example 1 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project buck by facebook.

the class Resolver method getDependenciesOf.

private List<Dependency> getDependenciesOf(Artifact dep) throws ArtifactDescriptorException {
    ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
    descriptorRequest.setArtifact(dep);
    descriptorRequest.setRepositories(repos);
    descriptorRequest.setRequestContext(JavaScopes.RUNTIME);
    ArtifactDescriptorResult result = repoSys.readArtifactDescriptor(session, descriptorRequest);
    return result.getDependencies();
}
Also used : ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest)

Example 2 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project BIMserver by opensourceBIM.

the class PluginManager method loadDependencies.

private PublicFindClassClassLoader loadDependencies(Set<org.bimserver.plugins.Dependency> bimServerDependencies, Model model, PublicFindClassClassLoader previous) throws FileNotFoundException, IOException {
    List<org.apache.maven.model.Dependency> dependencies = model.getDependencies();
    Iterator<org.apache.maven.model.Dependency> it = dependencies.iterator();
    Path workspaceDir = Paths.get("..");
    bimServerDependencies.add(new org.bimserver.plugins.Dependency(workspaceDir.resolve("PluginBase/target/classes")));
    bimServerDependencies.add(new org.bimserver.plugins.Dependency(workspaceDir.resolve("Shared/target/classes")));
    while (it.hasNext()) {
        org.apache.maven.model.Dependency depend = it.next();
        try {
            if (depend.getGroupId().equals("org.opensourcebim") && (depend.getArtifactId().equals("shared") || depend.getArtifactId().equals("pluginbase"))) {
                // TODO we might want to check the version though
                continue;
            }
            if (depend.isOptional() || "test".equals(depend.getScope())) {
                continue;
            }
            Dependency dependency2 = new Dependency(new DefaultArtifact(depend.getGroupId() + ":" + depend.getArtifactId() + ":jar:" + depend.getVersion()), "compile");
            DelegatingClassLoader depDelLoader = new DelegatingClassLoader(previous);
            if (!dependency2.getArtifact().isSnapshot()) {
                if (dependency2.getArtifact().getFile() != null) {
                    bimServerDependencies.add(new org.bimserver.plugins.Dependency(dependency2.getArtifact().getFile().toPath()));
                    loadDependencies(dependency2.getArtifact().getFile().toPath(), depDelLoader);
                } else {
                    ArtifactRequest request = new ArtifactRequest();
                    request.setArtifact(dependency2.getArtifact());
                    request.setRepositories(mavenPluginRepository.getRepositories());
                    try {
                        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
                        if (resolveArtifact.getArtifact().getFile() != null) {
                            bimServerDependencies.add(new org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                            loadDependencies(resolveArtifact.getArtifact().getFile().toPath(), depDelLoader);
                        } else {
                        // TODO error?
                        }
                    } catch (ArtifactResolutionException e) {
                        e.printStackTrace();
                    }
                }
            } else {
                // Snapshot projects linked in Eclipse
                ArtifactRequest request = new ArtifactRequest();
                if ((!"test".equals(dependency2.getScope()) && !dependency2.getArtifact().isSnapshot())) {
                    request.setArtifact(dependency2.getArtifact());
                    request.setRepositories(mavenPluginRepository.getLocalRepositories());
                    try {
                        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
                        if (resolveArtifact.getArtifact().getFile() != null) {
                            bimServerDependencies.add(new org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                            loadDependencies(resolveArtifact.getArtifact().getFile().toPath(), depDelLoader);
                        } else {
                        // TODO error?
                        }
                    } catch (Exception e) {
                        LOGGER.info(dependency2.getArtifact().toString());
                        e.printStackTrace();
                    }
                // bimServerDependencies.add(new
                // org.bimserver.plugins.Dependency(resolveArtifact.getArtifact().getFile().toPath()));
                }
            }
            ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
            descriptorRequest.setArtifact(dependency2.getArtifact());
            descriptorRequest.setRepositories(mavenPluginRepository.getRepositories());
            ArtifactDescriptorResult descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
            CollectRequest collectRequest = new CollectRequest();
            collectRequest.setRootArtifact(descriptorResult.getArtifact());
            collectRequest.setDependencies(descriptorResult.getDependencies());
            collectRequest.setManagedDependencies(descriptorResult.getManagedDependencies());
            collectRequest.setRepositories(descriptorResult.getRepositories());
            DependencyNode node = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest).getRoot();
            DependencyRequest dependencyRequest = new DependencyRequest();
            dependencyRequest.setRoot(node);
            CollectResult collectResult = mavenPluginRepository.getSystem().collectDependencies(mavenPluginRepository.getSession(), collectRequest);
            PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
            // collectResult.getRoot().accept(new
            // ConsoleDependencyGraphDumper());
            collectResult.getRoot().accept(nlg);
            try {
                mavenPluginRepository.getSystem().resolveDependencies(mavenPluginRepository.getSession(), dependencyRequest);
            } catch (DependencyResolutionException e) {
            // Ignore
            }
            for (DependencyNode dependencyNode : nlg.getNodes()) {
                ArtifactRequest newRequest = new ArtifactRequest(dependencyNode);
                newRequest.setRepositories(mavenPluginRepository.getRepositories());
                ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), newRequest);
                Artifact artifact = resolveArtifact.getArtifact();
                Path jarFile = Paths.get(artifact.getFile().getAbsolutePath());
                loadDependencies(jarFile, depDelLoader);
                Artifact versionArtifact = new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), "pom", artifact.getVersion());
                ArtifactRequest request = new ArtifactRequest();
                request.setArtifact(versionArtifact);
                request.setRepositories(mavenPluginRepository.getRepositories());
                // try {
                // ArtifactResult resolveArtifact =
                // mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(),
                // request);
                // File depPomFile =
                // resolveArtifact.getArtifact().getFile();
                // if (depPomFile != null) {
                // MavenXpp3Reader mavenreader = new MavenXpp3Reader();
                // Model depModel = null;
                // try (FileReader reader = new FileReader(depPomFile)) {
                // try {
                // depModel = mavenreader.read(reader);
                // } catch (XmlPullParserException e) {
                // e.printStackTrace();
                // }
                // }
                // previous = loadDependencies(bimServerDependencies,
                // depModel, previous);
                // } else {
                // LOGGER.info("Artifact not found " + versionArtifact);
                // }
                // } catch (ArtifactResolutionException e1) {
                // LOGGER.error(e1.getMessage());
                // }
                // EclipsePluginClassloader depLoader = new
                // EclipsePluginClassloader(depDelLoader, projectRoot);
                bimServerDependencies.add(new org.bimserver.plugins.Dependency(jarFile));
            }
            previous = depDelLoader;
        } catch (DependencyCollectionException e) {
            e.printStackTrace();
        } catch (ArtifactDescriptorException e2) {
            e2.printStackTrace();
        } catch (ArtifactResolutionException e) {
            e.printStackTrace();
        }
    }
    return previous;
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) Path(java.nio.file.Path) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) CollectResult(org.eclipse.aether.collection.CollectResult) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DelegatingClassLoader(org.bimserver.plugins.classloaders.DelegatingClassLoader) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) ObjectIDMException(org.bimserver.plugins.objectidms.ObjectIDMException) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) ServiceException(org.bimserver.shared.exceptions.ServiceException) IOException(java.io.IOException) PluginException(org.bimserver.shared.exceptions.PluginException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) UserException(org.bimserver.shared.exceptions.UserException) ChannelConnectionException(org.bimserver.shared.ChannelConnectionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 3 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project fabric8 by jboss-fuse.

the class AetherBasedResolver method collectDependencies.

protected DependencyNode collectDependencies(Artifact root, String pomVersion, final Filter<Dependency> excludeDependencyFilter) throws RepositoryException, IOException {
    final DefaultRepositorySystemSession session = newSession();
    try {
        List<RemoteRepository> repos = selectRepositories();
        assignProxyAndMirrors(repos);
        ArtifactDescriptorResult artifactDescriptorResult = m_repoSystem.readArtifactDescriptor(session, new ArtifactDescriptorRequest(root, repos, null));
        repos.addAll(artifactDescriptorResult.getRepositories());
        Dependency rootDependency = new Dependency(root, null);
        List<Dependency> dependencies = artifactDescriptorResult.getDependencies();
        final DefaultDependencyNode rootNode = new DefaultDependencyNode(rootDependency);
        GenericVersionScheme versionScheme = new GenericVersionScheme();
        rootNode.setVersion(versionScheme.parseVersion(pomVersion));
        rootNode.setVersionConstraint(versionScheme.parseVersionConstraint(pomVersion));
        DependencyNode pomNode = rootNode;
        // final Filter<Dependency> shouldExclude = Filters.or(DependencyFilters.testScopeFilter, excludeDependencyFilter, new NewerVersionExistsFilter(rootNode));
        final Filter<Dependency> shouldExclude = Filters.or(Arrays.asList(DependencyFilters.testScopeFilter, excludeDependencyFilter));
        DependencySelector dependencySelector = new AndDependencySelector(new ScopeDependencySelector("test"), new ExclusionDependencySelector(), new DependencySelector() {

            @Override
            public DependencySelector deriveChildSelector(DependencyCollectionContext context) {
                return this;
            }

            @Override
            public boolean selectDependency(Dependency dependency) {
                try {
                    return !DependencyFilters.matches(dependency, shouldExclude);
                } catch (Exception e) {
                    failedToMakeDependencyTree(dependency, e);
                    return false;
                }
            }
        });
        session.setDependencySelector(dependencySelector);
        // work on the root dependency directly?
        if (true) {
            for (Dependency dependency : dependencies) {
                DependencyNode node = resolveDependencies(session, repos, pomNode, dependency, shouldExclude);
                if (node != null) {
                    pomNode.getChildren().add(node);
                }
            }
        } else {
            DependencyNode node = resolveDependencies(session, repos, pomNode, rootDependency, shouldExclude);
            if (node != null) {
                pomNode = node;
            }
        }
        // now lets transform the dependency tree to remove different versions for the same artifact
        final DependencyGraphTransformationContext tranformContext = new DependencyGraphTransformationContext() {

            Map<Object, Object> map = new HashMap<>();

            public RepositorySystemSession getSession() {
                return session;
            }

            public Object get(Object key) {
                return map.get(key);
            }

            public Object put(Object key, Object value) {
                return map.put(key, value);
            }
        };
        DependencyGraphTransformer transformer = new ReplaceConflictingVersionResolver();
        pomNode = transformer.transformGraph(pomNode, tranformContext);
        transformer = new DuplicateTransformer();
        pomNode = transformer.transformGraph(pomNode, tranformContext);
        return pomNode;
    } finally {
        releaseSession(session);
    }
}
Also used : DependencyGraphTransformationContext(org.eclipse.aether.collection.DependencyGraphTransformationContext) ReplaceConflictingVersionResolver(io.fabric8.maven.ReplaceConflictingVersionResolver) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DuplicateTransformer(io.fabric8.maven.DuplicateTransformer) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DependencyNode(org.eclipse.aether.graph.DependencyNode) ChainedDependencyGraphTransformer(org.eclipse.aether.util.graph.transformer.ChainedDependencyGraphTransformer) DependencyGraphTransformer(org.eclipse.aether.collection.DependencyGraphTransformer) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) ScopeDependencySelector(org.eclipse.aether.util.graph.selector.ScopeDependencySelector) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) DependencyCollectionContext(org.eclipse.aether.collection.DependencyCollectionContext) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) Dependency(org.eclipse.aether.graph.Dependency) FailedToResolveDependency(io.fabric8.maven.FailedToResolveDependency) DependencyCollectionException(org.eclipse.aether.collection.DependencyCollectionException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) ArtifactNotFoundException(org.eclipse.aether.transfer.ArtifactNotFoundException) RepositoryException(org.eclipse.aether.RepositoryException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) MetadataTransferException(org.eclipse.aether.transfer.MetadataTransferException) NoRouteToHostException(java.net.NoRouteToHostException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) MalformedURLException(java.net.MalformedURLException) MetadataNotFoundException(org.eclipse.aether.transfer.MetadataNotFoundException) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) ScopeDependencySelector(org.eclipse.aether.util.graph.selector.ScopeDependencySelector) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector) DependencySelector(org.eclipse.aether.collection.DependencySelector) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 4 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project BIMserver by opensourceBIM.

the class MavenPluginLocation method createMavenVersion.

private MavenPluginVersion createMavenVersion(Version version) throws ArtifactDescriptorException, FileNotFoundException, IOException, ArtifactResolutionException, XmlPullParserException {
    ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
    Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "pom", version.toString());
    descriptorRequest.setArtifact(versionArtifact);
    descriptorRequest.setRepositories(mavenPluginRepository.getRepositoriesAsList());
    MavenPluginVersion mavenPluginVersion = new MavenPluginVersion(versionArtifact, version);
    ArtifactDescriptorResult descriptorResult;
    descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
    try {
        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(descriptorResult.getArtifact());
        request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
        ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
        File pomFile = resolveArtifact.getArtifact().getFile();
        MavenXpp3Reader mavenreader = new MavenXpp3Reader();
        try (FileReader fileReader = new FileReader(pomFile)) {
            Model model = mavenreader.read(fileReader);
            mavenPluginVersion.setModel(model);
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
    }
    for (org.eclipse.aether.graph.Dependency dependency : descriptorResult.getDependencies()) {
        DefaultArtifactVersion artifactVersion = new DefaultArtifactVersion(dependency.getArtifact().getVersion());
        mavenPluginVersion.addDependency(new MavenDependency(dependency.getArtifact(), artifactVersion));
    }
    return mavenPluginVersion;
}
Also used : DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) ParseException(java.text.ParseException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) Model(org.apache.maven.model.Model) FileReader(java.io.FileReader) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) File(java.io.File) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 5 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project mule by mulesoft.

the class DependencyResolver method readArtifactDescriptor.

/**
 * Gets information about an artifact like its direct dependencies and potential relocations.
 *
 * @param artifact the {@link Artifact} requested, must not be {@code null}
 * @return {@link ArtifactDescriptorResult} descriptor result, never {@code null}
 * @throws {@link ArtifactDescriptorException} if the artifact descriptor could not be read
 */
public ArtifactDescriptorResult readArtifactDescriptor(Artifact artifact) throws ArtifactDescriptorException {
    checkNotNull(artifact, "artifact cannot be null");
    final ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(artifact, resolutionContext.getRemoteRepositories(), null);
    return repositoryState.getSystem().readArtifactDescriptor(repositoryState.getSession(), request);
}
Also used : ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest)

Aggregations

ArtifactDescriptorRequest (org.eclipse.aether.resolution.ArtifactDescriptorRequest)11 ArtifactDescriptorResult (org.eclipse.aether.resolution.ArtifactDescriptorResult)9 Artifact (org.eclipse.aether.artifact.Artifact)7 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)7 IOException (java.io.IOException)6 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)6 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)6 FileNotFoundException (java.io.FileNotFoundException)5 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)5 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)5 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)5 File (java.io.File)4 Dependency (org.eclipse.aether.graph.Dependency)4 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)4 FileReader (java.io.FileReader)3 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)3 Model (org.apache.maven.model.Model)3 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)3 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)3 DependencyNode (org.eclipse.aether.graph.DependencyNode)3