Search in sources :

Example 96 with Artifact

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

the class Analyzer method collectDeps.

@SuppressWarnings("unchecked")
private Set<MavenArchive> collectDeps(String depDescription, ArtifactResolver.CollectionResult res) {
    Set<MavenArchive> ret = null;
    try {
        ret = new HashSet<>();
        for (Artifact a : res.getResolvedArtifacts()) {
            try {
                ret.add(MavenArchive.of(a));
            } catch (IllegalArgumentException e) {
                res.getFailures().add(e);
            }
        }
        if (!res.getFailures().isEmpty()) {
            StringBuilder bld = new StringBuilder();
            for (Exception e : res.getFailures()) {
                bld.append(e.getMessage()).append(", ");
            }
            bld.replace(bld.length() - 2, bld.length(), "");
            throw new MarkerException("Resolution of some artifacts failed: " + bld.toString());
        } else {
            return ret;
        }
    } catch (MarkerException e) {
        return handleResolutionError(e, depDescription, ret);
    }
}
Also used : Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) FileNotFoundException(java.io.FileNotFoundException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) RepositoryException(org.eclipse.aether.RepositoryException)

Example 97 with Artifact

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

the class Analyzer method resolveArtifacts.

@SuppressWarnings("unchecked")
void resolveArtifacts() {
    if (resolvedOldApi == null) {
        final ArtifactResolver resolver = new ArtifactResolver(repositorySystem, repositorySystemSession, project.getRemoteProjectRepositories());
        Function<String, MavenArchive> toFileArchive = gav -> {
            try {
                Artifact a = resolveConstrained(project, gav, versionRegex, resolver);
                return MavenArchive.of(a);
            } catch (ArtifactResolutionException | VersionRangeResolutionException | IllegalArgumentException e) {
                throw new MarkerException(e.getMessage(), e);
            }
        };
        List<MavenArchive> oldArchives = new ArrayList<>(1);
        try {
            if (oldGavs != null) {
                oldArchives = Stream.of(oldGavs).map(toFileArchive).collect(toList());
            }
            if (oldArtifacts != null) {
                oldArchives.addAll(Stream.of(oldArtifacts).map(MavenArchive::of).collect(toList()));
            }
        } catch (MarkerException | IllegalArgumentException e) {
            String message = "Failed to resolve old artifacts: " + e.getMessage() + ".";
            if (failOnMissingArchives) {
                throw new IllegalStateException(message, e);
            } else {
                log.warn(message + " The API analysis will proceed comparing the new archives against an empty" + " archive.");
            }
        }
        List<MavenArchive> newArchives = new ArrayList<>(1);
        try {
            if (newGavs != null) {
                newArchives = Stream.of(newGavs).map(toFileArchive).collect(toList());
            }
            if (newArtifacts != null) {
                newArchives.addAll(Stream.of(newArtifacts).map(MavenArchive::of).collect(toList()));
            }
        } catch (MarkerException | IllegalArgumentException e) {
            String message = "Failed to resolve new artifacts: " + e.getMessage() + ".";
            if (failOnMissingArchives) {
                throw new IllegalStateException(message, e);
            } else {
                log.warn(message + " The API analysis will not proceed.");
                return;
            }
        }
        // now we need to be a little bit clever. When using RELEASE or LATEST as the version of the old artifact
        // it might happen that it gets resolved to the same version as the new artifacts - this notoriously happens
        // when releasing using the release plugin - you first build your artifacts, put them into the local repo
        // and then do the site updates for the released version. When you do the site, maven will find the released
        // version in the repo and resolve RELEASE to it. You compare it against what you just built, i.e. the same
        // code, et voila, the site report doesn't ever contain any found differences...
        Set<MavenArchive> oldTransitiveDeps = new HashSet<>();
        Set<MavenArchive> newTransitiveDeps = new HashSet<>();
        if (resolveDependencies) {
            String[] resolvedOld = oldArchives.stream().map(MavenArchive::getName).toArray(String[]::new);
            String[] resolvedNew = newArchives.stream().map(MavenArchive::getName).toArray(String[]::new);
            oldTransitiveDeps.addAll(collectDeps("old", resolver, resolvedOld));
            newTransitiveDeps.addAll(collectDeps("new", resolver, resolvedNew));
        }
        resolvedOldApi = API.of(oldArchives).supportedBy(oldTransitiveDeps).build();
        resolvedNewApi = API.of(newArchives).supportedBy(newTransitiveDeps).build();
    }
}
Also used : Arrays(java.util.Arrays) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) PlexusConfiguration(org.codehaus.plexus.configuration.PlexusConfiguration) XmlToJson(org.revapi.configuration.XmlToJson) MavenProject(org.apache.maven.project.MavenProject) Locale(java.util.Locale) Map(java.util.Map) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) API(org.revapi.API) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) Set(java.util.Set) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) Artifact(org.eclipse.aether.artifact.Artifact) AnalysisResult(org.revapi.AnalysisResult) Reader(java.io.Reader) Revapi(org.revapi.Revapi) FileNotFoundException(java.io.FileNotFoundException) ValidationResult(org.revapi.configuration.ValidationResult) List(java.util.List) Stream(java.util.stream.Stream) RepositoryUtils(org.apache.maven.RepositoryUtils) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) ModelNode(org.jboss.dmr.ModelNode) Pattern(java.util.regex.Pattern) Spliterator(java.util.Spliterator) RepositorySystem(org.eclipse.aether.RepositorySystem) Reporter(org.revapi.Reporter) XmlPlexusConfiguration(org.codehaus.plexus.configuration.xml.XmlPlexusConfiguration) Xpp3DomBuilder(org.codehaus.plexus.util.xml.Xpp3DomBuilder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) StreamSupport(java.util.stream.StreamSupport) JSONUtil(org.revapi.configuration.JSONUtil) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) Iterator(java.util.Iterator) AnalysisContext(org.revapi.AnalysisContext) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Log(org.apache.maven.plugin.logging.Log) InputStreamReader(java.io.InputStreamReader) File(java.io.File) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) Collectors.toList(java.util.stream.Collectors.toList) RepositoryException(org.eclipse.aether.RepositoryException) Collections(java.util.Collections) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) HashSet(java.util.HashSet)

