org.eclipse.aether.resolution.ArtifactDescriptorRequest Java Examples

The following examples show how to use org.eclipse.aether.resolution.ArtifactDescriptorRequest. 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: DeployUtils.java    From vertx-deploy-tools with Apache License 2.0 6 votes vote down vote up
private boolean hasTransitiveSnapshots(Dependency dependency) throws MojoFailureException {
    ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
    descriptorRequest.setArtifact(
            new DefaultArtifact(dependency.getGroupId(), dependency.getArtifactId(), dependency.getClassifier(), dependency.getType(), dependency.getVersion()));
    descriptorRequest.setRepositories(remoteRepos);

    try {
        ArtifactDescriptorResult result = repoSystem.readArtifactDescriptor(repoSession, descriptorRequest);
        Optional<org.eclipse.aether.graph.Dependency> snapshotDependency = result.getDependencies().stream()
                .filter(d -> d.getArtifact().isSnapshot())
                .findFirst();
        return snapshotDependency.isPresent();
    } catch (ArtifactDescriptorException e) {
        throw new MojoFailureException(e.getMessage(), e);
    }
}
 
Example #2
Source File: ExtensionDescriptorMojo.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private CollectRequest newCollectRequest(DefaultArtifact projectArtifact) throws MojoExecutionException {
    final ArtifactDescriptorResult projectDescr;
    try {
        projectDescr = repoSystem.readArtifactDescriptor(repoSession,
                new ArtifactDescriptorRequest()
                        .setArtifact(projectArtifact)
                        .setRepositories(repos));
    } catch (ArtifactDescriptorException e) {
        throw new MojoExecutionException("Failed to read descriptor of " + projectArtifact, e);
    }

    final CollectRequest request = new CollectRequest().setRootArtifact(projectArtifact)
            .setRepositories(repos)
            .setManagedDependencies(projectDescr.getManagedDependencies());
    for (Dependency dep : projectDescr.getDependencies()) {
        if ("test".equals(dep.getScope())
                || "provided".equals(dep.getScope())
                || dep.isOptional()) {
            continue;
        }
        request.addDependency(dep);
    }
    return request;
}
 
Example #3
Source File: BootstrapMavenContext.java    From quarkus with Apache License 2.0 6 votes vote down vote up
private List<RemoteRepository> resolveCurrentProjectRepos(List<RemoteRepository> repos)
        throws BootstrapMavenException {
    final Model model = loadCurrentProjectModel();
    if (model == null) {
        return repos;
    }
    final Artifact projectArtifact = new DefaultArtifact(ModelUtils.getGroupId(model), model.getArtifactId(), "", "pom",
            ModelUtils.getVersion(model));
    final List<RemoteRepository> rawRepos;
    try {
        rawRepos = getRepositorySystem()
                .readArtifactDescriptor(getRepositorySystemSession(), new ArtifactDescriptorRequest()
                        .setArtifact(projectArtifact)
                        .setRepositories(repos))
                .getRepositories();
    } catch (ArtifactDescriptorException e) {
        throw new BootstrapMavenException("Failed to read artifact descriptor for " + projectArtifact, e);
    }
    return getRepositorySystem().newResolutionRepositories(getRepositorySystemSession(), rawRepos);
}
 
Example #4
Source File: GithubImporter.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private List<String> getMavenParentDependencies(String parent)
		throws DependencyResolutionException, ArtifactDescriptorException {
	List<String> dependencies = new ArrayList<>();
	DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
	RepositorySystem system = newRepositorySystem(locator);
	RepositorySystemSession session = newSession(system);

	RemoteRepository central = new RemoteRepository.Builder("central", "default", "http://repo1.maven.org/maven2/")
			.build();

	org.eclipse.aether.artifact.Artifact artifact = new DefaultArtifact(parent);
	ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(artifact, Arrays.asList(central), null);
	try {
		ArtifactDescriptorResult result = system.readArtifactDescriptor(session, request);
		for (org.eclipse.aether.graph.Dependency dependency : result.getManagedDependencies()) {
			dependencies.add(dependency.getArtifact().getGroupId() + ":" + dependency.getArtifact().getGroupId());
		}
	} catch (Exception e) {
		logger.error(e.getMessage());
	}
	return dependencies;

}
 
