Search in sources :

Example 26 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project drools by kiegroup.

the class MavenRepository method resolveArtifact.

public Artifact resolveArtifact(String artifactName, boolean logUnresolvedArtifact) {
    Artifact artifact = new DefaultArtifact(artifactName);
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(artifact);
    for (RemoteRepository repo : remoteRepositoriesForRequest) {
        artifactRequest.addRepository(repo);
    }
    RepositorySystemSession session = aether.getSession();
    Object sessionChecks = null;
    boolean isSnapshot = artifactName.endsWith("-SNAPSHOT");
    if (artifactName.endsWith("-SNAPSHOT")) {
        // ensure to always update snapshots
        sessionChecks = session.getData().get(SESSION_CHECKS);
        session.getData().set(SESSION_CHECKS, null);
    }
    try {
        ArtifactResult artifactResult = aether.getSystem().resolveArtifact(session, artifactRequest);
        return artifactResult.getArtifact();
    } catch (ArtifactResolutionException e) {
        if (logUnresolvedArtifact) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to resolve artifact: " + artifactName, e);
            } else {
                log.warn("Unable to resolve artifact: " + artifactName);
            }
        }
        return null;
    } finally {
        if (sessionChecks != null) {
            session.getData().set(SESSION_CHECKS, sessionChecks);
        }
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 27 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project drools by kiegroup.

the class MavenRepository method initRemoteRepositoriesForRequest.

private Collection<RemoteRepository> initRemoteRepositoriesForRequest() {
    final MavenRepositoryConfiguration repositoryUtils = getMavenRepositoryConfiguration();
    Collection<RemoteRepository> remoteRepos = new HashSet<RemoteRepository>();
    remoteRepos.addAll(repositoryUtils.getRemoteRepositoriesForRequest());
    for (RemoteRepository repo : aether.getRepositories()) {
        remoteRepos.add(repositoryUtils.resolveMirroredRepo(repo));
    }
    return remoteRepos;
}
Also used : RemoteRepository(org.eclipse.aether.repository.RemoteRepository) HashSet(java.util.HashSet)

Example 28 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project drools by kiegroup.

the class MavenRepository method getRemoteRepositoryFromDistributionManagement.

protected RemoteRepository getRemoteRepositoryFromDistributionManagement(File pomfile) {
    MavenProject mavenProject = parseMavenPom(pomfile);
    DistributionManagement distMan = mavenProject.getDistributionManagement();
    if (distMan == null) {
        return null;
    }
    DeploymentRepository deployRepo = distMan.getSnapshotRepository() != null && mavenProject.getVersion().endsWith("SNAPSHOT") ? distMan.getSnapshotRepository() : distMan.getRepository();
    if (deployRepo == null) {
        return null;
    }
    RemoteRepository.Builder remoteRepoBuilder = new RemoteRepository.Builder(deployRepo.getId(), deployRepo.getLayout(), deployRepo.getUrl()).setSnapshotPolicy(new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_DAILY, RepositoryPolicy.CHECKSUM_POLICY_WARN)).setReleasePolicy(new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_ALWAYS, RepositoryPolicy.CHECKSUM_POLICY_WARN));
    Server server = MavenSettings.getSettings().getServer(deployRepo.getId());
    if (server != null) {
        MavenEmbedder embedder = MavenProjectLoader.newMavenEmbedder(false);
        try {
            Authentication authentication = embedder.getMavenSession().getRepositorySession().getAuthenticationSelector().getAuthentication(remoteRepoBuilder.build());
            remoteRepoBuilder.setAuthentication(authentication);
        } finally {
            embedder.dispose();
        }
    }
    return remoteRepoBuilder.build();
}
Also used : MavenProject(org.apache.maven.project.MavenProject) Server(org.apache.maven.settings.Server) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) MavenEmbedder(org.kie.maven.integration.embedder.MavenEmbedder) Authentication(org.eclipse.aether.repository.Authentication) DeploymentRepository(org.apache.maven.model.DeploymentRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DistributionManagement(org.apache.maven.model.DistributionManagement)

Example 29 with RemoteRepository

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

the class AetherClassPathClassifier method buildPluginUrlClassification.

/**
 * Classifies an {@link Artifact} recursively. {@value org.eclipse.aether.util.artifact.JavaScopes#COMPILE} dependencies will be
 * resolved for building the {@link URL}'s for the class loader. Once classified the node is added to {@link Map} of
 * artifactsClassified.
 *
 * @param artifactToClassify {@link Artifact} that represents the artifact to be classified
 * @param context {@link ClassPathClassifierContext} with settings for the classification process
 * @param artifactsClassified {@link Map} that contains already classified plugins
 * @param rootArtifactRemoteRepositories remote repositories defined at the root artifact.
 */
private void buildPluginUrlClassification(Artifact artifactToClassify, ClassPathClassifierContext context, Predicate<Dependency> directDependenciesFilter, Set<ArtifactClassificationNode> artifactsClassified, List<RemoteRepository> rootArtifactRemoteRepositories) {
    List<URL> urls;
    try {
        final DependencyFilter dependencyFilter = andFilter(classpathFilter(COMPILE), new PatternExclusionsDependencyFilter(context.getExcludedArtifacts()), orFilter(new PatternExclusionsDependencyFilter("*:*:*:" + MULE_PLUGIN_CLASSIFIER + ":*"), new PatternInclusionsDependencyFilter(toId(artifactToClassify))));
        urls = toUrl(dependencyResolver.resolveDependencies(new Dependency(artifactToClassify, COMPILE), emptyList(), emptyList(), dependencyFilter, rootArtifactRemoteRepositories));
    } catch (Exception e) {
        throw new IllegalStateException("Couldn't resolve dependencies for artifact: '" + artifactToClassify + "' classification", e);
    }
    List<Dependency> directDependencies;
    List<ArtifactClassificationNode> artifactDependencies = newArrayList();
    try {
        directDependencies = dependencyResolver.getDirectDependencies(artifactToClassify, rootArtifactRemoteRepositories);
    } catch (ArtifactDescriptorException e) {
        throw new IllegalStateException("Couldn't get direct dependencies for artifact: '" + artifactToClassify + "'", e);
    }
    logger.debug("Searching for dependencies on direct dependencies of artifact {}", artifactToClassify);
    List<Artifact> pluginArtifactDependencies = filterArtifacts(directDependencies, directDependenciesFilter);
    logger.debug("Artifacts {} identified a plugin dependencies for plugin {}", pluginArtifactDependencies, artifactToClassify);
    pluginArtifactDependencies.stream().map(artifact -> {
        Optional<ArtifactClassificationNode> artifactClassificationNodeOptional = findArtifactClassified(artifactsClassified, artifact);
        if (!artifactClassificationNodeOptional.isPresent()) {
            buildPluginUrlClassification(artifact, context, directDependenciesFilter, artifactsClassified, rootArtifactRemoteRepositories);
        } else {
            if (!areCompatibleVersions(artifactClassificationNodeOptional.get().getArtifact().getVersion(), artifact.getVersion())) {
                throw new IllegalStateException(format("Incompatible version of artifacts found: %s and %s", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact)));
            }
            logger.debug("Checking for highest version of artifact, already discovered: '{}' versus: '{}'", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact));
            if (isHighestVersion(artifact.getVersion(), artifactClassificationNodeOptional.get().getArtifact().getVersion())) {
                artifactsClassified.remove(artifactClassificationNodeOptional.get());
                buildPluginUrlClassification(artifact, context, directDependenciesFilter, artifactsClassified, rootArtifactRemoteRepositories);
                logger.warn("Replacing artifact: '{}' for highest version: '{}'", toId(artifactClassificationNodeOptional.get().getArtifact()), toId(artifact));
            }
        }
        return findArtifactClassified(artifactsClassified, artifact).orElseThrow(() -> new IllegalStateException(format("Should %s be already added to the list of artifacts classified", toId(artifact))));
    }).forEach(artifactDependencies::add);
    final List<Class> exportClasses = getArtifactExportedClasses(artifactToClassify, context, rootArtifactRemoteRepositories);
    resolveSnapshotVersionsToTimestampedFromClassPath(urls, context.getClassPathURLs());
    ArtifactClassificationNode artifactUrlClassification = new ArtifactClassificationNode(artifactToClassify, urls, exportClasses, artifactDependencies);
    logger.debug("Artifact discovered: {}", toId(artifactUrlClassification.getArtifact()));
    artifactsClassified.add(artifactUrlClassification);
}
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) Optional(java.util.Optional) 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) URL(java.net.URL) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) PatternExclusionsDependencyFilter(org.mule.test.runner.classification.PatternExclusionsDependencyFilter) PatternInclusionsDependencyFilter(org.mule.test.runner.classification.PatternInclusionsDependencyFilter) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException)

