Search in sources :

Example 36 with ArtifactRequest

use of org.eclipse.aether.resolution.ArtifactRequest in project acceptance-test-harness by jenkinsci.

the class ArtifactResolverUtil method resolve.

/**
 * @param artifact The artifact to be resolved
 *
 * @return artifact resolution result
 */
public ArtifactResult resolve(DefaultArtifact artifact) {
    Builder repoBuilder = new RemoteRepository.Builder("repo.jenkins-ci.org", "default", "http://repo.jenkins-ci.org/public/");
    DefaultSettingsBuildingRequest request = new DefaultSettingsBuildingRequest();
    File userHome = new File(System.getProperty("user.home"));
    File userSettingsFile = new File(new File(userHome, ".m2"), "settings.xml");
    request.setUserSettingsFile(userSettingsFile);
    if (userSettingsFile.exists()) {
        LOGGER.debug("Found maven settings file - " + userSettingsFile.getAbsolutePath());
        SettingsBuilder settingsBuilder = new DefaultSettingsBuilderFactory().newInstance();
        try {
            Settings settings = settingsBuilder.build(request).getEffectiveSettings();
            org.apache.maven.settings.Proxy mavenActiveproxy = settings.getActiveProxy();
            if (mavenActiveproxy != null) {
                LOGGER.debug("Found maven proxy settings. Will use for artifact resolution");
                repoBuilder.setProxy(asProxy(mavenActiveproxy));
            } else {
                LOGGER.debug("Did not find an active proxy in maven settings xml file");
            }
        } catch (SettingsBuildingException e) {
            LOGGER.warn("Could not find or load settings.xml to attempt to user proxy settings.", e);
        }
    }
    RemoteRepository repo = repoBuilder.build();
    ArtifactResult r;
    try {
        r = repoSystem.resolveArtifact(repoSystemSession, new ArtifactRequest(artifact, Arrays.asList(repo), null));
    } catch (ArtifactResolutionException e) {
        throw new RuntimeException("Could not resolve " + artifact + " from Maven repository", e);
    }
    LOGGER.debug("Found " + r);
    return r;
}
Also used : SettingsBuildingException(org.apache.maven.settings.building.SettingsBuildingException) DefaultSettingsBuildingRequest(org.apache.maven.settings.building.DefaultSettingsBuildingRequest) SettingsBuilder(org.apache.maven.settings.building.SettingsBuilder) AuthenticationBuilder(org.eclipse.aether.util.repository.AuthenticationBuilder) Builder(org.eclipse.aether.repository.RemoteRepository.Builder) RemoteRepository(org.eclipse.aether.repository.RemoteRepository) DefaultSettingsBuilderFactory(org.apache.maven.settings.building.DefaultSettingsBuilderFactory) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) SettingsBuilder(org.apache.maven.settings.building.SettingsBuilder) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) File(java.io.File) Settings(org.apache.maven.settings.Settings)

Example 37 with ArtifactRequest

use of org.eclipse.aether.resolution.ArtifactRequest 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)

Example 38 with ArtifactRequest

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

the class SimpleModelResolver method resolveModel.

@Override
public ModelSource resolveModel(String groupId, String artifactId, String version) throws UnresolvableModelException {
    Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);
    try {
        ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null);
        pomArtifact = system.resolveArtifact(session, request).getArtifact();
    } catch (org.eclipse.aether.resolution.ArtifactResolutionException ex) {
        throw new UnresolvableModelException(ex.getMessage(), groupId, artifactId, version, ex);
    }
    File pomFile = pomArtifact.getFile();
    return new FileModelSource(pomFile);
}
Also used : ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) FileModelSource(org.apache.maven.model.building.FileModelSource) UnresolvableModelException(org.apache.maven.model.resolution.UnresolvableModelException) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 39 with ArtifactRequest

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

the class DependencyResolver method resolveArtifact.

/**
 * Resolves the path for an artifact.
 *
 * @param artifact the {@link Artifact} requested, must not be {@code null}
 * @param remoteRepositories remote repositories to be used in addition to the one in context
 * @return The resolution result, never {@code null}.
 * @throws {@link ArtifactResolutionException} if the artifact could not be resolved.
 */
