Search in sources :

Example 21 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testRepositoryEventsOnVersionResolverFail.

@Test
public void testRepositoryEventsOnVersionResolverFail() {
    resolver.setVersionResolver(new VersionResolver() {

        public VersionResult resolveVersion(RepositorySystemSession session, VersionRequest request) throws VersionResolutionException {
            throw new VersionResolutionException(new VersionResult(request));
        }
    });
    RecordingRepositoryListener listener = new RecordingRepositoryListener();
    session.setRepositoryListener(listener);
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    try {
        resolver.resolveArtifact(session, request);
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
    }
    List<EventWrapper> events = listener.getEvents();
    assertEquals(2, events.size());
    EventWrapper event = events.get(0);
    assertEquals(artifact, event.getEvent().getArtifact());
    assertEquals(Type.ARTIFACT_RESOLVING, event.getType());
    event = events.get(1);
    assertEquals(artifact, event.getEvent().getArtifact());
    assertEquals(Type.ARTIFACT_RESOLVED, event.getType());
    assertNotNull(event.getEvent().getException());
    assertEquals(1, event.getEvent().getExceptions().size());
}
Also used : RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) TestRepositorySystemSession(org.sonatype.aether.test.impl.TestRepositorySystemSession) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) VersionResolver(org.sonatype.aether.impl.VersionResolver) VersionResult(org.sonatype.aether.resolution.VersionResult) VersionRequest(org.sonatype.aether.resolution.VersionRequest) VersionResolutionException(org.sonatype.aether.resolution.VersionResolutionException) RecordingRepositoryListener(org.sonatype.aether.test.impl.RecordingRepositoryListener) EventWrapper(org.sonatype.aether.test.impl.RecordingRepositoryListener.EventWrapper) Test(org.junit.Test)

Example 22 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project sonatype-aether by sonatype.

the class ResolveTransitiveDependencies method main.

public static void main(String[] args) throws Exception {
    System.out.println("------------------------------------------------------------");
    System.out.println(ResolveTransitiveDependencies.class.getSimpleName());
    RepositorySystem system = Booter.newRepositorySystem();
    RepositorySystemSession session = Booter.newRepositorySystemSession(system);
    Artifact artifact = new DefaultArtifact("org.sonatype.aether:aether-impl:1.9");
    RemoteRepository repo = Booter.newCentralRepository();
    DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, JavaScopes.COMPILE));
    collectRequest.addRepository(repo);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);
    List<ArtifactResult> artifactResults = system.resolveDependencies(session, dependencyRequest).getArtifactResults();
    for (ArtifactResult artifactResult : artifactResults) {
        System.out.println(artifactResult.getArtifact() + " resolved to " + artifactResult.getArtifact().getFile());
    }
}
Also used : RepositorySystem(org.sonatype.aether.RepositorySystem) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) DependencyRequest(org.sonatype.aether.resolution.DependencyRequest) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) DependencyFilter(org.sonatype.aether.graph.DependencyFilter) Dependency(org.sonatype.aether.graph.Dependency) CollectRequest(org.sonatype.aether.collection.CollectRequest) Artifact(org.sonatype.aether.artifact.Artifact) DefaultArtifact(org.sonatype.aether.util.artifact.DefaultArtifact) DefaultArtifact(org.sonatype.aether.util.artifact.DefaultArtifact) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult)

Example 23 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project sonatype-aether by sonatype.

the class Aether method resolve.

