Java Code Examples for org.eclipse.aether.collection.CollectRequest#setRequestContext()

The following examples show how to use org.eclipse.aether.collection.CollectRequest#setRequestContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DependencyDownloader.java    From go-offline-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Download a plugin, all of its transitive dependencies and dependencies declared on the plugin declaration.
 * <p>
 * Dependencies and plugin artifacts that refer to an artifact in the current reactor build are ignored.
 * Transitive dependencies that are marked as optional are ignored
 * Transitive dependencies with the scopes "test", "system" and "provided" are ignored.
 *
 * @param plugin the plugin to download
 */
public Set<ArtifactWithRepoType> resolvePlugin(Plugin plugin) {
    Artifact pluginArtifact = toArtifact(plugin);
    Dependency pluginDependency = new Dependency(pluginArtifact, null);
    CollectRequest collectRequest = new CollectRequest(pluginDependency, pluginRepositories);
    collectRequest.setRequestContext(RepositoryType.PLUGIN.getRequestContext());

    List<Dependency> pluginDependencies = new ArrayList<>();
    for (org.apache.maven.model.Dependency d : plugin.getDependencies()) {
        Dependency dependency = RepositoryUtils.toDependency(d, typeRegistry);
        pluginDependencies.add(dependency);
    }
    collectRequest.setDependencies(pluginDependencies);

    try {
        CollectResult collectResult = repositorySystem.collectDependencies(pluginSession, collectRequest);
        return getArtifactsFromCollectResult(collectResult, RepositoryType.PLUGIN);
    } catch (DependencyCollectionException | RuntimeException e) {
        log.error("Error resolving plugin " + plugin.getGroupId() + ":" + plugin.getArtifactId());
        handleRepositoryException(e);
    }
    return Collections.emptySet();
}
 
Example 2
Source File: Resolver.java    From buck with Apache License 2.0 6 votes vote down vote up
private ImmutableMap<String, Artifact> getRunTimeTransitiveDeps(Iterable<Dependency> mavenCoords)
    throws RepositoryException {

  CollectRequest collectRequest = new CollectRequest();
  collectRequest.setRequestContext(JavaScopes.RUNTIME);
  collectRequest.setRepositories(repos);

  for (Dependency dep : mavenCoords) {
    collectRequest.addDependency(dep);
  }

  DependencyFilter filter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
  DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, filter);

  DependencyResult dependencyResult = repoSys.resolveDependencies(session, dependencyRequest);

  ImmutableSortedMap.Builder<String, Artifact> knownDeps = ImmutableSortedMap.naturalOrder();
  for (ArtifactResult artifactResult : dependencyResult.getArtifactResults()) {
    Artifact node = artifactResult.getArtifact();
    knownDeps.put(buildKey(node), node);
  }
  return knownDeps.build();
}
 
Example 3
Source File: DependencyDownloader.java    From go-offline-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Download a single dependency and all of its transitive dependencies that is needed by the build without appearing in any dependency tree
 * <p>
 * Dependencies and plugin artifacts that refer to an artifact in the current reactor build are ignored.
 * Transitive dependencies that are marked as optional are ignored
 * Transitive dependencies with the scopes "test", "system" and "provided" are ignored.
 *
 * @param dynamicDependency the dependency to download
 */
public Set<ArtifactWithRepoType> resolveDynamicDependency(DynamicDependency dynamicDependency) {
    DefaultArtifact artifact = new DefaultArtifact(dynamicDependency.getGroupId(), dynamicDependency.getArtifactId(), dynamicDependency.getClassifier(), "jar", dynamicDependency.getVersion());

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot(new Dependency(artifact, null));
    RepositoryType repositoryType = dynamicDependency.getRepositoryType();
    RepositorySystemSession session;
    switch (repositoryType) {
        case MAIN:
            session = remoteSession;
            collectRequest.setRepositories(remoteRepositories);
            collectRequest.setRequestContext(repositoryType.getRequestContext());
            break;
        case PLUGIN:
            session = pluginSession;
            collectRequest.setRepositories(pluginRepositories);
            collectRequest.setRequestContext(repositoryType.getRequestContext());
            break;
        default:
            throw new IllegalStateException("Unknown enum val " + repositoryType);

    }
    try {
        CollectResult collectResult = repositorySystem.collectDependencies(session, collectRequest);
        return getArtifactsFromCollectResult(collectResult, repositoryType);
    } catch (DependencyCollectionException | RuntimeException e) {
        log.error("Error resolving dynamic dependency" + dynamicDependency.getGroupId() + ":" + dynamicDependency.getArtifactId());
        handleRepositoryException(e);
    }
    return Collections.emptySet();
}
 
Example 4
Source File: Maven3DependencyTreeBuilder.java    From archiva with Apache License 2.0 5 votes vote down vote up
private void resolve( ResolveRequest resolveRequest )
{

    RepositorySystem system = mavenSystemManager.getRepositorySystem();
    RepositorySystemSession session = MavenSystemManager.newRepositorySystemSession( resolveRequest.localRepoDir );

    org.eclipse.aether.artifact.Artifact artifact = new DefaultArtifact(
        resolveRequest.groupId + ":" + resolveRequest.artifactId + ":" + resolveRequest.version );

    CollectRequest collectRequest = new CollectRequest();
    collectRequest.setRoot( new Dependency( artifact, "" ) );

    // add remote repositories
    for ( RemoteRepository remoteRepository : resolveRequest.remoteRepositories )
    {
        org.eclipse.aether.repository.RemoteRepository repo = new org.eclipse.aether.repository.RemoteRepository.Builder( remoteRepository.getId( ), "default", remoteRepository.getLocation( ).toString() ).build( );
        collectRequest.addRepository(repo);
    }
    collectRequest.setRequestContext( "project" );

    //collectRequest.addRepository( repo );

    try
    {
        CollectResult collectResult = system.collectDependencies( session, collectRequest );
        collectResult.getRoot().accept( resolveRequest.dependencyVisitor );
        log.debug("Collected dependency results for resolve");
    }
    catch ( DependencyCollectionException e )
    {
        log.error( "Error while collecting dependencies (resolve): {}", e.getMessage(), e );
    }



}