Search in sources :

Example 56 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project mule by mulesoft.

the class AetherClassPathClassifier method getArtifactExportedClasses.

/**
 * Resolves the exported plugin classes from the given {@link Artifact}
 *
 * @param exporterArtifact {@link Artifact} used to resolve the exported classes
 * @param context {@link ClassPathClassifierContext} with settings for the classification process
 * @param rootArtifactRemoteRepositories remote repositories defined by the rootArtifact
 * @return {@link List} of {@link Class} that the given {@link Artifact} exports
 */
private List<Class> getArtifactExportedClasses(Artifact exporterArtifact, ClassPathClassifierContext context, List<RemoteRepository> rootArtifactRemoteRepositories) {
    final AtomicReference<URL> artifactUrl = new AtomicReference<>();
    try {
        artifactUrl.set(dependencyResolver.resolveArtifact(exporterArtifact, rootArtifactRemoteRepositories).getArtifact().getFile().toURI().toURL());
    } catch (MalformedURLException | ArtifactResolutionException e) {
        throw new IllegalStateException("Unable to resolve artifact URL", e);
    }
    Artifact rootArtifact = context.getRootArtifact();
    return context.getExportPluginClasses().stream().filter(clazz -> {
        boolean isFromCurrentArtifact = clazz.getProtectionDomain().getCodeSource().getLocation().equals(artifactUrl.get());
        if (isFromCurrentArtifact && exporterArtifact != rootArtifact) {
            logger.warn("Exported class '{}' from plugin '{}' is being used from another artifact, {}", clazz.getSimpleName(), exporterArtifact, rootArtifact);
        }
        return isFromCurrentArtifact;
    }).collect(toList());
}
Also used : PLUGIN(org.mule.test.runner.api.ArtifactClassificationType.PLUGIN) ListIterator(java.util.ListIterator) URL(java.net.URL) Optional.of(java.util.Optional.of) TEST(org.eclipse.aether.util.artifact.JavaScopes.TEST) LoggerFactory(org.slf4j.LoggerFactory) DependencyFilterUtils.orFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.orFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) APPLICATION(org.mule.test.runner.api.ArtifactClassificationType.APPLICATION) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) Collections.singleton(java.util.Collections.singleton) DependencyFilterUtils.andFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.andFilter) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactIdUtils.toId(org.eclipse.aether.util.artifact.ArtifactIdUtils.toId) PROVIDED(org.eclipse.aether.util.artifact.JavaScopes.PROVIDED) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) Preconditions.checkNotNull(org.mule.runtime.api.util.Preconditions.checkNotNull) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) String.format(java.lang.String.format) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Optional(java.util.Optional) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) Optional.empty(java.util.Optional.empty) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Extension(org.mule.runtime.extension.api.annotation.Extension) Dependency(org.eclipse.aether.graph.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) COMPILE(org.eclipse.aether.util.artifact.JavaScopes.COMPILE) MODULE(org.mule.test.runner.api.ArtifactClassificationType.MODULE) VersionChecker.areCompatibleVersions(org.mule.maven.client.internal.util.VersionChecker.areCompatibleVersions) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Sets.newLinkedHashSet(com.google.common.collect.Sets.newLinkedHashSet) IOException(java.io.IOException) File(java.io.File) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Collectors.toList(java.util.stream.Collectors.toList) FileFilter(java.io.FileFilter) StringUtils.endsWithIgnoreCase(org.apache.commons.lang3.StringUtils.endsWithIgnoreCase) VersionChecker.isHighestVersion(org.mule.maven.client.internal.util.VersionChecker.isHighestVersion) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) Exclusion(org.eclipse.aether.graph.Exclusion) DependencyFilterUtils.classpathFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.classpathFilter) Collections(java.util.Collections) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) AtomicReference(java.util.concurrent.atomic.AtomicReference) URL(java.net.URL) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 57 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project mule by mulesoft.

the class AetherClassPathClassifier method buildContainerUrlClassification.

