Search in sources :

Example 16 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project che by eclipse.

the class CheArtifactResolver method resolveOrig.

private void resolveOrig(Artifact artifact, List<ArtifactRepository> remoteRepositories, RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException {
    if (artifact == null) {
        return;
    }
    if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
        File systemFile = artifact.getFile();
        if (systemFile == null) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " has no file attached", artifact);
        }
        if (!systemFile.exists()) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " not found in path: " + systemFile, artifact);
        }
        if (!systemFile.isFile()) {
            throw new ArtifactNotFoundException("System artifact: " + artifact + " is not a file: " + systemFile, artifact);
        }
        artifact.setResolved(true);
        return;
    }
    if (!artifact.isResolved()) {
        ArtifactResult result;
        try {
            ArtifactRequest artifactRequest = new ArtifactRequest();
            artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact));
            artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories));
            // Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved or not
            LocalRepositoryManager lrm = session.getLocalRepositoryManager();
            String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact());
            artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
            result = repoSystem.resolveArtifact(session, artifactRequest);
        } catch (org.eclipse.aether.resolution.ArtifactResolutionException e) {
            if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) {
                throw new ArtifactNotFoundException(e.getMessage(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(), artifact.getDependencyTrail(), e);
            } else {
                throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e);
            }
        }
        artifact.selectVersion(result.getArtifact().getVersion());
        artifact.setFile(result.getArtifact().getFile());
        artifact.setResolved(true);
        if (artifact.isSnapshot()) {
            Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
            if (matcher.matches()) {
                Snapshot snapshot = new Snapshot();
                snapshot.setTimestamp(matcher.group(2));
                try {
                    snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
                    artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
                } catch (NumberFormatException e) {
                    logger.warn("Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage());
                }
            }
        }
    }
}
Also used : Matcher(java.util.regex.Matcher) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) Snapshot(org.apache.maven.artifact.repository.metadata.Snapshot) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) LegacyLocalRepositoryManager(org.apache.maven.artifact.repository.LegacyLocalRepositoryManager) SnapshotArtifactRepositoryMetadata(org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata) File(java.io.File) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException)

Example 17 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project che by eclipse.

the class MavenServerImpl method resolveArtifact.

@Override
public MavenArtifact resolveArtifact(MavenArtifactKey artifactKey, List<MavenRemoteRepository> repositories) throws RemoteException {
    Artifact artifact = getMavenComponent(ArtifactFactory.class).createArtifactWithClassifier(artifactKey.getGroupId(), artifactKey.getArtifactId(), artifactKey.getVersion(), artifactKey.getPackaging(), artifactKey.getClassifier());
    List<ArtifactRepository> repos = new ArrayList<>();
    ArtifactRepositoryFactory factory = getMavenComponent(ArtifactRepositoryFactory.class);
    for (MavenRemoteRepository repository : repositories) {
        try {
            ArtifactRepository artifactRepository = ProjectUtils.buildArtifactRepository(MavenModelUtil.convertToMavenRepository(repository), factory, container);
            repos.add(artifactRepository);
        } catch (InvalidRepositoryException e) {
            MavenServerContext.getLogger().error(e);
        }
    }
    MavenExecutionRequest request = newMavenRequest(null, Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
    repos.forEach(request::addRemoteRepository);
    DefaultMaven maven = (DefaultMaven) getMavenComponent(Maven.class);
    RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);
    org.eclipse.aether.impl.ArtifactResolver artifactResolver = getMavenComponent(org.eclipse.aether.impl.ArtifactResolver.class);
    InternalLoggerFactory loggerFactory = new InternalLoggerFactory();
    if (artifactResolver instanceof DefaultArtifactResolver) {
        ((DefaultArtifactResolver) artifactResolver).setLoggerFactory(loggerFactory);
    }
    org.eclipse.aether.RepositorySystem repositorySystem = getMavenComponent(org.eclipse.aether.RepositorySystem.class);
    if (repositorySystem instanceof DefaultRepositorySystem) {
        ((DefaultRepositorySystem) repositorySystem).setLoggerFactory(loggerFactory);
    }
    List<RemoteRepository> remoteRepositories = RepositoryUtils.toRepos(request.getRemoteRepositories());
    remoteRepositories = repositorySystem.newResolutionRepositories(repositorySystemSession, remoteRepositories);
    try {
        ArtifactResult artifactResult = repositorySystem.resolveArtifact(repositorySystemSession, new ArtifactRequest(RepositoryUtils.toArtifact(artifact), remoteRepositories, null));
        return MavenModelUtil.convertArtifact(RepositoryUtils.toArtifact(artifactResult.getArtifact()), localRepository);
    } catch (ArtifactResolutionException e) {
        MavenServerContext.getLogger().info(e);
    }
    return MavenModelUtil.convertArtifact(artifact, localRepository);
}
Also used : DefaultMaven(org.apache.maven.DefaultMaven) Maven(org.apache.maven.Maven) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) DefaultRepositorySystem(org.eclipse.aether.internal.impl.DefaultRepositorySystem) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) ArrayList(java.util.ArrayList) MavenRemoteRepository(org.eclipse.che.maven.data.MavenRemoteRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifactResolver(org.eclipse.aether.internal.impl.DefaultArtifactResolver) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactRepositoryFactory(org.apache.maven.artifact.repository.ArtifactRepositoryFactory) DefaultMaven(org.apache.maven.DefaultMaven) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) MavenArtifact(org.eclipse.che.maven.data.MavenArtifact) Artifact(org.apache.maven.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) MavenRemoteRepository(org.eclipse.che.maven.data.MavenRemoteRepository)