Example 98 with Artifact

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

the class Analyzer method resolveConstrained.

/**
 * Resolves the gav using the resolver. If the gav corresponds to the project artifact and is an unresolved version
 * for a RELEASE or LATEST, the gav is resolved such it a release not newer than the project version is found that
 * optionally corresponds to the provided version regex, if provided.
 *
 * <p>If the gav exactly matches the current project, the file of the artifact is found on the filesystem in
 * target directory and the resolver is ignored.
 *
 * @param project the project to restrict by, if applicable
 * @param gav the gav to resolve
 * @param versionRegex the optional regex the version must match to be considered.
 * @param resolver the version resolver to use
 * @return the resolved artifact matching the criteria.
 *
 * @throws VersionRangeResolutionException on error
 * @throws ArtifactResolutionException on error
 */
static Artifact resolveConstrained(MavenProject project, String gav, Pattern versionRegex, ArtifactResolver resolver) throws VersionRangeResolutionException, ArtifactResolutionException {
    boolean latest = gav.endsWith(":LATEST");
    if (latest || gav.endsWith(":RELEASE")) {
        Artifact a = new DefaultArtifact(gav);
        if (latest) {
            versionRegex = versionRegex == null ? ANY : versionRegex;
        } else {
            versionRegex = versionRegex == null ? ANY_NON_SNAPSHOT : versionRegex;
        }
        String upTo = project.getGroupId().equals(a.getGroupId()) && project.getArtifactId().equals(a.getArtifactId()) ? project.getVersion() : null;
        return resolver.resolveNewestMatching(gav, upTo, versionRegex, latest, latest);
    } else {
        String projectGav = getProjectArtifactCoordinates(project, null);
        Artifact ret = null;
        if (projectGav.equals(gav)) {
            ret = findProjectArtifact(project);
        }
        return ret == null ? resolver.resolveArtifact(gav) : ret;
    }
}
Also used : Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 99 with Artifact

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

the class ReportAggregateMojo method prepareAnalyzer.

private Analyzer prepareAnalyzer(Revapi revapi, MavenProject project, Locale locale, ProjectVersions storedVersions) {
    Plugin runPluginConfig = findRevapi(project);
    if (runPluginConfig == null) {
        return null;
    }
    Xpp3Dom runConfig = (Xpp3Dom) runPluginConfig.getConfiguration();
    Artifact[] oldArtifacts = storedVersions.oldGavs;
    Artifact[] newArtifacts = storedVersions.newGavs;
    if (oldArtifacts == null || oldArtifacts.length == 0 || newArtifacts == null || newArtifacts.length == 0) {
        return null;
    }
    String versionRegex = getValueOfChild(runConfig, "versionFormat");
    AnalyzerBuilder bld = AnalyzerBuilder.forArtifacts(oldArtifacts, newArtifacts).withAlwaysCheckForReleasedVersion(this.alwaysCheckForReleaseVersion).withAnalysisConfiguration(this.analysisConfiguration).withAnalysisConfigurationFiles(this.analysisConfigurationFiles).withCheckDependencies(this.checkDependencies).withResolveProvidedDependencies(this.resolveProvidedDependencies).withDisallowedExtensions(disallowedExtensions).withFailOnMissingConfigurationFiles(this.failOnMissingConfigurationFiles).withFailOnUnresolvedArtifacts(this.failOnUnresolvedArtifacts).withFailOnUnresolvedDependencies(this.failOnUnresolvedDependencies).withLocale(locale).withLog(getLog()).withProject(project).withRepositorySystem(repositorySystem).withRepositorySystemSession(repositorySystemSession).withSkip(skip).withVersionFormat(versionRegex);
    if (revapi == null) {
        bld = bld.withReporter(ReportTimeReporter.class);
    } else {
        bld = bld.withRevapiInstance(revapi);
    }
    Map<String, Object> contextData = new HashMap<>(1);
    contextData.put(ReportTimeReporter.MIN_SEVERITY_KEY, reportSeverity.asDifferenceSeverity());
    bld.withContextData(contextData);
    return bld.build().analyzer;
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) HashMap(java.util.HashMap) Artifact(org.eclipse.aether.artifact.Artifact) Plugin(org.apache.maven.model.Plugin)

