Search in sources :

Example 1 with Dependency

use of org.eclipse.aether.graph.Dependency in project buck by facebook.

the class Resolver method getDependenciesFromPom.

private ImmutableList<Dependency> getDependenciesFromPom(Model model) {
    return model.getDependencies().stream().map(dep -> {
        ArtifactType stereotype = session.getArtifactTypeRegistry().get(dep.getType());
        if (stereotype == null) {
            stereotype = new DefaultArtifactType(dep.getType());
        }
        Map<String, String> props = null;
        boolean system = dep.getSystemPath() != null && dep.getSystemPath().length() > 0;
        if (system) {
            props = ImmutableMap.of(ArtifactProperties.LOCAL_PATH, dep.getSystemPath());
        }
        @SuppressWarnings("PMD.PrematureDeclaration") DefaultArtifact artifact = new DefaultArtifact(dep.getGroupId(), dep.getArtifactId(), dep.getClassifier(), null, dep.getVersion(), props, stereotype);
        ImmutableList<Exclusion> exclusions = FluentIterable.from(dep.getExclusions()).transform(input -> {
            String group = input.getGroupId();
            String artifact1 = input.getArtifactId();
            group = (group == null || group.length() == 0) ? "*" : group;
            artifact1 = (artifact1 == null || artifact1.length() == 0) ? "*" : artifact1;
            return new Exclusion(group, artifact1, "*", "*");
        }).toList();
        return new Dependency(artifact, dep.getScope(), dep.isOptional(), exclusions);
    }).collect(MoreCollectors.toImmutableList());
}
Also used : ServiceLocator(org.eclipse.aether.spi.locator.ServiceLocator) STGroupString(org.stringtemplate.v4.STGroupString) DefaultDependencyManagementInjector(org.apache.maven.model.management.DefaultDependencyManagementInjector) SortedSet(java.util.SortedSet) DependencyFilterUtils(org.eclipse.aether.util.filter.DependencyFilterUtils) Version(org.eclipse.aether.version.Version) URL(java.net.URL) TEST(org.eclipse.aether.util.artifact.JavaScopes.TEST) DefaultModelBuilderFactory(org.apache.maven.model.building.DefaultModelBuilderFactory) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) Matcher(java.util.regex.Matcher) ModelBuilder(org.apache.maven.model.building.ModelBuilder) FluentIterable(com.google.common.collect.FluentIterable) DefaultProfileSelector(org.apache.maven.model.profile.DefaultProfileSelector) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) Map(java.util.Map) DefaultArtifactType(org.eclipse.aether.artifact.DefaultArtifactType) Path(java.nio.file.Path) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) DefaultDependencyManagementImporter(org.apache.maven.model.composition.DefaultDependencyManagementImporter) LocalRepository(org.eclipse.aether.repository.LocalRepository) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) Collection(java.util.Collection) Artifact(org.eclipse.aether.artifact.Artifact) Executors(java.util.concurrent.Executors) Objects(java.util.Objects) CmdLineException(org.kohsuke.args4j.CmdLineException) List(java.util.List) ModelBuildingResult(org.apache.maven.model.building.ModelBuildingResult) ST(org.stringtemplate.v4.ST) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) DependencyResult(org.eclipse.aether.resolution.DependencyResult) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Model(org.apache.maven.model.Model) RepositorySystem(org.eclipse.aether.RepositorySystem) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) VersionScheme(org.eclipse.aether.version.VersionScheme) ArtifactProperties(org.eclipse.aether.artifact.ArtifactProperties) Dependency(org.eclipse.aether.graph.Dependency) JavaScopes(org.eclipse.aether.util.artifact.JavaScopes) MutableDirectedGraph(com.facebook.buck.graph.MutableDirectedGraph) GenericVersionScheme(org.eclipse.aether.util.version.GenericVersionScheme) Callable(java.util.concurrent.Callable) TraversableGraph(com.facebook.buck.graph.TraversableGraph) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) TreeSet(java.util.TreeSet) ArtifactType(org.eclipse.aether.artifact.ArtifactType) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Nullable(javax.annotation.Nullable) MoreCollectors(com.facebook.buck.util.MoreCollectors) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) CollectRequest(org.eclipse.aether.collection.CollectRequest) Resources(com.google.common.io.Resources) DefaultPluginConfigurationExpander(org.apache.maven.model.plugin.DefaultPluginConfigurationExpander) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ModelBuildingException(org.apache.maven.model.building.ModelBuildingException) MostExecutors(com.facebook.buck.util.concurrent.MostExecutors) MavenRepositorySystemUtils(org.apache.maven.repository.internal.MavenRepositorySystemUtils) IOException(java.io.IOException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) Maps(com.google.common.collect.Maps) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) InvalidVersionSpecificationException(org.eclipse.aether.version.InvalidVersionSpecificationException) ExecutionException(java.util.concurrent.ExecutionException) MorePaths(com.facebook.buck.io.MorePaths) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Futures(com.google.common.util.concurrent.Futures) Ordering(com.google.common.collect.Ordering) RepositoryException(org.eclipse.aether.RepositoryException) Paths(java.nio.file.Paths) DefaultPluginManagementInjector(org.apache.maven.model.management.DefaultPluginManagementInjector) DefaultModelBuildingRequest(org.apache.maven.model.building.DefaultModelBuildingRequest) Exclusion(org.eclipse.aether.graph.Exclusion) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) DefaultArtifactType(org.eclipse.aether.artifact.DefaultArtifactType) ArtifactType(org.eclipse.aether.artifact.ArtifactType) ImmutableList(com.google.common.collect.ImmutableList) Exclusion(org.eclipse.aether.graph.Exclusion) DefaultArtifactType(org.eclipse.aether.artifact.DefaultArtifactType) STGroupString(org.stringtemplate.v4.STGroupString) Dependency(org.eclipse.aether.graph.Dependency) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 2 with Dependency