/**
 * Container classification is being done by resolving the {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} direct
 * dependencies of the rootArtifact. Is uses the exclusions defined in
 * {@link ClassPathClassifierContext#getProvidedExclusions()} to filter the dependency graph plus
 * {@link ClassPathClassifierContext#getExcludedArtifacts()}.
 * <p/>
 * In order to resolve correctly the {@value org.eclipse.aether.util.artifact.JavaScopes#PROVIDED} direct dependencies it will
 * get for each one the manage dependencies and use that list to resolve the graph.
 *
 * @param context {@link ClassPathClassifierContext} with settings for the classification process
 * @param pluginUrlClassifications {@link PluginUrlClassification}s to check if rootArtifact was classified as plugin
 * @param rootArtifactType {@link ArtifactClassificationType} for rootArtifact
 * @param rootArtifactRemoteRepositories remote repositories defined at the rootArtifact
 * @return {@link List} of {@link URL}s for the container class loader
 */
private List<URL> buildContainerUrlClassification(ClassPathClassifierContext context, List<Dependency> directDependencies, List<ArtifactUrlClassification> serviceUrlClassifications, List<PluginUrlClassification> pluginUrlClassifications, ArtifactClassificationType rootArtifactType, List<RemoteRepository> rootArtifactRemoteRepositories) {
    directDependencies = directDependencies.stream().filter(getContainerDirectDependenciesFilter(rootArtifactType)).filter(dependency -> {
        Artifact artifact = dependency.getArtifact();
        return (!serviceUrlClassifications.stream().filter(artifactUrlClassification -> artifactUrlClassification.getArtifactId().equals(toId(artifact))).findAny().isPresent() && !pluginUrlClassifications.stream().filter(artifactUrlClassification -> artifactUrlClassification.getArtifactId().equals(toId(artifact))).findAny().isPresent());
    }).map(depToTransform -> depToTransform.setScope(COMPILE)).collect(toList());
    // Add logging dependencies to avoid every module from having to declare this dependency.
    // This brings the slf4j bridges required by transitive dependencies of the container to its classpath
    // TODO MULE-10837 Externalize this dependency along with the other commonly used container dependencies.
    directDependencies.add(new Dependency(new DefaultArtifact(RUNTIME_GROUP_ID, LOGGING_ARTIFACT_ID, JAR_EXTENSION, muleVersion), COMPILE));
    logger.debug("Selected direct dependencies to be used for resolving container dependency graph (changed to compile in " + "order to resolve the graph): {}", directDependencies);
    Set<Dependency> managedDependencies = selectContainerManagedDependencies(context, directDependencies, rootArtifactType, rootArtifactRemoteRepositories);
    logger.debug("Collected managed dependencies from direct provided dependencies to be used for resolving container " + "dependency graph: {}", managedDependencies);
    List<String> excludedFilterPattern = newArrayList(context.getProvidedExclusions());
    excludedFilterPattern.addAll(context.getExcludedArtifacts());
    if (!pluginUrlClassifications.isEmpty()) {
        excludedFilterPattern.addAll(pluginUrlClassifications.stream().map(pluginUrlClassification -> pluginUrlClassification.getArtifactId()).collect(toList()));
    }
    if (!serviceUrlClassifications.isEmpty()) {
        excludedFilterPattern.addAll(serviceUrlClassifications.stream().map(serviceUrlClassification -> serviceUrlClassification.getArtifactId()).collect(toList()));
    }
    logger.debug("Resolving dependencies for container using exclusion filter patterns: {}", excludedFilterPattern);
    final DependencyFilter dependencyFilter = new PatternExclusionsDependencyFilter(excludedFilterPattern);
    List<URL> containerUrls;
    try {
        containerUrls = toUrl(dependencyResolver.resolveDependencies(null, directDependencies, newArrayList(managedDependencies), dependencyFilter, rootArtifactRemoteRepositories));
    } catch (Exception e) {
        throw new IllegalStateException("Couldn't resolve dependencies for Container", e);
    }
    containerUrls = containerUrls.stream().filter(url -> {
        String file = toFile(url).getAbsolutePath();
        return !(endsWithIgnoreCase(file, POM_XML) || endsWithIgnoreCase(file, POM_EXTENSION) || endsWithIgnoreCase(file, ZIP_EXTENSION));
    }).collect(toList());
    if (MODULE.equals(rootArtifactType)) {
        File rootArtifactOutputFile = resolveRootArtifactFile(context.getRootArtifact());
        if (rootArtifactOutputFile == null) {
            throw new IllegalStateException("rootArtifact (" + context.getRootArtifact() + ") identified as MODULE but doesn't have an output");
        }
        containerUrls.add(0, toUrl(rootArtifactOutputFile));
    }
    resolveSnapshotVersionsToTimestampedFromClassPath(containerUrls, context.getClassPathURLs());
    return containerUrls;
}
Also used : PLUGIN(org.mule.test.runner.api.ArtifactClassificationType.PLUGIN) ListIterator(java.util.ListIterator) URL(java.net.URL) Optional.of(java.util.Optional.of) TEST(org.eclipse.aether.util.artifact.JavaScopes.TEST) LoggerFactory(org.slf4j.LoggerFactory) DependencyFilterUtils.orFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.orFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) APPLICATION(org.mule.test.runner.api.ArtifactClassificationType.APPLICATION) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) Collections.singleton(java.util.Collections.singleton) DependencyFilterUtils.andFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.andFilter) Map(java.util.Map) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Collectors.toSet(java.util.stream.Collectors.toSet) ArtifactIdUtils.toId(org.eclipse.aether.util.artifact.ArtifactIdUtils.toId) PROVIDED(org.eclipse.aether.util.artifact.JavaScopes.PROVIDED) Maps.newHashMap(com.google.common.collect.Maps.newHashMap) Collections.emptyList(java.util.Collections.emptyList) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) Artifact(org.eclipse.aether.artifact.Artifact) Preconditions.checkNotNull(org.mule.runtime.api.util.Preconditions.checkNotNull) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) String.format(java.lang.String.format) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Optional(java.util.Optional) Maps.newLinkedHashMap(com.google.common.collect.Maps.newLinkedHashMap) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) Optional.empty(java.util.Optional.empty) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Extension(org.mule.runtime.extension.api.annotation.Extension) Dependency(org.eclipse.aether.graph.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) COMPILE(org.eclipse.aether.util.artifact.JavaScopes.COMPILE) MODULE(org.mule.test.runner.api.ArtifactClassificationType.MODULE) VersionChecker.areCompatibleVersions(org.mule.maven.client.internal.util.VersionChecker.areCompatibleVersions) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Sets.newLinkedHashSet(com.google.common.collect.Sets.newLinkedHashSet) IOException(java.io.IOException) File(java.io.File) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Collectors.toList(java.util.stream.Collectors.toList) FileFilter(java.io.FileFilter) StringUtils.endsWithIgnoreCase(org.apache.commons.lang3.StringUtils.endsWithIgnoreCase) VersionChecker.isHighestVersion(org.mule.maven.client.internal.util.VersionChecker.isHighestVersion) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) Exclusion(org.eclipse.aether.graph.Exclusion) DependencyFilterUtils.classpathFilter(org.eclipse.aether.util.filter.DependencyFilterUtils.classpathFilter) Collections(java.util.Collections) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) URL(java.net.URL) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) FileUtils.toFile(org.apache.commons.io.FileUtils.toFile) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 58 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project mule by mulesoft.

