Search in sources :

Example 21 with RepositorySystem

use of org.eclipse.aether.RepositorySystem in project embulk by embulk.

the class MavenArtifactFinder method create.

public static MavenArtifactFinder create(final Path localMavenRepositoryPath) throws MavenRepositoryNotFoundException {
    final Path absolutePath;
    try {
        absolutePath = localMavenRepositoryPath.normalize().toAbsolutePath();
    } catch (IOError ex) {
        throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, ex);
    } catch (SecurityException ex) {
        throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, ex);
    }
    if (!Files.exists(absolutePath)) {
        throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, absolutePath, new NoSuchFileException(absolutePath.toString()));
    }
    if (!Files.isDirectory(absolutePath)) {
        throw new MavenRepositoryNotFoundException(localMavenRepositoryPath, absolutePath, new NotDirectoryException(absolutePath.toString()));
    }
    final RepositorySystem repositorySystem = createRepositorySystem();
    return new MavenArtifactFinder(localMavenRepositoryPath, absolutePath, repositorySystem, createRepositorySystemSession(repositorySystem, absolutePath));
}
Also used : Path(java.nio.file.Path) RepositorySystem(org.eclipse.aether.RepositorySystem) NotDirectoryException(java.nio.file.NotDirectoryException) IOError(java.io.IOError) NoSuchFileException(java.nio.file.NoSuchFileException)

Example 22 with RepositorySystem

use of org.eclipse.aether.RepositorySystem in project liferay-ide by liferay.

the class AetherUtil method getLatestAvailableArtifact.

public static Artifact getLatestAvailableArtifact(String gavCoords) {
    Artifact retval = null;
    RepositorySystem system = newRepositorySystem();
    RepositorySystemSession session = newRepositorySystemSession(system);
    String latestVersion = getLatestVersion(gavCoords, system, session);
    String[] gav = gavCoords.split(":");
    Artifact defaultArtifact = new DefaultArtifact(gav[0] + ":" + gav[1] + ":" + latestVersion);
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(defaultArtifact);
    artifactRequest.addRepository(newCentralRepository());
    try {
        ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
        retval = artifactResult.getArtifact();
    } catch (ArtifactResolutionException e) {
        LiferayMavenCore.logError("Unable to get latest Liferay archetype", e);
        artifactRequest.setArtifact(new DefaultArtifact(gavCoords));
        try {
            retval = system.resolveArtifact(session, artifactRequest).getArtifact();
        } catch (ArtifactResolutionException e1) {
            LiferayMavenCore.logError("Unable to get default Liferay archetype", e1);
        }
    }
    if (retval == null) {
        retval = defaultArtifact;
    }
    return retval;
}
Also used : RepositorySystem(org.eclipse.aether.RepositorySystem) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 23 with RepositorySystem

use of org.eclipse.aether.RepositorySystem 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 24 with RepositorySystem

use of org.eclipse.aether.RepositorySystem in project kie-wb-common by kiegroup.

the class MavenDependencyConfigExecutorTest method installArtifactLocally.

private void installArtifactLocally(final String groupId, final String artifactId, final String version) throws Exception {
    Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "pom", version);
    final Path pom = getPom(groupId, artifactId, version);
    pomArtifact = pomArtifact.setFile(pom.toFile());
    final InstallRequest installRequest = new InstallRequest();
    installRequest.addArtifact(pomArtifact);
    final DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
    RepositorySystem system = locator.getService(RepositorySystem.class);
    final DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
    final LocalRepository localRepo = new LocalRepository(m2Folder);
    session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
    system.install(session, installRequest);
}
Also used : Path(java.nio.file.Path) RepositorySystem(org.eclipse.aether.RepositorySystem) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) InstallRequest(org.eclipse.aether.installation.InstallRequest) LocalRepository(org.eclipse.aether.repository.LocalRepository) DefaultServiceLocator(org.eclipse.aether.impl.DefaultServiceLocator) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 25 with RepositorySystem

use of org.eclipse.aether.RepositorySystem in project gate-core by GateNLP.

the class SimpleMavenCache method cacheArtifact.

public void cacheArtifact(Artifact artifact) throws IOException, SettingsBuildingException, DependencyCollectionException, DependencyResolutionException, ArtifactResolutionException, ModelBuildingException {
    List<RemoteRepository> repos = getRepositoryList();
    Dependency dependency = new Dependency(artifact, "runtime");
    RepositorySystem repoSystem = getRepositorySystem();
    RepositorySystemSession repoSession = getRepositorySession(repoSystem, null);
    CollectRequest collectRequest = new CollectRequest(dependency, repos);
    DependencyNode node = repoSystem.collectDependencies(repoSession, collectRequest).getRoot();
    DependencyRequest dependencyRequest = new DependencyRequest();
    dependencyRequest.setRoot(node);
    DependencyResult result = repoSystem.resolveDependencies(repoSession, dependencyRequest);
    for (ArtifactResult ar : result.getArtifactResults()) {
        // generate output file name from the *requested* artifact rather
        // than the resolved one (e.g. if we requested a -SNAPSHOT version
        // but got a timestamped one)
        File file = getArtifactFile(ar.getRequest().getArtifact());
        FileUtils.copyFile(ar.getArtifact().getFile(), file);
        // get the POM corresponding to the specific resolved JAR
        Artifact pomArtifact = new SubArtifact(ar.getArtifact(), "", "pom");
        ArtifactRequest artifactRequest = new ArtifactRequest(pomArtifact, repos, null);
        ArtifactResult artifactResult = repoSystem.resolveArtifact(repoSession, artifactRequest);
        // but copy it to a file named for the original requested version number
        file = getArtifactFile(new SubArtifact(ar.getRequest().getArtifact(), "", "pom"));
        FileUtils.copyFile(artifactResult.getArtifact().getFile(), file);
        cacheParents(artifactResult.getArtifact().getFile(), repoSystem, repoSession, repos);
    }
}
Also used : RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) DependencyResult(org.eclipse.aether.resolution.DependencyResult) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Dependency(org.eclipse.aether.graph.Dependency) CollectRequest(org.eclipse.aether.collection.CollectRequest) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) Utils.getRepositorySystem(gate.util.maven.Utils.getRepositorySystem) RepositorySystem(org.eclipse.aether.RepositorySystem) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DependencyRequest(org.eclipse.aether.resolution.DependencyRequest) DependencyNode(org.eclipse.aether.graph.DependencyNode) SubArtifact(org.eclipse.aether.util.artifact.SubArtifact) File(java.io.File)

Aggregations

RepositorySystem (org.eclipse.aether.RepositorySystem)27 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)18 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)12 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)9 Artifact (org.eclipse.aether.artifact.Artifact)8 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)8 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)7 DefaultServiceLocator (org.eclipse.aether.impl.DefaultServiceLocator)6 File (java.io.File)5 ArrayList (java.util.ArrayList)5 LocalRepository (org.eclipse.aether.repository.LocalRepository)5 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)4 IOException (java.io.IOException)3 CollectRequest (org.eclipse.aether.collection.CollectRequest)3 Dependency (org.eclipse.aether.graph.Dependency)3 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)3 DependencyRequest (org.eclipse.aether.resolution.DependencyRequest)3 URL (java.net.URL)2 Path (java.nio.file.Path)2 List (java.util.List)2