public AetherResult resolve(String groupId, String artifactId, String version) throws DependencyResolutionException {
    RepositorySystemSession session = newSession();
    Dependency dependency = new Dependency(new DefaultArtifact(groupId, artifactId, "", "jar", version), "runtime");
    RemoteRepository central = new RemoteRepository("central", "default", remoteRepository);
    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(dependency);
    collectRequest.addRepository(central);
    DependencyRequest dependencyRequest = new DependencyRequest();
    dependencyRequest.setCollectRequest(collectRequest);
    DependencyNode rootNode = repositorySystem.resolveDependencies(session, dependencyRequest).getRoot();
    StringBuilder dump = new StringBuilder();
    displayTree(rootNode, dump);
    PreorderNodeListGenerator nlg = new PreorderNodeListGenerator();
    rootNode.accept(nlg);
    return new AetherResult(rootNode, nlg.getFiles(), nlg.getClassPath());
}
Also used : MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) DependencyRequest(org.sonatype.aether.resolution.DependencyRequest) DependencyNode(org.sonatype.aether.graph.DependencyNode) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) Dependency(org.sonatype.aether.graph.Dependency) CollectRequest(org.sonatype.aether.collection.CollectRequest) PreorderNodeListGenerator(org.sonatype.aether.util.graph.PreorderNodeListGenerator) DefaultArtifact(org.sonatype.aether.util.artifact.DefaultArtifact)

Example 24 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project sonatype-aether by sonatype.

the class DefaultMetadataResolverTest method testFavorLocal.

@Test
public void testFavorLocal() throws IOException {
    lrm.add(session, new LocalMetadataRegistration(metadata));
    String path = session.getLocalRepositoryManager().getPathForLocalMetadata(metadata);
    File file = new File(session.getLocalRepository().getBasedir(), path);
    TestFileUtils.write(file.getAbsolutePath(), file);
    final long timestamp = file.lastModified();
    MetadataRequest request = new MetadataRequest(metadata, repository, "");
    request.setFavorLocalRepository(true);
    resolver.setUpdateCheckManager(new StaticUpdateCheckManager(false) {

        @Override
        public boolean isUpdatedRequired(RepositorySystemSession session, long lastModified, String policy) {
            assertEquals(timestamp, lastModified);
            return super.isUpdatedRequired(session, lastModified, policy);
        }
    });
    List<MetadataResult> results = resolver.resolveMetadata(session, Arrays.asList(request));
    assertEquals(1, results.size());
    MetadataResult result = results.get(0);
    assertSame(request, result.getRequest());
    assertNull(String.valueOf(result.getException()), result.getException());
    connector.assertSeenExpected();
}
Also used : RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) TestRepositorySystemSession(org.sonatype.aether.test.impl.TestRepositorySystemSession) MetadataRequest(org.sonatype.aether.resolution.MetadataRequest) LocalMetadataRegistration(org.sonatype.aether.repository.LocalMetadataRegistration) File(java.io.File) MetadataResult(org.sonatype.aether.resolution.MetadataResult) Test(org.junit.Test)

Example 25 with RepositorySystemSession

use of org.sonatype.aether.RepositorySystemSession in project intellij-community by JetBrains.

the class Maven30ServerEmbedderImpl method doResolveProject.

