Search in sources :

Example 51 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project bnd by bndtools.

the class RemotePostProcessor method postProcessRelease.

private ArtifactResult postProcessRelease(ArtifactRequest request, Artifact artifact) throws MojoExecutionException {
    for (RemoteRepository repository : request.getRepositories()) {
        if (!repository.getPolicy(false).isEnabled()) {
            // Skip the repo if it isn't enabled for releases
            continue;
        }
        // Remove the workspace from the session so that we don't use it
        DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
        newSession.setWorkspaceReader(null);
        // Find the snapshot metadata for the module
        MetadataRequest mr = new MetadataRequest().setRepository(repository).setMetadata(new DefaultMetadata(artifact.getGroupId(), artifact.getArtifactId(), null, "maven-metadata.xml", RELEASE));
        for (MetadataResult metadataResult : system.resolveMetadata(newSession, singletonList(mr))) {
            if (metadataResult.isResolved()) {
                try {
                    Metadata read = metadataReader.read(metadataResult.getMetadata().getFile(), null);
                    Versioning versioning = read.getVersioning();
                    if (versioning == null || versioning.getVersions() == null || versioning.getVersions().isEmpty()) {
                        continue;
                    } else if (versioning.getVersions().contains(artifact.getVersion())) {
                        ArtifactResult result = system.resolveArtifact(newSession, new ArtifactRequest().setArtifact(artifact).addRepository(repository));
                        if (result.isResolved()) {
                            File toUse = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForLocalArtifact(artifact));
                            if (!toUse.exists()) {
                                logger.warn("The resolved artifact {} does not exist at {}", artifact, toUse);
                                continue;
                            } else {
                                logger.debug("Located snapshot file {} for artifact {}", toUse, artifact);
                            }
                            result.getArtifact().setFile(toUse);
                            return result;
                        }
                    }
                } catch (Exception e) {
                    throw new MojoExecutionException("Unable to read project metadata for " + artifact, e);
                }
            }
        }
    }
    logger.debug("Unable to resolve a remote repository containing {}", artifact);
    return null;
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MetadataRequest(org.eclipse.aether.resolution.MetadataRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MetadataResult(org.eclipse.aether.resolution.MetadataResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 52 with ArtifactResult

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

the class AetherBasedResolver method resolveFile.

/**
 * Resolve maven artifact as file in repository.
 */
public File resolveFile(Artifact artifact, MavenRepositoryURL repositoryURL, Exception previousException) throws IOException {
    List<LocalRepository> defaultRepos = selectDefaultRepositories();
    List<RemoteRepository> remoteRepos = selectRepositories();
    if (repositoryURL != null) {
        addRepo(remoteRepos, repositoryURL);
    }
    if (previousException != null) {
        // we'll try using previous repositories, without these that will fail again anyway
        List<RemoteRepository> altered = new LinkedList<>();
        RepositoryException repositoryException = findAetherException(previousException);
        if (repositoryException instanceof ArtifactResolutionException) {
            // check only this aggregate exception and assume it's related to current artifact
            ArtifactResult result = ((ArtifactResolutionException) repositoryException).getResult();
            if (result != null && result.getRequest() != null && result.getRequest().getArtifact().equals(artifact)) {
                // - these exceptions contain repository that was checked
                for (Exception exception : result.getExceptions()) {
                    RepositoryException singleException = findAetherException(exception);
                    if (singleException instanceof ArtifactTransferException) {
                        RemoteRepository repository = ((ArtifactTransferException) singleException).getRepository();
                        if (repository != null) {
                            RetryChance chance = isRetryableException(singleException);
                            if (chance == RetryChance.NEVER) {
                                LOG.debug("Removing " + repository + " from list of repositories, previous exception: " + singleException.getClass().getName() + ": " + singleException.getMessage());
                            } else {
                                altered.add(repository);
                            }
                        }
                    }
                }
                // swap list of repos now
                remoteRepos = altered;
            }
        }
    }
    assignProxyAndMirrors(remoteRepos);
    File resolved = resolve(defaultRepos, remoteRepos, artifact);
    LOG.debug("Resolved ({}) as {}", artifact.toString(), resolved.getAbsolutePath());
    return resolved;
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.eclipse.aether.transfer.ArtifactTransferException) LocalRepository(org.eclipse.aether.repository.LocalRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RepositoryException(org.eclipse.aether.RepositoryException) File(java.io.File) JarFile(java.util.jar.JarFile) LinkedList(java.util.LinkedList) 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) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 53 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project spf4j by zolyfarkas.

the class MavenRepositoryUtils method resolveArtifactAndDependencies.

public static Set<File> resolveArtifactAndDependencies(final String scope, final String groupId, final String artifactId, final String classifier, final String extension, final String versionExpr, final List<RemoteRepository> repos, final RepositorySystem repositorySystem, final RepositorySystemSession session) throws DependencyResolutionException {
    Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier, extension, versionExpr);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, scope));
    collectRequest.setRepositories(repos);
    DependencyFilter dependencyFilter = DependencyFilterUtils.classpathFilter(scope);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, dependencyFilter);
    DependencyResult depresult = repositorySystem.resolveDependencies(session, dependencyRequest);
    List<ArtifactResult> artifactResults = depresult.getArtifactResults();
    Set<File> result = Sets.newHashSetWithExpectedSize(artifactResults.size());
    for (ArtifactResult ar : artifactResults) {
        result.add(ar.getArtifact().getFile());
    }
    return result;
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyResult(org.eclipse.aether.resolution.DependencyResult) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) File(java.io.File) 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 54 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project liferay-ide by liferay.