Example 30 with RemoteRepository

use of org.eclipse.aether.repository.RemoteRepository in project revapi by revapi.

the class Main method convertGavs.

private static ArchivesAndSupplementaryArchives convertGavs(String[] gavs, String errorMessagePrefix, File localRepo) {
    RepositorySystem repositorySystem = MavenBootstrap.newRepositorySystem();
    DefaultRepositorySystemSession session = MavenBootstrap.newRepositorySystemSession(repositorySystem, localRepo);
    session.setDependencySelector(new ScopeDependencySelector("compile", "provided"));
    session.setDependencyTraverser(new ScopeDependencyTraverser("compile", "provided"));
    RemoteRepository mavenCentral = new RemoteRepository.Builder("@@forced-maven-central@@", "default", "http://repo.maven.apache.org/maven2/").build();
    List<RemoteRepository> remoteRepositories = singletonList(mavenCentral);
    ArtifactResolver resolver = new ArtifactResolver(repositorySystem, session, remoteRepositories);
    List<FileArchive> archives = new ArrayList<>();
    List<FileArchive> supplementaryArchives = new ArrayList<>();
    for (String gav : gavs) {
        try {
            archives.add(new FileArchive(resolver.resolveArtifact(gav).getFile()));
            ArtifactResolver.CollectionResult res = resolver.collectTransitiveDeps(gav);
            res.getResolvedArtifacts().forEach(a -> supplementaryArchives.add(new FileArchive(a.getFile())));
            if (!res.getFailures().isEmpty()) {
                LOG.warn("Failed to resolve some transitive dependencies: " + res.getFailures().toString());
            }
        } catch (RepositoryException e) {
            throw new IllegalArgumentException(errorMessagePrefix + " " + e.getMessage());
        }
    }
    return new ArchivesAndSupplementaryArchives(archives, supplementaryArchives);
}
Also used : ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RepositoryException(org.eclipse.aether.RepositoryException) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) RepositorySystem(org.eclipse.aether.RepositorySystem) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) FileArchive(org.revapi.simple.FileArchive) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector)

Aggregations

RemoteRepository (org.eclipse.aether.repository.RemoteRepository)235 File (java.io.File)60 Artifact (org.eclipse.aether.artifact.Artifact)57 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)56 ArrayList (java.util.ArrayList)55 Test (org.junit.Test)46 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)41 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)38 IOException (java.io.IOException)35 Dependency (org.eclipse.aether.graph.Dependency)35 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)34 RepositorySystem (org.eclipse.aether.RepositorySystem)28 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)28 Proxy (org.eclipse.aether.repository.Proxy)27 RepositoryPolicy (org.eclipse.aether.repository.RepositoryPolicy)27 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)27 AuthenticationBuilder (org.eclipse.aether.util.repository.AuthenticationBuilder)27 List (java.util.List)24 CollectRequest (org.eclipse.aether.collection.CollectRequest)23 Authentication (org.eclipse.aether.repository.Authentication)23