Example 100 with Artifact

use of org.eclipse.aether.artifact.Artifact in project wildfly-swarm by wildfly-swarm.

the class MavenArtifactResolvingHelper method resolveAll.

@Override
public Set<ArtifactSpec> resolveAll(Collection<ArtifactSpec> specs, boolean transitive, boolean defaultExcludes) throws Exception {
    if (specs.isEmpty()) {
        return Collections.emptySet();
    }
    List<DependencyNode> nodes;
    if (transitive) {
        final CollectRequest request = new CollectRequest();
        request.setRepositories(this.remoteRepositories);
        // SWARM-1031
        if (this.dependencyManagement != null) {
            List<Dependency> managedDependencies = this.dependencyManagement.getDependencies().stream().map(mavenDep -> {
                DefaultArtifact artifact = new DefaultArtifact(mavenDep.getGroupId(), mavenDep.getArtifactId(), mavenDep.getType(), mavenDep.getVersion());
                return new Dependency(artifact, mavenDep.getScope());
            }).collect(Collectors.toList());
            request.setManagedDependencies(managedDependencies);
        }
        specs.forEach(spec -> request.addDependency(new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile")));
        RepositorySystemSession tempSession = new RepositorySystemSessionWrapper(this.session, new ConflictResolver(new NearestVersionSelector(), new JavaScopeSelector(), new SimpleOptionalitySelector(), new JavaScopeDeriver()), defaultExcludes);
        CollectResult result = this.system.collectDependencies(tempSession, request);
        PreorderNodeListGenerator gen = new PreorderNodeListGenerator();
        result.getRoot().accept(gen);
        nodes = gen.getNodes();
    } else {
        nodes = new ArrayList<>();
        for (ArtifactSpec spec : specs) {
            Dependency dependency = new Dependency(new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(), spec.type(), spec.version()), "compile");
            DefaultDependencyNode node = new DefaultDependencyNode(dependency);
            nodes.add(node);
        }
    }
    List<DependencyNode> extraDependencies = ExtraArtifactsHandler.getExtraDependencies(nodes);
    nodes.addAll(extraDependencies);
    resolveDependenciesInParallel(nodes);
    return nodes.stream().filter(node -> !"system".equals(node.getDependency().getScope())).map(node -> {
        final Artifact artifact = node.getArtifact();
        return new ArtifactSpec(node.getDependency().getScope(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getExtension(), artifact.getClassifier(), null);
    }).map(this::resolve).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : LocalArtifactRequest(org.eclipse.aether.repository.LocalArtifactRequest) Proxy(org.eclipse.aether.repository.Proxy) Dependency(org.eclipse.aether.graph.Dependency) DependencyManagement(org.apache.maven.model.DependencyManagement) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ArtifactRepositoryPolicy(org.apache.maven.artifact.repository.ArtifactRepositoryPolicy) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) CollectRequest(org.eclipse.aether.collection.CollectRequest) CollectResult(org.eclipse.aether.collection.CollectResult) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) RepositoryPolicy(org.eclipse.aether.repository.RepositoryPolicy) Collection(java.util.Collection) Authentication(org.apache.maven.artifact.repository.Authentication) Artifact(org.eclipse.aether.artifact.Artifact) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) Set(java.util.Set) LocalArtifactResult(org.eclipse.aether.repository.LocalArtifactResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) Collectors(java.util.stream.Collectors) ArtifactResolvingHelper(org.wildfly.swarm.tools.ArtifactResolvingHelper) Objects(java.util.Objects) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) AuthenticationBuilder(org.eclipse.aether.util.repository.AuthenticationBuilder) List(java.util.List) ArtifactResolver(org.eclipse.aether.impl.ArtifactResolver) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) Collections(java.util.Collections) RepositorySystem(org.eclipse.aether.RepositorySystem) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ConflictResolver(org.eclipse.aether.util.graph.transformer.ConflictResolver) CollectResult(org.eclipse.aether.collection.CollectResult) JavaScopeDeriver(org.eclipse.aether.util.graph.transformer.JavaScopeDeriver) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) PreorderNodeListGenerator(org.eclipse.aether.util.graph.visitor.PreorderNodeListGenerator) SimpleOptionalitySelector(org.eclipse.aether.util.graph.transformer.SimpleOptionalitySelector) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) NearestVersionSelector(org.eclipse.aether.util.graph.transformer.NearestVersionSelector) ArtifactSpec(org.wildfly.swarm.tools.ArtifactSpec) JavaScopeSelector(org.eclipse.aether.util.graph.transformer.JavaScopeSelector) DependencyNode(org.eclipse.aether.graph.DependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DefaultDependencyNode(org.eclipse.aether.graph.DefaultDependencyNode) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

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