the class DependencyResolver method resolveArtifact.

/**
 * Resolves the path for an artifact.
 *
 * @param artifact the {@link Artifact} requested, must not be {@code null}
 * @param remoteRepositories remote repositories to be used in addition to the one in context
 * @return The resolution result, never {@code null}.
 * @throws {@link ArtifactResolutionException} if the artifact could not be resolved.
 */
public ArtifactResult resolveArtifact(Artifact artifact, List<RemoteRepository> remoteRepositories) throws ArtifactResolutionException {
    checkNotNull(artifact, "artifact cannot be null");
    final ArtifactRequest request = new ArtifactRequest(artifact, resolutionContext.getRemoteRepositories(), null);
    // Has to set authentication to these remote repositories as they may come from a pom descriptor
    remoteRepositories.forEach(remoteRepository -> {
        RemoteRepository authenticatedRemoteRepository = setAuthentication(remoteRepository);
        if (!request.getRepositories().contains(authenticatedRemoteRepository)) {
            request.addRepository(authenticatedRemoteRepository);
        }
    });
    return repositoryState.getSystem().resolveArtifact(repositoryState.getSession(), request);
}
Also used : ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) RemoteRepository(org.eclipse.aether.repository.RemoteRepository)

Example 59 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project mule by mulesoft.

