Search in sources :

Example 6 with ArtifactResult

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

the class Dependency31Helper method resolve.

@Override
public File resolve(Object artifact, Log log) {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact((Artifact) artifact);
    request.setRepositories(projectRepositories);
    log.debug("Resolving artifact " + artifact + " from " + projectRepositories);
    ArtifactResult result;
    try {
        result = repositorySystem.resolveArtifact(repositorySystemSession, request);
    } catch (ArtifactResolutionException e) {
        log.warn("Cound not resolve " + artifact, e);
        return null;
    }
    log.debug("Resolved artifact " + artifact + " to " + result.getArtifact().getFile() + " from " + result.getRepository());
    return result.getArtifact().getFile();
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 7 with ArtifactResult

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

the class Dependency31Helper method resolveById.

@Override
public File resolveById(String id, Log log) throws MojoFailureException {
    if (id.startsWith("mvn:")) {
        if (id.contains("!")) {
            id = id.substring(0, "mvn:".length()) + id.substring(id.indexOf("!") + 1);
        }
        if (id.endsWith("/")) {
            id = id.substring(0, id.length() - 1);
        }
    }
    id = MavenUtil.mvnToAether(id);
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(id));
    request.setRepositories(projectRepositories);
    log.debug("Resolving artifact " + id + " from " + projectRepositories);
    ArtifactResult result;
    try {
        result = repositorySystem.resolveArtifact(repositorySystemSession, request);
    } catch (ArtifactResolutionException e) {
        log.warn("Could not resolve " + id, e);
        throw new MojoFailureException(format("Couldn't resolve artifact %s", id), e);
    }
    log.debug("Resolved artifact " + id + " to " + result.getArtifact().getFile() + " from " + result.getRepository());
    return result.getArtifact().getFile();
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MojoFailureException(org.apache.maven.plugin.MojoFailureException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 8 with ArtifactResult

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

the class AetherRepository method get.

@Override
public File get(String bsn, Version version, Map<String, String> properties, DownloadListener... listeners) throws Exception {
    init();
    // Use the index by preference
    if (indexedRepo != null)
        return indexedRepo.get(ConversionUtils.maybeMavenCoordsToBsn(bsn), version, properties, listeners);
    File file = null;
    boolean getSource = false;
    try {
        // Setup the Aether repo session and request
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
        if (bsn.endsWith(".source")) {
            String originalBsn = properties.get("bsn");
            if (originalBsn != null) {
                bsn = originalBsn;
                getSource = true;
            }
        }
        String[] coords = ConversionUtils.getGroupAndArtifactForBsn(bsn);
        MavenVersion mvnVersion = new MavenVersion(version);
        String versionStr = null;
        if ("exact".equals(properties.get("strategy")) || getSource) {
            versionStr = properties.get("version");
        } else {
            versionStr = mvnVersion.toString();
        }
        Artifact artifact = null;
        if (getSource) {
            artifact = new DefaultArtifact(coords[0], coords[1], "sources", "jar", versionStr);
        } else {
            artifact = new DefaultArtifact(coords[0], coords[1], "jar", versionStr);
        }
        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(artifact);
        request.setRepositories(Collections.singletonList(remoteRepo));
        // Log the transfer
        session.setTransferListener(new AbstractTransferListener() {

            @Override
            public void transferStarted(TransferEvent event) throws TransferCancelledException {
                System.err.println(event);
            }

            @Override
            public void transferSucceeded(TransferEvent event) {
                System.err.println(event);
            }

            @Override
            public void transferFailed(TransferEvent event) {
                System.err.println(event);
            }
        });
        try {
            // Resolve the version
            ArtifactResult artifactResult = repoSystem.resolveArtifact(session, request);
            artifact = artifactResult.getArtifact();
            file = artifact.getFile();
        } catch (ArtifactResolutionException ex) {
        // could not download artifact, simply return null
        }
        return file;
    } finally {
        for (DownloadListener dl : listeners) {
            if (file != null)
                dl.success(file);
            else
                dl.failure(null, "Download failed");
        }
    }
}
Also used : AbstractTransferListener(org.eclipse.aether.transfer.AbstractTransferListener) TransferCancelledException(org.eclipse.aether.transfer.TransferCancelledException) TransferEvent(org.eclipse.aether.transfer.TransferEvent) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenVersion(aQute.bnd.version.MavenVersion) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 9 with ArtifactResult

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

the class DependencyResolver method getFileSetRepository.

public FileSetRepository getFileSetRepository(String name, Collection<File> bundlesInputParameter, boolean useMavenDependencies) throws Exception {
    Collection<File> bundles = new ArrayList<>();
    if (useMavenDependencies) {
        Map<File, ArtifactResult> dependencies = resolve();
        bundles.addAll(dependencies.keySet());
        // TODO Find a better way to get all the artifacts produced by this project!!!
        File current = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".jar");
        if (current.exists() && !bundles.contains(current)) {
            bundles.add(current);
        }
    }
    if (bundlesInputParameter != null) {
        bundles.addAll(bundlesInputParameter);
    }
    return new FileSetRepository(name, bundles);
}
Also used : FileSetRepository(aQute.bnd.repository.fileset.FileSetRepository) ArrayList(java.util.ArrayList) File(java.io.File) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 10 with ArtifactResult

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

the class DependencyResolver method discoverArtifacts.

private void discoverArtifacts(Map<File, ArtifactResult> files, List<DependencyNode> nodes, String parent, List<RemoteRepository> remoteRepositories) throws MojoExecutionException {
    for (DependencyNode node : nodes) {
        // Ensure that the file is downloaded so we can index it
        try {
            ArtifactResult resolvedArtifact = postProcessor.postProcessResult(system.resolveArtifact(session, new ArtifactRequest(node.getArtifact(), remoteRepositories, parent)));
            logger.debug("Located file: {} for artifact {}", resolvedArtifact.getArtifact().getFile(), resolvedArtifact);
            files.put(resolvedArtifact.getArtifact().getFile(), resolvedArtifact);
        } catch (ArtifactResolutionException e) {
            throw new MojoExecutionException("Failed to resolve the dependency " + node.getArtifact().toString(), e);
        }
        if (includeTransitive) {
            discoverArtifacts(files, node.getChildren(), node.getRequestContext(), remoteRepositories);
        } else {
            logger.debug("Ignoring transitive dependencies of {}", node.getDependency());
        }
    }
}
Also used : ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DependencyNode(org.eclipse.aether.graph.DependencyNode) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Aggregations

ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)23 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)14 File (java.io.File)12 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)11 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)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