Search in sources :

Example 36 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project spring-boot by spring-projects.

the class ModifiedClassPathClassLoader method resolveCoordinates.

private static List<URL> resolveCoordinates(String[] coordinates) {
    Exception latestFailure = null;
    DefaultServiceLocator serviceLocator = MavenRepositorySystemUtils.newServiceLocator();
    serviceLocator.addService(RepositoryConnectorFactory.class, BasicRepositoryConnectorFactory.class);
    serviceLocator.addService(TransporterFactory.class, HttpTransporterFactory.class);
    RepositorySystem repositorySystem = serviceLocator.getService(RepositorySystem.class);
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
    LocalRepository localRepository = new LocalRepository(System.getProperty("user.home") + "/.m2/repository");
    RemoteRepository remoteRepository = new RemoteRepository.Builder("central", "default", "https://repo.maven.apache.org/maven2").build();
    session.setLocalRepositoryManager(repositorySystem.newLocalRepositoryManager(session, localRepository));
    for (int i = 0; i < MAX_RESOLUTION_ATTEMPTS; i++) {
        CollectRequest collectRequest = new CollectRequest(null, Arrays.asList(remoteRepository));
        collectRequest.setDependencies(createDependencies(coordinates));
        DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, null);
        try {
            DependencyResult result = repositorySystem.resolveDependencies(session, dependencyRequest);
            List<URL> resolvedArtifacts = new ArrayList<>();
            for (ArtifactResult artifact : result.getArtifactResults()) {
                resolvedArtifacts.add(artifact.getArtifact().getFile().toURI().toURL());
            }
            return resolvedArtifacts;
        } catch (Exception ex) {
            latestFailure = ex;
        }
    }
    throw new IllegalStateException("Resolution failed after " + MAX_RESOLUTION_ATTEMPTS + " attempts", latestFailure);
}
Also used : DependencyResult(org.eclipse.aether.resolution.DependencyResult) LocalRepository(org.eclipse.aether.repository.LocalRepository) ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DefaultServiceLocator(org.eclipse.aether.impl.DefaultServiceLocator) CollectRequest(org.eclipse.aether.collection.CollectRequest) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) RepositorySystem(org.eclipse.aether.RepositorySystem) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest)

Example 37 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project grails-maven by grails.

the class AbstractGrailsMojo method resolveArtifact.

protected File resolveArtifact(String artifactId) throws MojoExecutionException {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(artifactId));
    request.setRepositories(remoteRepos);
    getLog().debug("Resolving artifact " + artifactId + " from " + remoteRepos);
    ArtifactResult result;
    File file;
    try {
        result = repoSystem.resolveArtifact(repoSession, request);
        file = result.getArtifact().getFile();
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    getLog().debug("Resolved artifact " + artifactId + " to " + file + " from " + result.getRepository());
    return file;
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 38 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project drools by kiegroup.

the class MavenProjectLoader method resolve.

private static boolean resolve(final RepositorySystemSession session, final Artifact artifact) {
    final ArtifactRequest artifactRequest = new ArtifactRequest();
    final org.eclipse.aether.artifact.Artifact jarArtifact = new org.eclipse.aether.artifact.DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(), "jar", artifact.getVersion());
    artifactRequest.setArtifact(jarArtifact);
    try {
        ArtifactResult result = Aether.getAether().getSystem().resolveArtifact(session, artifactRequest);
        return result != null && result.isResolved();
    } catch (final Exception are) {
        log.info(are.getMessage(), are);
        return false;
    }
}
Also used : ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 39 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult 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 40 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project meecrowave by apache.

the class MeecrowaveBundleMojo method resolve.

private File resolve(final String group, final String artifact, final String version, final String classifier) {
    final DefaultArtifact art = new DefaultArtifact(group, artifact, classifier, "jar", version);
    final ArtifactRequest artifactRequest = new ArtifactRequest().setArtifact(art).setRepositories(remoteRepositories);
    final LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    art.setFile(new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifactRequest.getArtifact())));
    try {
        final ArtifactResult result = repositorySystem.resolveArtifact(session, artifactRequest);
        if (result.isMissing()) {
            throw new IllegalStateException("Can't find commons-cli, please add it to the pom.");
        }
        return result.getArtifact().getFile();
    } catch (final ArtifactResolutionException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Aggregations

ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)83 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)53 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)51 Artifact (org.eclipse.aether.artifact.Artifact)41 File (java.io.File)37 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)37 IOException (java.io.IOException)16 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)16 ArrayList (java.util.ArrayList)15 Dependency (org.eclipse.aether.graph.Dependency)15 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)13 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)13 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)12 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)11 CollectRequest (org.eclipse.aether.collection.CollectRequest)11 ArtifactDescriptorResult (org.eclipse.aether.resolution.ArtifactDescriptorResult)10 FileNotFoundException (java.io.FileNotFoundException)9 RepositorySystem (org.eclipse.aether.RepositorySystem)8 Test (org.junit.Test)8 DependencyFilter (org.eclipse.aether.graph.DependencyFilter)7