Example #5
Source File: ExternalBomResolver.java    From sundrio with Apache License 2.0 6 votes vote down vote up
private Map<Artifact, Dependency> resolveDependencies(BomImport bom) throws Exception {
    org.eclipse.aether.artifact.Artifact artifact = new org.eclipse.aether.artifact.DefaultArtifact(bom.getGroupId(), bom.getArtifactId(), "pom", bom.getVersion());

    List<RemoteRepository> repositories = remoteRepositories;
    if (bom.getRepository() != null) {
        // Include the additional repository into the copy
        repositories = new LinkedList<RemoteRepository>(repositories);
        RemoteRepository repo = new RemoteRepository.Builder(bom.getArtifactId() + "-repository", "default", bom.getRepository()).build();
        repositories.add(0, repo);
    }

    ArtifactRequest artifactRequest = new ArtifactRequest(artifact, repositories, null);
    system.resolveArtifact(session, artifactRequest); // To get an error when the artifact does not exist

    ArtifactDescriptorRequest req = new ArtifactDescriptorRequest(artifact, repositories, null);
    ArtifactDescriptorResult res = system.readArtifactDescriptor(session, req);

    Map<Artifact, Dependency> mavenDependencies = new LinkedHashMap<Artifact, Dependency>();
    if (res.getManagedDependencies() != null) {
        for (org.eclipse.aether.graph.Dependency dep : res.getManagedDependencies()) {
            mavenDependencies.put(toMavenArtifact(dep), toMavenDependency(dep));
        }
    }

    return mavenDependencies;
}
 
Example #6
Source File: Resolver.java    From buck with Apache License 2.0 5 votes vote down vote up
private List<Dependency> getDependenciesOf(Artifact dep) throws ArtifactDescriptorException {
  ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest();
  descriptorRequest.setArtifact(dep);
  descriptorRequest.setRepositories(repos);
  descriptorRequest.setRequestContext(JavaScopes.RUNTIME);

  ArtifactDescriptorResult result = repoSys.readArtifactDescriptor(session, descriptorRequest);
  return result.getDependencies();
}
 
Example #7
Source File: DependencyResolver.java    From start.spring.io with Apache License 2.0 5 votes vote down vote up
private List<org.eclipse.aether.graph.Dependency> resolveManagedDependencies(String groupId, String artifactId,
		String version, List<RemoteRepository> repositories) {
	try {
		return this.repositorySystem
				.readArtifactDescriptor(this.repositorySystemSession, new ArtifactDescriptorRequest(
						new DefaultArtifact(groupId, artifactId, "pom", version), repositories, null))
				.getManagedDependencies();
	}
	catch (ArtifactDescriptorException ex) {
		throw new RuntimeException(ex);
	}
}
 
Example #8
Source File: MavenResolverDependencyManagementVersionResolver.java    From initializr with Apache License 2.0 5 votes vote down vote up
private ArtifactDescriptorResult resolveBom(String groupId, String artifactId, String version) {
	synchronized (this.monitor) {
		try {
			return this.repositorySystem.readArtifactDescriptor(this.repositorySystemSession,
					new ArtifactDescriptorRequest(new DefaultArtifact(groupId, artifactId, "pom", version),
							repositories, null));
		}
		catch (ArtifactDescriptorException ex) {
			throw new IllegalStateException(
					"Bom '" + groupId + ":" + artifactId + ":" + version + "' could not be resolved", ex);
		}
	}
}
 
