Search in sources :

Example 6 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project mule by mulesoft.

the class DependencyResolver method readArtifactDescriptor.

/**
 * Gets information about an artifact like its direct dependencies and potential relocations.
 *
 * @param artifact the {@link Artifact} requested, must not be {@code null}
 * @param remoteRepositories to be used for resolving the artifact in addition to the ones already defined in context.
 * @return {@link ArtifactDescriptorResult} descriptor result, never {@code null}
 * @throws {@link ArtifactDescriptorException} if the artifact descriptor could not be read
 */
public ArtifactDescriptorResult readArtifactDescriptor(Artifact artifact, List<RemoteRepository> remoteRepositories) throws ArtifactDescriptorException {
    checkNotNull(artifact, "artifact cannot be null");
    final ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(artifact, resolutionContext.getRemoteRepositories(), null);
    // Has to set authentication to these remote repositories as they may come from a pom descriptor
    remoteRepositories.forEach(remoteRepository -> {
        RemoteRepository authenticatedRemoteRepository = setAuthentication(remoteRepository);
        if (!request.getRepositories().contains(authenticatedRemoteRepository)) {
            request.addRepository(authenticatedRemoteRepository);
        }
    });
    return repositoryState.getSystem().readArtifactDescriptor(repositoryState.getSession(), request);
}
Also used : RemoteRepository(org.eclipse.aether.repository.RemoteRepository) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest)

Example 7 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project google-cloud-intellij by GoogleCloudPlatform.

the class CloudApiMavenService method getManagedDependencyVersion.

/**
 * Finds the version of the passed in {@link CloudLibraryClientMavenCoordinates} that is managed
 * by the given BOM version.
 *
 * @param libraryMavenCoordinates the maven coordinates of the {@link CloudLibrary} for which we
 *     are finding the version
 * @param bomVersion the version of the BOM from which to fetch the library version
 * @return the optional version of the library found in the given BOM
 */
Optional<String> getManagedDependencyVersion(CloudLibraryClientMavenCoordinates libraryMavenCoordinates, String bomVersion) {
    Artifact bomArtifact = new DefaultArtifact(toBomCoordinates(bomVersion));
    ArtifactDescriptorRequest request = new ArtifactDescriptorRequest();
    request.setArtifact(bomArtifact);
    request.addRepository(MAVEN_CENTRAL_REPOSITORY);
    try {
        ArtifactDescriptorResult result = SYSTEM.readArtifactDescriptor(SESSION, request);
        return result.getManagedDependencies().stream().filter(dependency -> {
            Artifact artifact = dependency.getArtifact();
            String coordinatesFromBom = toFormattedMavenCoordinates(libraryMavenCoordinates.getGroupId(), libraryMavenCoordinates.getArtifactId());
            String libraryCoordinates = toFormattedMavenCoordinates(artifact.getGroupId(), artifact.getArtifactId());
            return coordinatesFromBom.equalsIgnoreCase(libraryCoordinates);
        }).findFirst().map(dependency -> dependency.getArtifact().getVersion());
    } catch (ArtifactDescriptorException e) {
        logger.warn("Error fetching version of client library from bom version " + bomVersion);
        return Optional.empty();
    }
}
Also used : ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest)

Example 8 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project camel by apache.

the class BOMResolver method retrieveUpstreamBOMVersions.

