Search in sources :

Example 46 with Artifact

use of org.eclipse.aether.artifact.Artifact in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiMavenService method getBomVersions.

/**
 * Returns the available Google Cloud Java client library BOM versions from Maven Central.
 *
 * @return returns the {@link Version versions} of the BOMs
 */
List<Version> getBomVersions() {
    Artifact artifact = new DefaultArtifact(toBomCoordinates(GOOGLE_CLOUD_JAVA_BOM_ALL_VERSIONS_CONSTRAINT));
    VersionRangeRequest rangeRequest = new VersionRangeRequest();
    rangeRequest.setArtifact(artifact);
    rangeRequest.addRepository(MAVEN_CENTRAL_REPOSITORY);
    try {
        VersionRangeResult result = SYSTEM.resolveVersionRange(SESSION, rangeRequest);
        return result.getVersions();
    } catch (VersionRangeResolutionException e) {
        logger.warn("Error fetching available BOM versions from Maven Central", e);
        return ImmutableList.of();
    }
}
Also used : VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException)

Example 47 with Artifact

use of org.eclipse.aether.artifact.Artifact in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiMavenService method getManagedDependencyVersion.

/**
 * Finds the version of the passed in {@link CloudLibraryClientMavenCoordinates} that is managed
 * by the given BOM version.
 *
 * @param libraryMavenCoordinates the maven coordinates of the {@link CloudLibrary} for which we
 *     are finding the version
 * @param bomVersion the version of the BOM from which to fetch the library version
 * @return the optional version of the library found in the given BOM
 */
Optional<String> getManagedDependencyVersion(CloudLibraryClientMavenCoordinates libraryMavenCoordinates, String bomVersion) {
    Artifact bomArtifact = new DefaultArtifact(toBomCoordinates(bomVersion));
    ArtifactDescriptorRequest request = new ArtifactDescriptorRequest();
    request.setArtifact(bomArtifact);
    request.addRepository(MAVEN_CENTRAL_REPOSITORY);
    try {
        ArtifactDescriptorResult result = SYSTEM.readArtifactDescriptor(SESSION, request);
        return result.getManagedDependencies().stream().filter(dependency -> {
            Artifact artifact = dependency.getArtifact();
            String coordinatesFromBom = toFormattedMavenCoordinates(libraryMavenCoordinates.getGroupId(), libraryMavenCoordinates.getArtifactId());
            String libraryCoordinates = toFormattedMavenCoordinates(artifact.getGroupId(), artifact.getArtifactId());
            return coordinatesFromBom.equalsIgnoreCase(libraryCoordinates);
        }).findFirst().map(dependency -> dependency.getArtifact().getVersion());
    } catch (ArtifactDescriptorException e) {
        logger.warn("Error fetching version of client library from bom version " + bomVersion);
        return Optional.empty();
    }
}
Also used : ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest)

Example 48 with Artifact

use of org.eclipse.aether.artifact.Artifact in project revapi by revapi.

the class ReportAggregateMojo method getRunConfig.

