Search in sources :

Example 1 with ScopeDependencySelector

use of org.revapi.maven.utils.ScopeDependencySelector 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 2 with ScopeDependencySelector

use of org.revapi.maven.utils.ScopeDependencySelector in project revapi by revapi.

the class Main method convertGavs.

private static ArchivesAndSupplementaryArchives convertGavs(String[] gavs, String errorMessagePrefix, File localRepo) {
    RepositorySystem repositorySystem = MavenBootstrap.newRepositorySystem();
    DefaultRepositorySystemSession session = MavenBootstrap.newRepositorySystemSession(repositorySystem, localRepo);
    session.setDependencySelector(new ScopeDependencySelector("compile", "provided"));
    session.setDependencyTraverser(new ScopeDependencyTraverser("compile", "provided"));
    RemoteRepository mavenCentral = new RemoteRepository.Builder("@@forced-maven-central@@", "default", "http://repo.maven.apache.org/maven2/").build();
    List<RemoteRepository> remoteRepositories = singletonList(mavenCentral);
    ArtifactResolver resolver = new ArtifactResolver(repositorySystem, session, remoteRepositories);
    List<FileArchive> archives = new ArrayList<>();
    List<FileArchive> supplementaryArchives = new ArrayList<>();
    for (String gav : gavs) {
        try {
            archives.add(new FileArchive(resolver.resolveArtifact(gav).getFile()));
            ArtifactResolver.CollectionResult res = resolver.collectTransitiveDeps(gav);
            res.getResolvedArtifacts().forEach(a -> supplementaryArchives.add(new FileArchive(a.getFile())));
            if (!res.getFailures().isEmpty()) {
                LOG.warn("Failed to resolve some transitive dependencies: " + res.getFailures().toString());
            }
        } catch (RepositoryException e) {
            throw new IllegalArgumentException(errorMessagePrefix + " " + e.getMessage());
        }
    }
    return new ArchivesAndSupplementaryArchives(archives, supplementaryArchives);
}
Also used : ArrayList(java.util.ArrayList) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) RepositoryException(org.eclipse.aether.RepositoryException) ArtifactResolver(org.revapi.maven.utils.ArtifactResolver) ScopeDependencyTraverser(org.revapi.maven.utils.ScopeDependencyTraverser) RepositorySystem(org.eclipse.aether.RepositorySystem) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) FileArchive(org.revapi.simple.FileArchive) ScopeDependencySelector(org.revapi.maven.utils.ScopeDependencySelector)

Aggregations

DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)2 ArtifactResolver (org.revapi.maven.utils.ArtifactResolver)2 ScopeDependencySelector (org.revapi.maven.utils.ScopeDependencySelector)2 ScopeDependencyTraverser (org.revapi.maven.utils.ScopeDependencyTraverser)2 File (java.io.File)1 MessageFormat (java.text.MessageFormat)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Locale (java.util.Locale)1 Map (java.util.Map)1 ResourceBundle (java.util.ResourceBundle)1 Function (java.util.function.Function)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Sink (org.apache.maven.doxia.sink.Sink)1 MavenSession (org.apache.maven.execution.MavenSession)1 Plugin (org.apache.maven.model.Plugin)1