private void retrieveUpstreamBOMVersions() throws Exception {
    RepositorySystem system = newRepositorySystem();
    DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
    LocalRepository localRepo = new LocalRepository(LOCAL_REPO);
    session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
    String camelVersion = DependencyResolver.resolveCamelParentProperty("${project.version}");
    List<Artifact> neededArtifacts = new LinkedList<>();
    neededArtifacts.add(new DefaultArtifact("org.apache.camel:camel:pom:" + camelVersion).setFile(camelRoot("pom.xml")));
    neededArtifacts.add(new DefaultArtifact("org.apache.camel:camel-parent:pom:" + camelVersion).setFile(camelRoot("parent/pom.xml")));
    neededArtifacts.add(new DefaultArtifact("org.apache.camel:spring-boot:pom:" + camelVersion).setFile(camelRoot("platforms/spring-boot/pom.xml")));
    neededArtifacts.add(new DefaultArtifact("org.apache.camel:camel-spring-boot-dm:pom:" + camelVersion).setFile(camelRoot("platforms/spring-boot/spring-boot-dm/pom.xml")));
    neededArtifacts.add(new DefaultArtifact("org.apache.camel:camel-spring-boot-dependencies:pom:" + camelVersion).setFile(camelRoot("platforms/spring-boot/spring-boot-dm/camel-spring-boot-dependencies/pom.xml")));
    Artifact camelSpringBootParent = new DefaultArtifact("org.apache.camel:camel-starter-parent:pom:" + camelVersion).setFile(camelRoot("platforms/spring-boot/spring-boot-dm/camel-starter-parent/pom.xml"));
    neededArtifacts.add(camelSpringBootParent);
    RemoteRepository localRepoDist = new RemoteRepository.Builder("org.apache.camel.itest.springboot", "default", new File(LOCAL_REPO).toURI().toString()).build();
    for (Artifact artifact : neededArtifacts) {
        DeployRequest deployRequest = new DeployRequest();
        deployRequest.addArtifact(artifact);
        deployRequest.setRepository(localRepoDist);
        system.deploy(session, deployRequest);
    }
    RemoteRepository mavenCentral = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/").build();
    ArtifactDescriptorRequest dReq = new ArtifactDescriptorRequest(camelSpringBootParent, Arrays.asList(localRepoDist, mavenCentral), null);
    ArtifactDescriptorResult dRes = system.readArtifactDescriptor(session, dReq);
    this.versions = new TreeMap<>();
    for (Dependency dependency : dRes.getManagedDependencies()) {
        Artifact a = dependency.getArtifact();
        String key = a.getGroupId() + ":" + a.getArtifactId();
        versions.put(key, dependency.getArtifact().getVersion());
    }
}
Also used : DeployRequest(org.eclipse.aether.deployment.DeployRequest) LocalRepository(org.eclipse.aether.repository.LocalRepository) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) Dependency(org.eclipse.aether.graph.Dependency) LinkedList(java.util.LinkedList) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) RepositorySystem(org.eclipse.aether.RepositorySystem) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) File(java.io.File) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest)

Example 9 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project BIMserver by opensourceBIM.

the class MavenPluginLocation method getAllVersions.

@Override
public List<MavenPluginVersion> getAllVersions() {
    List<MavenPluginVersion> pluginVersions = new ArrayList<>();
    Artifact artifact = new DefaultArtifact(groupId, artifactId, null, "[0,)");
    VersionRangeRequest rangeRequest = new VersionRangeRequest();
    rangeRequest.setArtifact(artifact);
    rangeRequest.setRepositories(mavenPluginRepository.getRepositoriesAsList());
    // RemoteRepository centralRepo = newCentralRepository();
    try {
        VersionRangeResult rangeResult = mavenPluginRepository.getSystem().resolveVersionRange(mavenPluginRepository.getSession(), rangeRequest);
        List<Version> versions = rangeResult.getVersions();
        if (!versions.isEmpty()) {
            for (int i = versions.size() - 1; i >= Math.max(0, versions.size() - 3); i--) {
                Version version = versions.get(i);
                ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
                Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "pom", version.toString());
                descriptorRequest.setArtifact(versionArtifact);
                descriptorRequest.setRepositories(mavenPluginRepository.getRepositoriesAsList());
                MavenPluginVersion mavenPluginVersion = new MavenPluginVersion(versionArtifact, version);
                ArtifactDescriptorResult descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
                ArtifactRequest request = new ArtifactRequest();
                request.setArtifact(descriptorResult.getArtifact());
                request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
                ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
                File pomFile = resolveArtifact.getArtifact().getFile();
                MavenXpp3Reader mavenreader = new MavenXpp3Reader();
                try (FileReader fileReader = new FileReader(pomFile)) {
                    Model model = mavenreader.read(fileReader);
                    mavenPluginVersion.setModel(model);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }
                for (org.eclipse.aether.graph.Dependency dependency : descriptorResult.getDependencies()) {
                    DefaultArtifactVersion artifactVersion = new DefaultArtifactVersion(dependency.getArtifact().getVersion());
                    mavenPluginVersion.addDependency(new MavenDependency(dependency.getArtifact(), artifactVersion));
                }
                pluginVersions.add(0, mavenPluginVersion);
            }
        }
    } catch (VersionRangeResolutionException e) {
        e.printStackTrace();
    } catch (ArtifactDescriptorException e) {
        e.printStackTrace();
    } catch (ArtifactResolutionException e) {
        e.printStackTrace();
    }
    return pluginVersions;
}
Also used : ArrayList(java.util.ArrayList) FileNotFoundException(java.io.FileNotFoundException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) Version(org.eclipse.aether.version.Version) SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) Model(org.apache.maven.model.Model) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) File(java.io.File) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 10 with ArtifactDescriptorRequest