public ArtifactResult resolveArtifact(Artifact artifact, List<RemoteRepository> remoteRepositories) throws ArtifactResolutionException {
    checkNotNull(artifact, "artifact cannot be null");
    final ArtifactRequest request = new ArtifactRequest(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().resolveArtifact(repositoryState.getSession(), request);
}
Also used : ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) RemoteRepository(org.eclipse.aether.repository.RemoteRepository)

Example 40 with ArtifactRequest

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

the class AetherClassPathClassifierTestCase method serviceClassification.

@Test
public void serviceClassification() throws Exception {
    this.directDependencies = newArrayList(fooCoreDep, fooToolsArtifactDep, fooTestsSupportDep, derbyDriverDep, guavaDep, fooServiceDep);
    when(dependencyResolver.getDirectDependencies(rootArtifact)).thenReturn(directDependencies);
    ArtifactDescriptorResult artifactDescriptorResult = mock(ArtifactDescriptorResult.class);
    List<Dependency> managedDependencies = newArrayList(guavaDep);
    when(artifactDescriptorResult.getManagedDependencies()).thenReturn(managedDependencies);
    when(dependencyResolver.readArtifactDescriptor(any(Artifact.class), any(List.class))).thenReturn(artifactDescriptorResult);
    when(dependencyResolver.resolveArtifact(any(Artifact.class), any(List.class))).thenAnswer(invocation -> {
        Artifact artifact = (Artifact) invocation.getArguments()[0];
        artifact = artifact.setFile(temporaryFolder.newFile());
        return new ArtifactResult(new ArtifactRequest(artifact, null, null)).setArtifact(artifact);
    });
    when(artifactClassificationTypeResolver.resolveArtifactClassificationType(rootArtifact)).thenReturn(APPLICATION);
    File fooServiceArtifactFile = temporaryFolder.newFolder();
    File metaInfFolder = new File(fooServiceArtifactFile, META_INF);
    metaInfFolder.delete();
    metaInfFolder.mkdir();
    File descriptor = new File(metaInfFolder, MULE_ARTIFACT_JSON_DESCRIPTOR);
    MuleServiceModel muleServiceModel = new MuleServiceModelBuilder().setName("Foo").setMinMuleVersion("4.0.0").withServiceProviderClassName("org.foo.ServiceProvider").withClassLoaderModelDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap())).withBundleDescriptorLoader(new MuleArtifactLoaderDescriptor(MULE_LOADER_ID, emptyMap())).build();
    String model = new MuleServiceModelJsonSerializer().serialize(muleServiceModel);
    FileUtils.write(descriptor, model);
    List<File> fooServiceUrls = newArrayList(fooServiceArtifactFile);
    when(dependencyResolver.resolveDependencies(argThat(equalTo(new Dependency(fooServiceDep.getArtifact(), COMPILE))), argThat(equalTo(emptyList())), argThat(equalTo(emptyList())), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())))).thenReturn(fooServiceUrls);
    File rootArtifactFile = temporaryFolder.newFile();
    Artifact jarRootArtifact = rootArtifact.setFile(rootArtifactFile);
    ArtifactResult rootArtifactResult = mock(ArtifactResult.class);
    when(rootArtifactResult.getArtifact()).thenReturn(jarRootArtifact);
    when(dependencyResolver.resolveArtifact(argThat(new ArtifactMatcher(rootArtifact.getGroupId(), rootArtifact.getArtifactId())))).thenReturn(rootArtifactResult);
    File serviceArtifactFile = temporaryFolder.newFile();
    ArtifactResult serviceArtifactResult = mock(ArtifactResult.class);
    Artifact jarServiceArtifact = serviceArtifact.setFile(serviceArtifactFile);
    when(serviceArtifactResult.getArtifact()).thenReturn(jarServiceArtifact);
    when(dependencyResolver.resolveArtifact(argThat(new ArtifactMatcher(serviceArtifact.getGroupId(), serviceArtifact.getArtifactId())))).thenReturn(serviceArtifactResult);
    ArtifactDescriptorResult defaultArtifactDescriptorResult = noManagedDependencies();
    Dependency compileMuleCoreDep = fooCoreDep.setScope(COMPILE);
    Dependency compileMuleArtifactDep = fooToolsArtifactDep.setScope(COMPILE);
    File fooCoreArtifactFile = temporaryFolder.newFile();
    File fooToolsArtifactFile = temporaryFolder.newFile();
    when(dependencyResolver.resolveDependencies(argThat(nullValue(Dependency.class)), (List<Dependency>) and((argThat(hasItems(equalTo(compileMuleCoreDep), equalTo(compileMuleArtifactDep)))), argThat(hasSize(3))), (List<Dependency>) argThat(hasItems(equalTo(guavaDep))), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())))).thenReturn(newArrayList(fooCoreArtifactFile, fooToolsArtifactFile));
    ArtifactsUrlClassification classification = classifier.classify(context);
    assertThat(classification.getTestRunnerLibUrls(), hasSize(1));
    assertThat(classification.getTestRunnerLibUrls(), hasItem(rootArtifactFile.toURI().toURL()));
    assertThat(classification.getPluginUrlClassifications(), is(empty()));
    assertThat(classification.getApplicationSharedLibUrls(), is(empty()));
    assertThat(classification.getContainerUrls(), hasSize(2));
    assertThat(classification.getServiceUrlClassifications(), hasSize(1));
    assertThat(classification.getServiceUrlClassifications().get(0).getUrls(), hasItem(fooServiceArtifactFile.toURI().toURL()));
    verify(defaultArtifactDescriptorResult, atLeastOnce()).getManagedDependencies();
    verify(dependencyResolver, atLeastOnce()).readArtifactDescriptor(any(Artifact.class), anyObject());
    verify(dependencyResolver, atLeastOnce()).resolveDependencies(argThat(equalTo(new Dependency(fooServiceDep.getArtifact(), COMPILE))), argThat(equalTo(emptyList())), argThat(equalTo(emptyList())), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())));
    verify(artifactClassificationTypeResolver).resolveArtifactClassificationType(rootArtifact);
    verify(dependencyResolver, atLeastOnce()).resolveDependencies(argThat(nullValue(Dependency.class)), (List<Dependency>) and((argThat(hasItems(equalTo(compileMuleCoreDep), equalTo(compileMuleArtifactDep)))), argThat(hasSize(3))), (List<Dependency>) argThat(hasItems(equalTo(guavaDep))), argThat(instanceOf(DependencyFilter.class)), argThat(equalTo(emptyList())));
}
Also used : MuleArtifactLoaderDescriptor(org.mule.runtime.api.deployment.meta.MuleArtifactLoaderDescriptor) Dependency(org.eclipse.aether.graph.Dependency) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) MuleServiceModelBuilder(org.mule.runtime.api.deployment.meta.MuleServiceModel.MuleServiceModelBuilder) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) MuleServiceModelJsonSerializer(org.mule.runtime.api.deployment.persistence.MuleServiceModelJsonSerializer) ArtifactDescriptorResult(org.eclipse.aether.resolution.ArtifactDescriptorResult) File(java.io.File) MuleServiceModel(org.mule.runtime.api.deployment.meta.MuleServiceModel) SmallTest(org.mule.tck.size.SmallTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)45 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)41 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)27 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)26 Artifact (org.eclipse.aether.artifact.Artifact)20 File (java.io.File)18 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)9 RemoteRepository (org.eclipse.aether.repository.RemoteRepository)9 IOException (java.io.IOException)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 ArrayList (java.util.ArrayList)7 RepositorySystemSession (org.eclipse.aether.RepositorySystemSession)7 FileNotFoundException (java.io.FileNotFoundException)5 Model (org.apache.maven.model.Model)4 FileReader (java.io.FileReader)3 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)3 MavenXpp3Reader (org.apache.maven.model.io.xpp3.MavenXpp3Reader)3 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)3 RepositorySystem (org.eclipse.aether.RepositorySystem)3 Dependency (org.eclipse.aether.graph.Dependency)3