Search in sources :

Example 16 with DefaultRepositorySystemSession

use of org.eclipse.aether.DefaultRepositorySystemSession in project bnd by bndtools.

the class AetherRepository method versions.

@Override
public SortedSet<Version> versions(String bsn) throws Exception {
    init();
    // Use the index by preference
    if (indexedRepo != null)
        return indexedRepo.versions(ConversionUtils.maybeMavenCoordsToBsn(bsn));
    Artifact artifact = null;
    try {
        artifact = new DefaultArtifact(bsn + ":[0,)");
    } catch (Exception e) {
    // ignore non-GAV style dependencies
    }
    if (artifact == null)
        return null;
    // Setup the Aether repo session and create the range request
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
    session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
    VersionRangeRequest rangeRequest = new VersionRangeRequest();
    rangeRequest.setArtifact(artifact);
    rangeRequest.setRepositories(Collections.singletonList(remoteRepo));
    // Resolve the range
    VersionRangeResult rangeResult = repoSystem.resolveVersionRange(session, rangeRequest);
    // Add to the result
    SortedSet<Version> versions = new TreeSet<Version>();
    for (org.eclipse.aether.version.Version version : rangeResult.getVersions()) {
        try {
            versions.add(MavenVersion.parseString(version.toString()).getOSGiVersion());
        } catch (IllegalArgumentException e) {
        // ignore version
        }
    }
    return versions;
}
Also used : VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) URISyntaxException(java.net.URISyntaxException) TransferCancelledException(org.eclipse.aether.transfer.TransferCancelledException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) Version(aQute.bnd.version.Version) MavenVersion(aQute.bnd.version.MavenVersion) TreeSet(java.util.TreeSet) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 17 with DefaultRepositorySystemSession

use of org.eclipse.aether.DefaultRepositorySystemSession in project bnd by bndtools.

the class RemotePostProcessor method postProcessRelease.

private ArtifactResult postProcessRelease(ArtifactRequest request, Artifact artifact) throws MojoExecutionException {
    for (RemoteRepository repository : request.getRepositories()) {
        if (!repository.getPolicy(false).isEnabled()) {
            // Skip the repo if it isn't enabled for releases
            continue;
        }
        // Remove the workspace from the session so that we don't use it
        DefaultRepositorySystemSession newSession = new DefaultRepositorySystemSession(session);
        newSession.setWorkspaceReader(null);
        // Find the snapshot metadata for the module
        MetadataRequest mr = new MetadataRequest().setRepository(repository).setMetadata(new DefaultMetadata(artifact.getGroupId(), artifact.getArtifactId(), null, "maven-metadata.xml", RELEASE));
        for (MetadataResult metadataResult : system.resolveMetadata(newSession, singletonList(mr))) {
            if (metadataResult.isResolved()) {
                try {
                    Metadata read = metadataReader.read(metadataResult.getMetadata().getFile(), null);
                    Versioning versioning = read.getVersioning();
                    if (versioning == null || versioning.getVersions() == null || versioning.getVersions().isEmpty()) {
                        continue;
                    } else if (versioning.getVersions().contains(artifact.getVersion())) {
                        ArtifactResult result = system.resolveArtifact(newSession, new ArtifactRequest().setArtifact(artifact).addRepository(repository));
                        if (result.isResolved()) {
                            File toUse = new File(session.getLocalRepository().getBasedir(), session.getLocalRepositoryManager().getPathForLocalArtifact(artifact));
                            if (!toUse.exists()) {
                                logger.warn("The resolved artifact {} does not exist at {}", artifact, toUse);
                                continue;
                            } else {
                                logger.debug("Located snapshot file {} for artifact {}", toUse, artifact);
                            }
                            result.getArtifact().setFile(toUse);
                            return result;
                        }
                    }
                } catch (Exception e) {
                    throw new MojoExecutionException("Unable to read project metadata for " + artifact, e);
                }
            }
        }
    }
    logger.debug("Unable to resolve a remote repository containing {}", artifact);
    return null;
}
Also used : Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MetadataRequest(org.eclipse.aether.resolution.MetadataRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) DefaultMetadata(org.eclipse.aether.metadata.DefaultMetadata) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) File(java.io.File) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MetadataResult(org.eclipse.aether.resolution.MetadataResult) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult)