Example 18 with ArtifactResult

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

the class AbstractGrailsMojo method resolveArtifactIds.

protected Collection<File> resolveArtifactIds(Collection<String> artifactIds) throws MojoExecutionException {
    Collection<ArtifactRequest> requests = new ArrayList<ArtifactRequest>();
    for (String artifactId : artifactIds) {
        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(new DefaultArtifact(artifactId));
        request.setRepositories(remoteRepos);
        getLog().debug("Resolving artifact " + artifactId + " from " + remoteRepos);
        requests.add(request);
    }
    Collection<File> files = new ArrayList<File>();
    try {
        List<ArtifactResult> result = repoSystem.resolveArtifacts(repoSession, requests);
        for (ArtifactResult artifactResult : result) {
            File file = artifactResult.getArtifact().getFile();
            files.add(file);
            getLog().debug("Resolved artifact " + artifactResult.getArtifact().getArtifactId() + " to " + file + " from " + artifactResult.getRepository());
        }
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    return files;
}
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 19 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project pinpoint by naver.

the class DependencyResolver method resolveArtifactsAndDependencies.

public List<File> resolveArtifactsAndDependencies(List<Artifact> artifacts) throws ArtifactResolutionException, DependencyResolutionException {
    List<Dependency> dependencies = new ArrayList<Dependency>();
    for (Artifact artifact : artifacts) {
        dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME));
    }
    CollectRequest collectRequest = new CollectRequest((Dependency) null, dependencies, repositories);
    DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);
    DependencyResult result = system.resolveDependencies(session, dependencyRequest);
    List<File> files = new ArrayList<File>();
    for (ArtifactResult artifactResult : result.getArtifactResults()) {
        files.add(artifactResult.getArtifact().getFile());
    }
    return files;
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyResult(org.eclipse.aether.resolution.DependencyResult) ArrayList(java.util.ArrayList) 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) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 20 with ArtifactResult

use of org.eclipse.aether.resolution.ArtifactResult in project intellij-community by JetBrains.

the class Maven3ServerEmbedderImpl method resolve.

private Artifact resolve(@NotNull final Artifact artifact, @NotNull final List<ArtifactRepository> repos) throws ArtifactResolutionException, ArtifactNotFoundException, RemoteException, org.eclipse.aether.resolution.ArtifactResolutionException {
    final String mavenVersion = getMavenVersion();
    // see https://youtrack.jetbrains.com/issue/IDEA-140208 for details
    if (USE_MVN2_COMPATIBLE_DEPENDENCY_RESOLVING || StringUtil.compareVersionNumbers(mavenVersion, "3.2.5") < 0) {
        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
        request.setRemoteRepositories(repos);
        try {
            getComponent(MavenExecutionRequestPopulator.class).populateFromSettings(request, myMavenSettings);
            getComponent(MavenExecutionRequestPopulator.class).populateDefaults(request);
        } catch (MavenExecutionRequestPopulationException e) {
            throw new RuntimeException(e);
        }
        getComponent(ArtifactResolver.class).resolve(artifact, request.getRemoteRepositories(), myLocalRepository);
        return artifact;
    } else {
        final MavenExecutionRequest request = createRequest(null, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList());
        for (ArtifactRepository artifactRepository : repos) {
            request.addRemoteRepository(artifactRepository);
        }
        DefaultMaven maven = (DefaultMaven) getComponent(Maven.class);
        RepositorySystemSession repositorySystemSession = maven.newRepositorySession(request);
        final org.eclipse.aether.impl.ArtifactResolver artifactResolver = getComponent(org.eclipse.aether.impl.ArtifactResolver.class);
        final MyLoggerFactory loggerFactory = new MyLoggerFactory();
        if (artifactResolver instanceof DefaultArtifactResolver) {
            ((DefaultArtifactResolver) artifactResolver).setLoggerFactory(loggerFactory);
        }
        final org.eclipse.aether.RepositorySystem repositorySystem = getComponent(org.eclipse.aether.RepositorySystem.class);
        if (repositorySystem instanceof DefaultRepositorySystem) {
            ((DefaultRepositorySystem) repositorySystem).setLoggerFactory(loggerFactory);
        }
        List<RemoteRepository> repositories = RepositoryUtils.toRepos(request.getRemoteRepositories());
        repositories = repositorySystem.newResolutionRepositories(repositorySystemSession, repositories);
        final ArtifactResult artifactResult = repositorySystem.resolveArtifact(repositorySystemSession, new ArtifactRequest(RepositoryUtils.toArtifact(artifact), repositories, null));
        return RepositoryUtils.toArtifact(artifactResult.getArtifact());
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) DefaultRepositorySystem(org.eclipse.aether.internal.impl.DefaultRepositorySystem) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) ArtifactResolver(org.apache.maven.artifact.resolver.ArtifactResolver) DefaultArtifactResolver(org.eclipse.aether.internal.impl.DefaultArtifactResolver) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifactResolver(org.eclipse.aether.internal.impl.DefaultArtifactResolver)

Aggregations

ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)24 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)15 File (java.io.File)13 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)12 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)9 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)8 ArrayList (java.util.ArrayList)6 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)6 Artifact (org.eclipse.aether.artifact.Artifact)6 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)4 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)4 DependencyResult (org.eclipse.aether.resolution.DependencyResult)4 MojoFailureException (org.apache.maven.plugin.MojoFailureException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Matcher (java.util.regex.Matcher)2 LegacyLocalRepositoryManager (org.apache.maven.artifact.repository.LegacyLocalRepositoryManager)2 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)2 Snapshot (org.apache.maven.artifact.repository.metadata.Snapshot)2