use of org.eclipse.aether.graph.Dependency in project buck by facebook.

the class Resolver method getRunTimeTransitiveDeps.

private ImmutableMap<String, Artifact> getRunTimeTransitiveDeps(Iterable<Dependency> mavenCoords) throws RepositoryException {
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRequestContext(JavaScopes.RUNTIME);
    collectRequest.setRepositories(repos);
    for (Dependency dep : mavenCoords) {
        collectRequest.addDependency(dep);
    }
    DependencyFilter filter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, filter);
    DependencyResult dependencyResult = repoSys.resolveDependencies(session, dependencyRequest);
    ImmutableSortedMap.Builder<String, Artifact> knownDeps = ImmutableSortedMap.naturalOrder();
    for (ArtifactResult artifactResult : dependencyResult.getArtifactResults()) {
        Artifact node = artifactResult.getArtifact();
        knownDeps.put(buildKey(node), node);
    }
    return knownDeps.build();
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyResult(org.eclipse.aether.resolution.DependencyResult) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) STGroupString(org.stringtemplate.v4.STGroupString) CollectRequest(org.eclipse.aether.collection.CollectRequest) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 3 with Dependency

use of org.eclipse.aether.graph.Dependency in project buck by facebook.

the class Resolver method resolve.

public void resolve(Collection<String> artifacts) throws RepositoryException, ExecutionException, InterruptedException, IOException {
    ImmutableList.Builder<RemoteRepository> repoBuilder = ImmutableList.builder();
    ImmutableMap.Builder<String, Dependency> dependencyBuilder = ImmutableMap.builder();
    repoBuilder.addAll(repos);
    for (String artifact : artifacts) {
        if (artifact.endsWith(".pom")) {
            Model model = loadPomModel(Paths.get(artifact));
            repoBuilder.addAll(getReposFromPom(model));
            for (Dependency dep : getDependenciesFromPom(model)) {
                dependencyBuilder.put(buildKey(dep.getArtifact()), dep);
            }
        } else {
            Dependency dep = getDependencyFromString(artifact);
            dependencyBuilder.put(buildKey(dep.getArtifact()), dep);
        }
    }
    repos = repoBuilder.build();
    ImmutableMap<String, Dependency> specifiedDependencies = dependencyBuilder.build();
    ImmutableMap<String, Artifact> knownDeps = getRunTimeTransitiveDeps(specifiedDependencies.values());
    // We now have the complete set of dependencies. Build the graph of dependencies. We'd like
    // aether to do this for us, but it doesn't preserve the complete dependency information we need
    // to accurately construct build files.
    final MutableDirectedGraph<Artifact> graph = buildDependencyGraph(knownDeps);
    // Now we have the graph, grab the sources and jars for each dependency, as well as the relevant
    // checksums (which are download by default. Yay!)
    ImmutableSetMultimap<Path, Prebuilt> downloadedArtifacts = downloadArtifacts(graph, specifiedDependencies);
    createBuckFiles(downloadedArtifacts);
}
Also used : Path(java.nio.file.Path) ImmutableList(com.google.common.collect.ImmutableList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) STGroupString(org.stringtemplate.v4.STGroupString) Dependency(org.eclipse.aether.graph.Dependency) ImmutableMap(com.google.common.collect.ImmutableMap) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Model(org.apache.maven.model.Model)

