Search in sources :

Example 46 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 47 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)

Example 48 with ArtifactResult

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

the class CustomMaven3ArtifactResolver method resolveOld.

private void resolveOld(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) 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)

Example 49 with ArtifactResult

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

the class ArtifactRepositoryManager method resolveDependency.

public Collection<File> resolveDependency(String groupId, String artifactId, String version) throws Exception {
    final DependencyRequest dependencyRequest = new DependencyRequest(createCollectRequest(groupId, artifactId, toVersion(version)), DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE));
    final DependencyResult result = ourSystem.resolveDependencies(mySession, dependencyRequest);
    final List<File> files = new ArrayList<>();
    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) File(java.io.File) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 50 with ArtifactResult

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

the class DependencyResolver method resolve.

public Map<File, ArtifactResult> resolve() throws MojoExecutionException {
    if (resolvedDependencies != null) {
        return resolvedDependencies;
    }
    DependencyResolutionRequest request = new DefaultDependencyResolutionRequest(project, session);
    request.setResolutionFilter(new DependencyFilter() {

        @Override
        public boolean accept(DependencyNode node, List<DependencyNode> parents) {
            if (node.getDependency() != null) {
                return scopes.contains(node.getDependency().getScope());
            }
            return false;
        }
    });
    DependencyResolutionResult result;
    try {
        result = resolver.resolve(request);
    } catch (DependencyResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    Map<File, ArtifactResult> dependencies = new HashMap<>();
    DependencyNode dependencyGraph = result.getDependencyGraph();
    if (dependencyGraph != null && !dependencyGraph.getChildren().isEmpty()) {
        List<RemoteRepository> remoteRepositories = new ArrayList<>(project.getRemoteProjectRepositories());
        ArtifactRepository deployRepo = project.getDistributionManagementArtifactRepository();
        if (deployRepo != null) {
            remoteRepositories.add(RepositoryUtils.toRepo(deployRepo));
        }
        discoverArtifacts(dependencies, dependencyGraph.getChildren(), project.getArtifact().getId(), remoteRepositories);
    }
    return resolvedDependencies = dependencies;
}
Also used : DependencyResolutionResult(org.apache.maven.project.DependencyResolutionResult) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DependencyFilter(org.eclipse.aether.graph.DependencyFilter) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DependencyResolutionRequest(org.apache.maven.project.DependencyResolutionRequest) DefaultDependencyResolutionRequest(org.apache.maven.project.DefaultDependencyResolutionRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) DependencyResolutionException(org.apache.maven.project.DependencyResolutionException) File(java.io.File)

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