Example #9
Source File: MavenAddonDependencyResolver.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
private ArtifactDescriptorResult readArtifactDescriptor(AddonId addonId) throws ArtifactDescriptorException
{
   String coords = toMavenCoords(addonId);
   RepositorySystem system = container.getRepositorySystem();
   Settings settings = getSettings();
   DefaultRepositorySystemSession session = container.setupRepoSession(system, settings);
   List<RemoteRepository> repositories = MavenRepositories.getRemoteRepositories(container, settings);
   ArtifactDescriptorRequest request = new ArtifactDescriptorRequest();
   request.setArtifact(new DefaultArtifact(coords));
   request.setRepositories(repositories);

   ArtifactDescriptorResult result = system.readArtifactDescriptor(session, request);
   return result;
}
 
Example #10
Source File: MavenPluginLocation.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
private MavenPluginVersion createMavenVersion(Version version) throws ArtifactDescriptorException, FileNotFoundException, IOException, ArtifactResolutionException, XmlPullParserException {
	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;
	descriptorResult = mavenPluginRepository.getSystem().readArtifactDescriptor(mavenPluginRepository.getSession(), descriptorRequest);
	
	try {
		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 (Exception e) {
		LOGGER.error(e.getMessage());
	}
	
	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;
}
 
Example #11
Source File: Cadfael.java    From Poseidon with Apache License 2.0 5 votes vote down vote up
public Set<Dependency> getAllDependencies(Artifact artifact) throws ArtifactDescriptorException {
    ArtifactDescriptorRequest request = new ArtifactDescriptorRequest(artifact, repositories, null);
    ArtifactDescriptorResult result = system.readArtifactDescriptor(session, request);

    Set<Dependency> dependencies = new HashSet<>(result.getManagedDependencies());
    dependencies.addAll(result.getDependencies());

    return dependencies;
}
 
Example #12
Source File: DependencyResolver.java    From google-cloud-eclipse with Apache License 2.0 5 votes vote down vote up
private static Collection<Dependency> _getManagedDependencies(
    IMavenExecutionContext context,
    RepositorySystem system,
    String groupId,
    String artifactId,
    String version,
    SubMonitor monitor)
    throws CoreException {
  ArtifactDescriptorRequest request = new ArtifactDescriptorRequest();
  String coords = groupId + ":" + artifactId + ":" + version;
  Artifact artifact = new DefaultArtifact(coords);
  request.setArtifact(artifact);
  request.setRepositories(centralRepository(system));

  // ensure checksum errors result in failure
  DefaultRepositorySystemSession session =
      new DefaultRepositorySystemSession(context.getRepositorySession());
  session.setChecksumPolicy(RepositoryPolicy.CHECKSUM_POLICY_FAIL);

  try {
    List<Dependency> managedDependencies =
        system.readArtifactDescriptor(session, request).getManagedDependencies();
    
    return managedDependencies;
  } catch (RepositoryException ex) {
    IStatus status = StatusUtil.error(DependencyResolver.class, ex.getMessage(), ex);
    throw new CoreException(status);
  }
}
 
Example #13
Source File: RepackageExtensionMojo.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private List<Dependency> obtainBomDependencies(final String urlLocation) {
    final Artifact artifact = downloadAndInstallArtifact(urlLocation).getArtifact();

    final Dependency dependency = new Dependency(artifact, JavaScopes.RUNTIME);

    final List<RemoteRepository> remoteRepositories = project.getRepositories().stream()
        .map(r -> new RemoteRepository.Builder(r.getId(), r.getLayout(), r.getUrl()).build())
        .collect(Collectors.toList());

    CollectResult result;
    try {
        final ArtifactDescriptorRequest descriptorRequest = new ArtifactDescriptorRequest(artifact, remoteRepositories, null);
        final ArtifactDescriptorResult descriptor = repository.readArtifactDescriptor(session, descriptorRequest);

        final List<Dependency> dependencies = Stream.concat(
            descriptor.getDependencies().stream(),
            descriptor.getManagedDependencies().stream())
            .collect(Collectors.toList());

        final DefaultRepositorySystemSession sessionToUse = new DefaultRepositorySystemSession(session);
        sessionToUse.setDependencyGraphTransformer(new NoopDependencyGraphTransformer());

        final CollectRequest request = new CollectRequest(dependency, dependencies, remoteRepositories);
        result = repository.collectDependencies(sessionToUse, request);
    } catch (final DependencyCollectionException | ArtifactDescriptorException e) {
        throw new IllegalStateException("Unabele to obtain BOM dependencies for: " + urlLocation, e);
    }

    final DependencyNode root = result.getRoot();

    final PostorderNodeListGenerator visitor = new PostorderNodeListGenerator();
    root.accept(visitor);

    return visitor.getDependencies(true);
}
 
Example #14
Source File: MavenArtifactResolver.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private ArtifactDescriptorResult resolveDescriptorInternal(final Artifact artifact, List<RemoteRepository> aggregatedRepos)
        throws BootstrapMavenException {
    try {
        return repoSystem.readArtifactDescriptor(repoSession,
                new ArtifactDescriptorRequest()
                        .setArtifact(artifact)
                        .setRepositories(
                                aggregatedRepos));
    } catch (ArtifactDescriptorException e) {
        throw new BootstrapMavenException("Failed to read descriptor of " + artifact, e);
    }
}
 
Example #15
Source File: BotsingMojo.java    From botsing with Apache License 2.0 5 votes vote down vote up
private void downloadArtifactDescriptorFile(DefaultArtifact aetherArtifact) throws MojoExecutionException {

		ArtifactDescriptorRequest descReq = new ArtifactDescriptorRequest().setRepositories(this.repositories).setArtifact(aetherArtifact);
		try {
			this.repoSystem.readArtifactDescriptor(this.repoSession, descReq);

		} catch ( ArtifactDescriptorException e) {
			throw new MojoExecutionException("Artifact Descriptor for " + aetherArtifact.getArtifactId() + " could not be resolved.", e);
		}
	}
 
Example #16
Source File: Bom.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
/**
 * Parses the dependencyManagement section of an artifact and returns
 * the artifacts included there.
 *
 * @param mavenRepositoryUrls URLs of Maven repositories to search for BOM members
 */
public static Bom readBom(String coordinates, List<String> mavenRepositoryUrls)
    throws ArtifactDescriptorException {
  Artifact artifact = new DefaultArtifact(coordinates);

  RepositorySystem system = RepositoryUtility.newRepositorySystem();
  RepositorySystemSession session = RepositoryUtility.newSession(system);

  ArtifactDescriptorRequest request = new ArtifactDescriptorRequest();

  for (String repositoryUrl : mavenRepositoryUrls) {
    request.addRepository(RepositoryUtility.mavenRepositoryFromUrl(repositoryUrl));
  }

  request.setArtifact(artifact);

  ArtifactDescriptorResult resolved = system.readArtifactDescriptor(session, request);
  List<Exception> exceptions = resolved.getExceptions();
  if (!exceptions.isEmpty()) {
    throw new ArtifactDescriptorException(resolved, exceptions.get(0).getMessage());
  }
  
  List<Artifact> managedDependencies = new ArrayList<>();
  for (Dependency dependency : resolved.getManagedDependencies()) {
    Artifact managed = dependency.getArtifact();
    if (!shouldSkipBomMember(managed) && !managedDependencies.contains(managed)) {
      managedDependencies.add(managed);
    }
  }
  
  Bom bom = new Bom(coordinates, ImmutableList.copyOf(managedDependencies));
  return bom;
}
 
Example #17
Source File: ManagedDependencyLister.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws ArtifactDescriptorException {
  DefaultArtifact artifact =
      new DefaultArtifact("com.google.cloud:libraries-bom:pom:1.0.0");

  RepositorySystemSession session = RepositoryUtility.newSession(system);

  ArtifactDescriptorRequest request = new ArtifactDescriptorRequest();
  request.addRepository(RepositoryUtility.CENTRAL);
  request.setArtifact(artifact);

  ArtifactDescriptorResult resolved = system.readArtifactDescriptor(session, request);
  for (Dependency dependency : resolved.getManagedDependencies()) {
    System.out.println(dependency);
  }
}