private ProjectVersions getRunConfig(MavenProject project) {
    ProjectVersions ret = new ProjectVersions();
    Plugin revapiPlugin = findRevapi(project);
    if (revapiPlugin == null) {
        return ret;
    }
    Xpp3Dom pluginConfig = (Xpp3Dom) revapiPlugin.getConfiguration();
    String[] oldArtifacts = getArtifacts(pluginConfig, "oldArtifacts");
    String[] newArtifacts = getArtifacts(pluginConfig, "newArtifacts");
    String oldVersion = getValueOfChild(pluginConfig, "oldVersion");
    if (oldVersion == null) {
        oldVersion = System.getProperties().getProperty(Props.oldVersion.NAME, Props.oldVersion.DEFAULT_VALUE);
    }
    String newVersion = getValueOfChild(pluginConfig, "newVersion");
    if (newVersion == null) {
        newVersion = System.getProperties().getProperty(Props.newVersion.NAME, project.getVersion());
    }
    String defaultOldArtifact = Analyzer.getProjectArtifactCoordinates(project, oldVersion);
    String defaultNewArtifact = Analyzer.getProjectArtifactCoordinates(project, newVersion);
    if (oldArtifacts == null || oldArtifacts.length == 0) {
        if (!project.getArtifact().getArtifactHandler().isAddedToClasspath()) {
            return ret;
        }
        oldArtifacts = new String[] { defaultOldArtifact };
    }
    if (newArtifacts == null || newArtifacts.length == 0) {
        if (!project.getArtifact().getArtifactHandler().isAddedToClasspath()) {
            return ret;
        }
        newArtifacts = new String[] { defaultNewArtifact };
    }
    String versionRegexString = getValueOfChild(pluginConfig, "versionFormat");
    Pattern versionRegex = versionRegexString == null ? null : Pattern.compile(versionRegexString);
    DefaultRepositorySystemSession session = new DefaultRepositorySystemSession(repositorySystemSession);
    session.setDependencySelector(new ScopeDependencySelector("compile", "provided"));
    session.setDependencyTraverser(new ScopeDependencyTraverser("compile", "provided"));
    if (alwaysCheckForReleaseVersion) {
        session.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS);
    }
    ArtifactResolver resolver = new ArtifactResolver(repositorySystem, session, mavenSession.getCurrentProject().getRemoteProjectRepositories());
    Function<String, Artifact> resolve = gav -> {
        try {
            return Analyzer.resolveConstrained(project, gav, versionRegex, resolver);
        } catch (VersionRangeResolutionException | ArtifactResolutionException e) {
            getLog().warn("Could not resolve artifact '" + gav + "' with message: " + e.getMessage());
            return null;
        }
    };
    ret.oldGavs = Stream.of(oldArtifacts).map(resolve).filter(f -> f != null).toArray(Artifact[]::new);
    ret.newGavs = Stream.of(newArtifacts).map(resolve).filter(f -> f != null).toArray(Artifact[]::new);
    return ret;
}
Also used : Component(org.apache.maven.plugins.annotations.Component) HashMap(java.util.HashMap) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) Function(java.util.function.Function) Sink(org.apache.maven.doxia.sink.Sink) MessageFormat(java.text.MessageFormat) Execute(org.apache.maven.plugins.annotations.Execute) Mojo(org.apache.maven.plugins.annotations.Mojo) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) ResourceBundle(java.util.ResourceBundle) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Map(java.util.Map) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) SITE(org.apache.maven.plugins.annotations.LifecyclePhase.SITE) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) API(org.revapi.API) MavenSession(org.apache.maven.execution.MavenSession) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) MavenReportException(org.apache.maven.reporting.MavenReportException) Artifact(org.eclipse.aether.artifact.Artifact) AnalysisResult(org.revapi.AnalysisResult) Collectors(java.util.stream.Collectors) Revapi(org.revapi.Revapi) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) PACKAGE(org.apache.maven.plugins.annotations.LifecyclePhase.PACKAGE) List(java.util.List) Plugin(org.apache.maven.model.Plugin) Stream(java.util.stream.Stream) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector) Pattern(java.util.regex.Pattern) Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) Artifact(org.eclipse.aether.artifact.Artifact) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) Plugin(org.apache.maven.model.Plugin) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector)

Example 49 with Artifact

use of org.eclipse.aether.artifact.Artifact in project revapi by revapi.

the class ArtifactResolver method makeRemoteOnly.