Example 18 with DefaultRepositorySystemSession

use of org.eclipse.aether.DefaultRepositorySystemSession in project meecrowave by apache.

the class MeecrowaveRunMojoTest method run.

@Test
public void run() throws Exception {
    final File moduleBase = jarLocation(MeecrowaveRunMojoTest.class).getParentFile().getParentFile();
    final File basedir = new File(moduleBase, "src/test/resources/" + getClass().getSimpleName());
    final File pom = new File(basedir, "pom.xml");
    final MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request.setBaseDirectory(basedir);
    final ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
    final DefaultRepositorySystemSession repositorySession = new DefaultRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(repositorySession, new LocalRepository(new File(moduleBase, "target/fake"), "")));
    configuration.setRepositorySession(repositorySession);
    final MavenProject project = mojo.lookup(ProjectBuilder.class).build(pom, configuration).getProject();
    final MavenSession session = mojo.newMavenSession(project);
    final int port;
    try (final ServerSocket serverSocket = new ServerSocket(0)) {
        port = serverSocket.getLocalPort();
    }
    final MojoExecution execution = mojo.newMojoExecution("run");
    execution.getConfiguration().addChild(new Xpp3Dom("httpPort") {

        {
            setValue(Integer.toString(port));
        }
    });
    final InputStream in = System.in;
    final CountDownLatch latch = new CountDownLatch(1);
    System.setIn(new InputStream() {

        // just to not return nothing
        private int val = 2;

        @Override
        public int read() throws IOException {
            try {
                latch.await();
            } catch (final InterruptedException e) {
                Thread.interrupted();
                fail(e.getMessage());
            }
            return val--;
        }
    });
    final Thread runner = new Thread() {

        @Override
        public void run() {
            try {
                mojo.executeMojo(session, project, execution);
            } catch (final Exception e) {
                fail(e.getMessage());
            }
        }
    };
    try {
        runner.start();
        for (int i = 0; i < 120; i++) {
            try {
                assertEquals("simple", IOUtils.toString(new URL("http://localhost:" + port + "/api/test")));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("first_name"));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("last_name"));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("firstname"));
                assertTrue(IOUtils.toString(new URL("http://localhost:" + port + "/api/test/model")).contains("null"));
                latch.countDown();
                break;
            } catch (final Exception | AssertionError e) {
                Thread.sleep(500);
            }
        }
    } finally {
        runner.join(TimeUnit.MINUTES.toMillis(1));
        System.setIn(in);
        if (runner.isAlive()) {
            runner.interrupt();
            fail("Runner didn't terminate properly");
        }
    }
}
Also used : Xpp3Dom(org.codehaus.plexus.util.xml.Xpp3Dom) InputStream(java.io.InputStream) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) LocalRepository(org.eclipse.aether.repository.LocalRepository) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) URL(java.net.URL) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) MavenProject(org.apache.maven.project.MavenProject) MojoExecution(org.apache.maven.plugin.MojoExecution) SimpleLocalRepositoryManagerFactory(org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory) File(java.io.File) Test(org.junit.Test)

Aggregations

DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)18 File (java.io.File)7 LocalRepository (org.eclipse.aether.repository.LocalRepository)6 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)6 Artifact (org.eclipse.aether.artifact.Artifact)5 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)5 RepositorySystem (org.eclipse.aether.RepositorySystem)4 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)4 IOException (java.io.IOException)3 DeployRequest (org.eclipse.aether.deployment.DeployRequest)3 Dependency (org.eclipse.aether.graph.Dependency)3 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)3 TransferCancelledException (org.eclipse.aether.transfer.TransferCancelledException)3 MavenVersion (aQute.bnd.version.MavenVersion)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 RemoteException (java.rmi.RemoteException)2 ArrayList (java.util.ArrayList)2 InvalidRepositoryException (org.apache.maven.artifact.InvalidRepositoryException)2