the class DependencyResolver method printCollectRequest.

private String printCollectRequest(CollectRequest collectRequest) {
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("[");
    Iterator<RemoteRepository> iterator = collectRequest.getRepositories().iterator();
    while (iterator.hasNext()) {
        RemoteRepository remoteRepository = iterator.next();
        stringBuilder.append(remoteRepository);
        stringBuilder.append("->authentication[");
        if (remoteRepository.getAuthentication() != null) {
            stringBuilder.append(remoteRepository.getAuthentication());
        }
        stringBuilder.append("]");
        if (iterator.hasNext()) {
            stringBuilder.append(", ");
        }
    }
    stringBuilder.append("]");
    return collectRequest.getRoot() + " -> " + collectRequest.getDependencies() + " < " + stringBuilder.toString();
}
Also used : ReflectionToStringBuilder(org.apache.commons.lang3.builder.ReflectionToStringBuilder) RemoteRepository(org.eclipse.aether.repository.RemoteRepository)

Example 60 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project storm by apache.

the class DependencyResolver method resolve.

/**
 * Resolve dependencies and return downloaded information of artifacts.
 *
 * @param dependencies the list of dependency
 * @return downloaded information of artifacts
 * @throws DependencyResolutionException If the dependency tree could not be built or any dependency
 *     artifact could not be resolved.
 * @throws ArtifactResolutionException If the artifact could not be resolved.
 */
public List<ArtifactResult> resolve(List<Dependency> dependencies) throws DependencyResolutionException, ArtifactResolutionException {
    if (dependencies.size() == 0) {
        return Collections.EMPTY_LIST;
    }
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(dependencies.get(0));
    for (int idx = 1; idx < dependencies.size(); idx++) {
        collectRequest.addDependency(dependencies.get(idx));
    }
    for (RemoteRepository repository : remoteRepositories) {
        collectRequest.addRepository(repository);
    }
    DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE, JavaScopes.RUNTIME);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);
    return system.resolveDependencies(session, dependencyRequest).getArtifactResults();
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) CollectRequest(org.eclipse.aether.collection.CollectRequest)

Aggregations

RemoteRepository (org.eclipse.aether.repository.RemoteRepository)66 File (java.io.File)24 ArrayList (java.util.ArrayList)17 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)16 Artifact (org.eclipse.aether.artifact.Artifact)15 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)13 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)13 Dependency (org.eclipse.aether.graph.Dependency)12 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)12 IOException (java.io.IOException)11 List (java.util.List)9 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)9 MalformedURLException (java.net.MalformedURLException)8 RepositorySystem (org.eclipse.aether.RepositorySystem)8 LocalRepository (org.eclipse.aether.repository.LocalRepository)8 RepositoryPolicy (org.eclipse.aether.repository.RepositoryPolicy)8 CollectRequest (org.eclipse.aether.collection.CollectRequest)7 DependencyFilter (org.eclipse.aether.graph.DependencyFilter)7 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)7 URL (java.net.URL)6