@NotNull
public Collection<MavenExecutionResult> doResolveProject(@NotNull final Collection<File> files, @NotNull final List<String> activeProfiles, @NotNull final List<String> inactiveProfiles, final List<ResolutionListener> listeners) throws RemoteException {
    final File file = files.size() == 1 ? files.iterator().next() : null;
    final MavenExecutionRequest request = createRequest(file, activeProfiles, inactiveProfiles, Collections.<String>emptyList());
    request.setUpdateSnapshots(myAlwaysUpdateSnapshots);
    final Collection<MavenExecutionResult> executionResults = ContainerUtil.newArrayList();
    executeWithMavenSession(request, new Runnable() {

        @Override
        public void run() {
            try {
                RepositorySystemSession repositorySession = getComponent(LegacySupport.class).getRepositorySession();
                if (repositorySession instanceof DefaultRepositorySystemSession) {
                    ((DefaultRepositorySystemSession) repositorySession).setTransferListener(new Maven30TransferListenerAdapter(myCurrentIndicator));
                    if (myWorkspaceMap != null) {
                        ((DefaultRepositorySystemSession) repositorySession).setWorkspaceReader(new Maven30WorkspaceReader(myWorkspaceMap));
                    }
                }
                List<ProjectBuildingResult> buildingResults = getProjectBuildingResults(request, files);
                for (ProjectBuildingResult buildingResult : buildingResults) {
                    MavenProject project = buildingResult.getProject();
                    if (project == null) {
                        List<Exception> exceptions = new ArrayList<Exception>();
                        for (ModelProblem problem : buildingResult.getProblems()) {
                            exceptions.add(problem.getException());
                        }
                        MavenExecutionResult mavenExecutionResult = new MavenExecutionResult(buildingResult.getPomFile(), exceptions);
                        executionResults.add(mavenExecutionResult);
                        continue;
                    }
                    List<Exception> exceptions = new ArrayList<Exception>();
                    loadExtensions(project, exceptions);
                    //Artifact projectArtifact = project.getArtifact();
                    //Map managedVersions = project.getManagedVersionMap();
                    //ArtifactMetadataSource metadataSource = getComponent(ArtifactMetadataSource.class);
                    project.setDependencyArtifacts(project.createArtifacts(getComponent(ArtifactFactory.class), null, null));
                    if (USE_MVN2_COMPATIBLE_DEPENDENCY_RESOLVING) {
                        addMvn2CompatResults(project, exceptions, listeners, myLocalRepository, executionResults);
                    } else {
                        final DependencyResolutionResult dependencyResolutionResult = resolveDependencies(project, repositorySession);
                        final List<Dependency> dependencies = dependencyResolutionResult.getDependencies();
                        Set<Artifact> artifacts = new LinkedHashSet<Artifact>(dependencies.size());
                        for (Dependency dependency : dependencies) {
                            final Artifact artifact = RepositoryUtils.toArtifact(dependency.getArtifact());
                            artifact.setScope(dependency.getScope());
                            artifact.setOptional(dependency.isOptional());
                            artifacts.add(artifact);
                            resolveAsModule(artifact);
                        }
                        project.setArtifacts(artifacts);
                        executionResults.add(new MavenExecutionResult(project, dependencyResolutionResult, exceptions));
                    }
                }
            } catch (Exception e) {
                executionResults.add(handleException(e));
            }
        }
    });
    return executionResults;
}
Also used : DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) THashSet(gnu.trove.THashSet) MavenExecutionResult(org.jetbrains.idea.maven.server.embedder.MavenExecutionResult) Dependency(org.sonatype.aether.graph.Dependency) InitializationException(org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException) ModelInterpolationException(org.apache.maven.project.interpolation.ModelInterpolationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RemoteException(java.rmi.RemoteException) SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ContextException(org.codehaus.plexus.context.ContextException) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) Artifact(org.apache.maven.artifact.Artifact) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) DefaultRepositorySystemSession(org.sonatype.aether.util.DefaultRepositorySystemSession) ModelProblem(org.apache.maven.model.building.ModelProblem) File(java.io.File) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)25 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)15 Artifact (org.sonatype.aether.artifact.Artifact)13 DefaultArtifact (org.sonatype.aether.util.artifact.DefaultArtifact)10 RepositorySystem (org.sonatype.aether.RepositorySystem)9 Test (org.junit.Test)8 Dependency (org.sonatype.aether.graph.Dependency)7 TestRepositorySystemSession (org.sonatype.aether.test.impl.TestRepositorySystemSession)7 ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)6 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)6 File (java.io.File)5 LocalArtifactRequest (org.sonatype.aether.repository.LocalArtifactRequest)5 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)5 CollectRequest (org.sonatype.aether.collection.CollectRequest)4 VersionResolver (org.sonatype.aether.impl.VersionResolver)4 LocalArtifactResult (org.sonatype.aether.repository.LocalArtifactResult)4 LocalMetadataRegistration (org.sonatype.aether.repository.LocalMetadataRegistration)4 VersionRequest (org.sonatype.aether.resolution.VersionRequest)4 VersionResolutionException (org.sonatype.aether.resolution.VersionResolutionException)4 VersionResult (org.sonatype.aether.resolution.VersionResult)4