use of org.eclipse.aether.resolution.ArtifactDescriptorRequest in project BIMserver by opensourceBIM.

the class MavenPluginLocation method getLatestVersion.

public MavenPluginVersion getLatestVersion() {
    Artifact artifact = new DefaultArtifact(groupId + ":" + artifactId + ":LATEST");
    VersionRangeRequest rangeRequest = new VersionRangeRequest();
    rangeRequest.setArtifact(artifact);
    try {
        VersionRangeResult rangeResult = mavenPluginRepository.getSystem().resolveVersionRange(mavenPluginRepository.getSession(), rangeRequest);
        List<Version> versions = rangeResult.getVersions();
        if (!versions.isEmpty()) {
            Version version = versions.get(0);
            ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
            Artifact versionArtifact = new DefaultArtifact(groupId + ":" + artifactId + ":pom:" + version.toString());
            descriptorRequest.setArtifact(versionArtifact);
            MavenPluginVersion mavenPluginVersion = new MavenPluginVersion(versionArtifact, version);
            ArtifactDescriptorResult descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
            ArtifactRequest request = new ArtifactRequest();
            request.setArtifact(descriptorResult.getArtifact());
            ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
            File pomFile = resolveArtifact.getArtifact().getFile();
            MavenXpp3Reader mavenreader = new MavenXpp3Reader();
            try (FileReader fileReader = new FileReader(pomFile)) {
                try {
                    Model model = mavenreader.read(fileReader);
                    mavenPluginVersion.setModel(model);
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            for (org.eclipse.aether.graph.Dependency dependency : descriptorResult.getDependencies()) {
                DefaultArtifactVersion artifactVersion = new DefaultArtifactVersion(dependency.getArtifact().getVersion());
                mavenPluginVersion.addDependency(new MavenDependency(dependency.getArtifact(), artifactVersion));
            }
            return mavenPluginVersion;
        }
    } catch (VersionRangeResolutionException e) {
        e.printStackTrace();
    } catch (ArtifactDescriptorException e) {
        e.printStackTrace();
    } catch (ArtifactResolutionException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) Version(org.eclipse.aether.version.Version) SPluginBundleVersion(org.bimserver.interfaces.objects.SPluginBundleVersion) VersionRangeRequest(org.eclipse.aether.resolution.VersionRangeRequest) FileReader(java.io.FileReader) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArtifactDescriptorRequest(org.eclipse.aether.resolution.ArtifactDescriptorRequest) VersionRangeResolutionException(org.eclipse.aether.resolution.VersionRangeResolutionException) VersionRangeResult(org.eclipse.aether.resolution.VersionRangeResult) DefaultArtifactVersion(org.apache.maven.artifact.versioning.DefaultArtifactVersion) MavenXpp3Reader(org.apache.maven.model.io.xpp3.MavenXpp3Reader) IOException(java.io.IOException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) Model(org.apache.maven.model.Model) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) File(java.io.File) ArtifactDescriptorException(org.eclipse.aether.resolution.ArtifactDescriptorException) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Aggregations

ArtifactDescriptorRequest (org.eclipse.aether.resolution.ArtifactDescriptorRequest)11 ArtifactDescriptorResult (org.eclipse.aether.resolution.ArtifactDescriptorResult)9 Artifact (org.eclipse.aether.artifact.Artifact)7 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)7 IOException (java.io.IOException)6 ArtifactDescriptorException (org.eclipse.aether.resolution.ArtifactDescriptorException)6 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)6 FileNotFoundException (java.io.FileNotFoundException)5 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)5 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)5 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)5 File (java.io.File)4 Dependency (org.eclipse.aether.graph.Dependency)4 VersionRangeResolutionException (org.eclipse.aether.resolution.VersionRangeResolutionException)4 FileReader (java.io.FileReader)3 DefaultArtifactVersion (org.apache.maven.artifact.versioning.DefaultArtifactVersion)3 Model (org.apache.maven.model.Model)3 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)3 DependencyCollectionException (org.eclipse.aether.collection.DependencyCollectionException)3 DependencyNode (org.eclipse.aether.graph.DependencyNode)3