private RepositorySystemSession makeRemoteOnly(RepositorySystemSession session) {
    return new AbstractForwardingRepositorySystemSession() {

        @Override
        protected RepositorySystemSession getSession() {
            return session;
        }

        @Override
        public WorkspaceReader getWorkspaceReader() {
            return null;
        }

        @Override
        public LocalRepositoryManager getLocalRepositoryManager() {
            LocalRepositoryManager wrapped = session.getLocalRepositoryManager();
            return new LocalRepositoryManager() {

                @Override
                public LocalRepository getRepository() {
                    return wrapped.getRepository();
                }

                @Override
                public String getPathForLocalArtifact(Artifact artifact) {
                    return wrapped.getPathForLocalArtifact(artifact);
                }

                @Override
                public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository repository, String context) {
                    return wrapped.getPathForRemoteArtifact(artifact, repository, context);
                }

                @Override
                public String getPathForLocalMetadata(Metadata metadata) {
                    return wrapped.getPathForLocalMetadata(metadata);
                }

                @Override
                public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository repository, String context) {
                    return wrapped.getPathForRemoteMetadata(metadata, repository, context);
                }

                @Override
                public LocalArtifactResult find(RepositorySystemSession session, LocalArtifactRequest request) {
                    return wrapped.find(session, request);
                }

                @Override
                public void add(RepositorySystemSession session, LocalArtifactRegistration request) {
                    wrapped.add(session, request);
                }

                @Override
                public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
                    if (request.getRepository() == null) {
                        // local metadata request... the returned file must not be null but may not exist
                        // we exploit that to not include the locally built results
                        LocalMetadataResult ret = new LocalMetadataResult(request);
                        ret.setFile(new File("<faked-to-force-remote-only-resolution-of-artifacts>"));
                        return ret;
                    } else {
                        return wrapped.find(session, request);
                    }
                }

                @Override
                public void add(RepositorySystemSession session, LocalMetadataRegistration request) {
                    wrapped.add(session, request);
                }
            };
        }
    };
}
Also used : AbstractForwardingRepositorySystemSession(org.eclipse.aether.AbstractForwardingRepositorySystemSession) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) LocalArtifactRegistration(org.eclipse.aether.repository.LocalArtifactRegistration) LocalMetadataRequest(org.eclipse.aether.repository.LocalMetadataRequest) LocalMetadataRegistration(org.eclipse.aether.repository.LocalMetadataRegistration) Metadata(org.eclipse.aether.metadata.Metadata) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) LocalRepositoryManager(org.eclipse.aether.repository.LocalRepositoryManager) LocalMetadataResult(org.eclipse.aether.repository.LocalMetadataResult) AbstractForwardingRepositorySystemSession(org.eclipse.aether.AbstractForwardingRepositorySystemSession) File(java.io.File) LocalArtifactRequest(org.eclipse.aether.repository.LocalArtifactRequest)

Example 50 with Artifact

use of org.eclipse.aether.artifact.Artifact in project revapi by revapi.

the class ArtifactResolver method collectTransitiveDeps.

protected void collectTransitiveDeps(String gav, Set<Artifact> resolvedArtifacts, Set<Exception> failures) throws RepositoryException {
    final Artifact rootArtifact = resolveArtifact(gav);
    CollectRequest collectRequest = new CollectRequest(new Dependency(rootArtifact, null), repositories);
    DependencyRequest request = new DependencyRequest(collectRequest, null);
    DependencyResult result;
    try {
        result = repositorySystem.resolveDependencies(session, request);
    } catch (DependencyResolutionException dre) {
        result = dre.getResult();
    }
    result.getRoot().accept(new TreeDependencyVisitor(new DependencyVisitor() {

        @Override
        public boolean visitEnter(DependencyNode node) {
            return true;
        }

        @Override
        public boolean visitLeave(DependencyNode node) {
            Dependency dep = node.getDependency();
            if (dep == null || dep.getArtifact().equals(rootArtifact)) {
                return true;
            }
            resolvedArtifacts.add(dep.getArtifact());
            return true;
        }
    }));
    failures.addAll(result.getCollectExceptions());
}
Also used : DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyResult(org.eclipse.aether.resolution.DependencyResult) DependencyVisitor(org.eclipse.aether.graph.DependencyVisitor) TreeDependencyVisitor(org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor) DependencyNode(org.eclipse.aether.graph.DependencyNode) TreeDependencyVisitor(org.eclipse.aether.util.graph.visitor.TreeDependencyVisitor) DependencyResolutionException(org.eclipse.aether.resolution.DependencyResolutionException) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact)

Aggregations

Artifact (org.eclipse.aether.artifact.Artifact)122 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)97 File (java.io.File)51 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)34 Dependency (org.eclipse.aether.graph.Dependency)32 IOException (java.io.IOException)29 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)26 ArrayList (java.util.ArrayList)23 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)22 List (java.util.List)20 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)18 CollectRequest (org.eclipse.aether.collection.CollectRequest)15 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)15 DependencyNode (org.eclipse.aether.graph.DependencyNode)14 ArtifactDescriptorResult (org.eclipse.aether.resolution.ArtifactDescriptorResult)14 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)13 DependencyFilter (org.eclipse.aether.graph.DependencyFilter)13 URL (java.net.URL)12 Map (java.util.Map)12 RepositorySystem (org.eclipse.aether.RepositorySystem)11