the class AetherUtil method getLatestAvailableArtifact.

public static Artifact getLatestAvailableArtifact(String gavCoords) {
    Artifact retval = null;
    RepositorySystem system = newRepositorySystem();
    RepositorySystemSession session = newRepositorySystemSession(system);
    String latestVersion = getLatestVersion(gavCoords, system, session);
    String[] gav = gavCoords.split(":");
    Artifact defaultArtifact = new DefaultArtifact(gav[0] + ":" + gav[1] + ":" + latestVersion);
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(defaultArtifact);
    artifactRequest.addRepository(newCentralRepository());
    try {
        ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
        retval = artifactResult.getArtifact();
    } catch (ArtifactResolutionException e) {
        LiferayMavenCore.logError("Unable to get latest Liferay archetype", e);
        artifactRequest.setArtifact(new DefaultArtifact(gavCoords));
        try {
            retval = system.resolveArtifact(session, artifactRequest).getArtifact();
        } catch (ArtifactResolutionException e1) {
            LiferayMavenCore.logError("Unable to get default Liferay archetype", e1);
        }
    }
    if (retval == null) {
        retval = defaultArtifact;
    }
    return retval;
}
Also used : RepositorySystem(org.eclipse.aether.RepositorySystem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) 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 55 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project fabric8-maven-plugin by fabric8io.

the class AbstractArtifactSearchMojo method resolveArtifactFile.

protected File resolveArtifactFile(HelmIndexMojo.ArtifactDTO artifactDTO, String classifier, String extension) {
    File file = null;
    try {
        ArtifactRequest artifactRequest = new ArtifactRequest();
        org.eclipse.aether.artifact.Artifact artifact = new DefaultArtifact(artifactDTO.getG(), artifactDTO.getA(), classifier, extension, artifactDTO.getV());
        artifactRequest.setArtifact(artifact);
        // convert maven remote repositories to Aether repos
        List<RemoteRepository> aetherRepoList = new ArrayList<>();
        for (MavenArtifactRepository remoteRepository : remoteRepositories) {
            RemoteRepository.Builder builder = new RemoteRepository.Builder(remoteRepository.getId(), remoteRepository.getLayout().getId(), remoteRepository.getUrl());
            RemoteRepository aetherRepo = builder.build();
            aetherRepoList.add(aetherRepo);
        }
        artifactRequest.setRepositories(aetherRepoList);
        ArtifactResult artifactResult = artifactResolver.resolveArtifact(repoSession, artifactRequest);
        org.eclipse.aether.artifact.Artifact resolvedArtifact = artifactResult.getArtifact();
        if (resolvedArtifact == null) {
            getLog().warn("Could not resolve artifact " + artifactDTO.description());
            return null;
        }
        file = resolvedArtifact.getFile();
    } catch (Exception e) {
        getLog().warn("Failed to resolve manifest manifest for " + artifactDTO.description() + ". " + e, e);
        return null;
    }
    if (file == null) {
        getLog().warn("Could not resolve artifact file for " + artifactDTO.description());
        return null;
    }
    if (!file.isFile() || !file.exists()) {
        getLog().warn("Resolved artifact file does not exist for " + artifactDTO.description());
        return null;
    }
    return file;
}
Also used : ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MavenArtifactRepository(org.apache.maven.artifact.repository.MavenArtifactRepository) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

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