Example 4 with Dependency

use of org.eclipse.aether.graph.Dependency in project druid by druid-io.

the class PullDependencies method downloadExtension.

/**
   * Download the extension given its maven coordinate
   *
   * @param versionedArtifact The maven artifact of the extension
   * @param toLocation        The location where this extension will be downloaded to
   */
private void downloadExtension(Artifact versionedArtifact, File toLocation) {
    final CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(versionedArtifact, JavaScopes.RUNTIME));
    final DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, DependencyFilterUtils.andFilter(DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME), new DependencyFilter() {

        @Override
        public boolean accept(DependencyNode node, List<DependencyNode> parents) {
            String scope = node.getDependency().getScope();
            if (scope != null) {
                scope = scope.toLowerCase();
                if (scope.equals("provided")) {
                    return false;
                }
                if (scope.equals("test")) {
                    return false;
                }
                if (scope.equals("system")) {
                    return false;
                }
            }
            if (accept(node.getArtifact())) {
                return false;
            }
            for (DependencyNode parent : parents) {
                if (accept(parent.getArtifact())) {
                    return false;
                }
            }
            return true;
        }

        private boolean accept(final Artifact artifact) {
            return exclusions.contains(artifact.getGroupId());
        }
    }));
    try {
        log.info("Start downloading extension [%s]", versionedArtifact);
        final List<Artifact> artifacts = aether.resolveArtifacts(dependencyRequest);
        for (Artifact artifact : artifacts) {
            if (!exclusions.contains(artifact.getGroupId())) {
                log.info("Adding file [%s] at [%s]", artifact.getFile().getName(), toLocation.getAbsolutePath());
                FileUtils.copyFileToDirectory(artifact.getFile(), toLocation);
            } else {
                log.debug("Skipped Artifact[%s]", artifact);
            }
        }
    } catch (Exception e) {
        log.error(e, "Unable to resolve artifacts for [%s].", dependencyRequest);
        throw Throwables.propagate(e);
    }
    log.info("Finish downloading extension [%s]", versionedArtifact);
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with Dependency

use of org.eclipse.aether.graph.Dependency in project karaf by apache.

the class Dependency31Helper method getDependencyTree.

private DependencyNode getDependencyTree(Artifact artifact) throws MojoExecutionException {
    try {
        CollectRequest collectRequest = new CollectRequest(new Dependency(artifact, "compile"), null, projectRepositories);
        DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(repositorySystemSession);
        session.setDependencySelector(new AndDependencySelector(new OptionalDependencySelector(), new ScopeDependencySelector1(), new ExclusionDependencySelector()));
        // between aether-util 0.9.0.M1 and M2, JavaEffectiveScopeCalculator was removed
        // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=397241
        DependencyGraphTransformer transformer = new ChainedDependencyGraphTransformer(new ConflictMarker(), new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver()), new JavaDependencyContextRefiner());
        session.setDependencyGraphTransformer(transformer);
        CollectResult result = repositorySystem.collectDependencies(session, collectRequest);
        return result.getRoot();
    } catch (DependencyCollectionException e) {
        throw new MojoExecutionException("Cannot build project dependency tree", e);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) AndDependencySelector(org.eclipse.aether.util.graph.selector.AndDependencySelector) Dependency(org.eclipse.aether.graph.Dependency) OptionalDependencySelector(org.eclipse.aether.util.graph.selector.OptionalDependencySelector) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ExclusionDependencySelector(org.eclipse.aether.util.graph.selector.ExclusionDependencySelector)

Aggregations

Dependency (org.eclipse.aether.graph.Dependency)55 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)37 Artifact (org.eclipse.aether.artifact.Artifact)34 File (java.io.File)25 CollectRequest (org.eclipse.aether.collection.CollectRequest)20 ArrayList (java.util.ArrayList)19 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)16 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)16 DependencyNode (org.eclipse.aether.graph.DependencyNode)15 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)15 DependencyFilter (org.eclipse.aether.graph.DependencyFilter)13 ArtifactDescriptorResult (org.eclipse.aether.resolution.ArtifactDescriptorResult)13 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)13 MalformedURLException (java.net.MalformedURLException)12 Exclusion (org.eclipse.aether.graph.Exclusion)12 IOException (java.io.IOException)11 List (java.util.List)10 URL (java.net.URL)9 DependencyResolutionException (org.eclipse.aether.resolution.DependencyResolutionException)9 DependencyResult (org.eclipse.aether.resolution.DependencyResult)9