Java Code Examples for org.apache.maven.artifact.resolver.ArtifactResolutionResult#getArtifacts()

The following examples show how to use org.apache.maven.artifact.resolver.ArtifactResolutionResult#getArtifacts() . 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: MavenHelper.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Resolve the artifacts with the given key.
 *
 * @param groupId the group identifier.
 * @param artifactId the artifact identifier.
 * @return the discovered artifacts.
 * @throws MojoExecutionException if resolution cannot be done.
 * @since 0.8
 */
public Set<Artifact> resolve(String groupId, String artifactId) throws MojoExecutionException {
	final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
	request.setResolveRoot(true);
	request.setResolveTransitively(true);
	request.setLocalRepository(getSession().getLocalRepository());
	request.setRemoteRepositories(getSession().getCurrentProject().getRemoteArtifactRepositories());
	request.setOffline(getSession().isOffline());
	request.setForceUpdate(getSession().getRequest().isUpdateSnapshots());
	request.setServers(getSession().getRequest().getServers());
	request.setMirrors(getSession().getRequest().getMirrors());
	request.setProxies(getSession().getRequest().getProxies());
	request.setArtifact(createArtifact(groupId, artifactId));

	final ArtifactResolutionResult result = resolve(request);

	return result.getArtifacts();
}
 
Example 2
Source File: Util.java    From yangtools with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Read transitive dependencies of given plugin and store them in map.
 *
 * @param plugin
 *            plugin to read
 * @param map
 *            map, where founded transitive dependencies will be stored
 * @param repoSystem
 *            repository system
 * @param localRepository
 *            local repository
 * @param remoteRepos
 *            list of remote repositories
 */
private static void getPluginTransitiveDependencies(final Plugin plugin,
        final Map<Artifact, Collection<Artifact>> map, final RepositorySystem repoSystem,
        final ArtifactRepository localRepository, final List<ArtifactRepository> remoteRepos) {

    List<Dependency> pluginDependencies = plugin.getDependencies();
    for (Dependency dep : pluginDependencies) {
        Artifact artifact = repoSystem.createDependencyArtifact(dep);

        ArtifactResolutionRequest request = new ArtifactResolutionRequest();
        request.setArtifact(artifact);
        request.setResolveTransitively(true);
        request.setLocalRepository(localRepository);
        request.setRemoteRepositories(remoteRepos);

        ArtifactResolutionResult result = repoSystem.resolve(request);
        Set<Artifact> pluginDependencyDependencies = result.getArtifacts();
        map.put(artifact, pluginDependencyDependencies);
    }
}
 
Example 3
Source File: GenerateBomMojo.java    From sundrio with Apache License 2.0 5 votes vote down vote up
private Set<Artifact> resolve(final Set<Artifact> dependencies) {
    ArtifactResolutionRequest request = new ArtifactResolutionRequest();
    request.setArtifact(getProject().getArtifact());
    request.setArtifactDependencies(dependencies);
    request.setLocalRepository(localRepository);
    request.setRemoteRepositories(remoteRepositories);
    request.setManagedVersionMap(getProject().getManagedVersionMap());
    request.setResolveTransitively(true);
    ArtifactResolutionResult result = artifactResolver.resolve(request);
    return result.getArtifacts();
}
 
Example 4
Source File: ExtensionClassLoaderFactory.java    From nifi-maven with Apache License 2.0 5 votes vote down vote up
private Set<URL> toURLs(final Artifact artifact) throws MojoExecutionException {
    final Set<URL> urls = new HashSet<>();

    final File artifactFile = artifact.getFile();
    if (artifactFile == null) {
        getLog().debug("Attempting to resolve Artifact " + artifact + " because it has no File associated with it");

        final ArtifactResolutionRequest request = new ArtifactResolutionRequest();
        request.setLocalRepository(localRepo);
        request.setArtifact(artifact);

        final ArtifactResolutionResult result = artifactResolver.resolve(request);
        if (!result.isSuccess()) {
            throw new MojoExecutionException("Could not resolve local dependency " + artifact);
        }

        getLog().debug("Resolved Artifact " + artifact + " to " + result.getArtifacts());

        for (final Artifact resolved : result.getArtifacts()) {
            urls.addAll(toURLs(resolved));
        }
    } else {
        try {
            final URL url = artifact.getFile().toURI().toURL();
            getLog().debug("Adding URL " + url + " to ClassLoader");
            urls.add(url);
        } catch (final MalformedURLException mue) {
            throw new MojoExecutionException("Failed to convert File " + artifact.getFile() + " into URL", mue);
        }
    }

    return urls;
}
 
Example 5
Source File: ArtifactUtils.java    From maven-jaxb2-plugin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static Collection<Artifact> resolveTransitively(
		final ArtifactFactory artifactFactory,
		final ArtifactResolver artifactResolver,
		final ArtifactRepository localRepository,
		final ArtifactMetadataSource artifactMetadataSource,
		final Dependency[] dependencies, final MavenProject project)
		throws InvalidDependencyVersionException,
		ArtifactResolutionException, ArtifactNotFoundException {
	if (dependencies == null) {
		return Collections.emptyList();
	}

	@SuppressWarnings("unchecked")
	final Set<Artifact> artifacts = MavenMetadataSource.createArtifacts(
			artifactFactory, Arrays.asList(dependencies), "runtime", null,
			project);

	final ArtifactResolutionResult artifactResolutionResult = artifactResolver
			.resolveTransitively(artifacts,

			project.getArtifact(),

			project.getRemoteArtifactRepositories(), localRepository,
					artifactMetadataSource);

	@SuppressWarnings("unchecked")
	final Set<Artifact> resolvedArtifacts = artifactResolutionResult
			.getArtifacts();

	return resolvedArtifacts;
}
 
Example 6
Source File: PreCompileMojo.java    From jprotobuf with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve the executable dependencies for the specified project
 * 
 * @param executablePomArtifact the project's POM
 * @return a set of Artifacts
 * @throws MojoExecutionException if a failure happens
 */
private Set<Artifact> resolveExecutableDependencies( Artifact executablePomArtifact )
    throws MojoExecutionException
{

    Set<Artifact> executableDependencies;
    try
    {
        MavenProject executableProject =
            this.projectBuilder.buildFromRepository( executablePomArtifact, this.remoteRepositories,
                                                     this.localRepository );

        // get all of the dependencies for the executable project
        List<Dependency> dependencies = executableProject.getDependencies();

        // make Artifacts of all the dependencies
        Set<Artifact> dependencyArtifacts =
            MavenMetadataSource.createArtifacts( this.artifactFactory, dependencies, null, null, null );

        // not forgetting the Artifact of the project itself
        dependencyArtifacts.add( executableProject.getArtifact() );

        // resolve all dependencies transitively to obtain a comprehensive list of assemblies
        ArtifactResolutionResult result =
            artifactResolver.resolveTransitively( dependencyArtifacts, executablePomArtifact,
                                                  Collections.emptyMap(), this.localRepository,
                                                  this.remoteRepositories, metadataSource, null,
                                                  Collections.emptyList() );
        executableDependencies = result.getArtifacts();
    }
    catch ( Exception ex )
    {
        throw new MojoExecutionException( "Encountered problems resolving dependencies of the executable "
            + "in preparation for its execution.", ex );
    }

    return executableDependencies;
}