org.eclipse.aether.resolution.ArtifactResult Java Examples

The following examples show how to use org.eclipse.aether.resolution.ArtifactResult. 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: AbstractVertxMojo.java    From vertx-maven-plugin with Apache License 2.0 7 votes vote down vote up
/**
 * this method resolves maven artifact from all configured repositories using the maven coordinates
 *
 * @param artifact - the maven coordinates of the artifact
 * @return {@link Optional} {@link File} pointing to the resolved artifact in local repository
 */
protected Optional<File> resolveArtifact(String artifact) {
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(new org.eclipse.aether.artifact.DefaultArtifact(artifact));
    try {
        ArtifactResult artifactResult = repositorySystem.resolveArtifact(repositorySystemSession, artifactRequest);
        if (artifactResult.isResolved()) {
            getLog().debug("Resolved :" + artifactResult.getArtifact().getArtifactId());
            return Optional.of(artifactResult.getArtifact().getFile());
        } else {
            getLog().error("Unable to resolve:" + artifact);
        }
    } catch (ArtifactResolutionException e) {
        getLog().error("Unable to resolve:" + artifact);
    }

    return Optional.empty();
}
 
Example #2
Source File: AbstractLibertySupport.java    From ci.maven with Apache License 2.0 6 votes vote down vote up
private File resolveArtifactFile(org.eclipse.aether.artifact.Artifact aetherArtifact) throws MojoExecutionException {
    ArtifactRequest req = new ArtifactRequest().setRepositories(this.repositories).setArtifact(aetherArtifact);
    ArtifactResult resolutionResult = null;
    
    try {
        resolutionResult = this.repositorySystem.resolveArtifact(this.repoSession, req);
        if (!resolutionResult.isResolved()) {
            throw new MojoExecutionException("Unable to resolve artifact: " + aetherArtifact.getGroupId() + ":"
                    + aetherArtifact.getArtifactId() + ":" + aetherArtifact.getVersion());
        }
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("Unable to resolve artifact: " + aetherArtifact.getGroupId() + ":"
                + aetherArtifact.getArtifactId() + ":" + aetherArtifact.getVersion(), e);
    }
    
    File artifactFile = resolutionResult.getArtifact().getFile();
    
    return artifactFile;
}
 
Example #3
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 #4
Source File: CommonBehaviorMojo.java    From botsing with Apache License 2.0 6 votes vote down vote up
private File getArtifactFile(DefaultArtifact aetherArtifact) throws MojoExecutionException {

		ArtifactRequest req = new ArtifactRequest().setRepositories(this.repositories).setArtifact(aetherArtifact);
		ArtifactResult resolutionResult;
		try {
			resolutionResult = this.repoSystem.resolveArtifact(this.repoSession, req);

		} catch (ArtifactResolutionException e) {
			throw new MojoExecutionException("Artifact " + aetherArtifact.getArtifactId() + " could not be resolved.",
					e);
		}

		// The file should exists, but we never know.
		File file = resolutionResult.getArtifact().getFile();
		if (file == null || !file.exists()) {
			getLog().warn("Artifact " + aetherArtifact.getArtifactId()
					+ " has no attached file. Its content will not be copied in the target model directory.");
		}

		return file;
	}
 
Example #5
Source File: DependencyGraphBuilder.java    From cloud-opensource-java with Apache License 2.0 6 votes vote down vote up
private DependencyGraph buildDependencyGraph(
    List<DependencyNode> dependencyNodes, DefaultRepositorySystemSession session) {
   
  try {
    DependencyNode node = resolveCompileTimeDependencies(dependencyNodes, session);
    return DependencyGraph.from(node);
  } catch (DependencyResolutionException ex) {
    DependencyResult result = ex.getResult();
    DependencyGraph graph = DependencyGraph.from(result.getRoot());

    for (ArtifactResult artifactResult : result.getArtifactResults()) {
      Artifact resolvedArtifact = artifactResult.getArtifact();

      if (resolvedArtifact == null) {
        Artifact requestedArtifact = artifactResult.getRequest().getArtifact();
        graph.addUnresolvableArtifactProblem(requestedArtifact);
      }
    }
    
    return graph;
  }
}
 
Example #6
Source File: AbstractMavenArtifactRepositoryManager.java    From galleon with Apache License 2.0 6 votes vote down vote up
@Override
public void resolve(MavenArtifact artifact) throws MavenUniverseException {
    if (artifact.isResolved()) {
        throw new MavenUniverseException("Artifact is already resolved");
    }
    final ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getClassifier(),
            artifact.getExtension(), artifact.getVersion()));

    request.setRepositories(getRepositories());

    final ArtifactResult result;
    try {
        result = repoSystem.resolveArtifact(getSession(), request);
    } catch (Exception e) {
        throw new MavenUniverseException(FpMavenErrors.artifactResolution(request.getArtifact().toString()), e);
    }
    if (!result.isResolved()) {
        throw new MavenUniverseException(FpMavenErrors.artifactResolution(request.getArtifact().toString()));
    }
    if (result.isMissing()) {
        throw new MavenUniverseException(FpMavenErrors.artifactMissing(request.getArtifact().toString()));
    }
    artifact.setPath(Paths.get(result.getArtifact().getFile().toURI()));
}
 
Example #7
Source File: DependencyResolver.java    From start.spring.io with Apache License 2.0 6 votes vote down vote up
static List<String> resolveDependencies(String groupId, String artifactId, String version,
		List<BillOfMaterials> boms, List<RemoteRepository> repositories) {
	DependencyResolver instance = instanceForThread.get();
	List<Dependency> managedDependencies = instance.getManagedDependencies(boms, repositories);
	Dependency aetherDependency = new Dependency(new DefaultArtifact(groupId, artifactId, "pom",
			instance.getVersion(groupId, artifactId, version, managedDependencies)), "compile");
	CollectRequest collectRequest = new CollectRequest((org.eclipse.aether.graph.Dependency) null,
			Collections.singletonList(aetherDependency), repositories);
	collectRequest.setManagedDependencies(managedDependencies);
	DependencyRequest dependencyRequest = new DependencyRequest(collectRequest,
			DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE, JavaScopes.RUNTIME));
	try {
		return instance.resolveDependencies(dependencyRequest).getArtifactResults().stream()
				.map(ArtifactResult::getArtifact)
				.map((artifact) -> artifact.getGroupId() + ":" + artifact.getArtifactId())
				.collect(Collectors.toList());
	}
	catch (DependencyResolutionException ex) {
		throw new RuntimeException(ex);
	}
}
 
Example #8
Source File: SingerMojo.java    From component-runtime with Apache License 2.0 6 votes vote down vote up
private Path findMain() {
    final LocalRepositoryManager lrm = repositorySystemSession.getLocalRepositoryManager();
    final Artifact artifact = new DefaultArtifact(GAV.GROUP, "component-kitap", "fatjar", "jar", GAV.VERSION);
    final File location = new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifact));
    if (!location.exists()) {
        final ArtifactRequest artifactRequest =
                new ArtifactRequest().setArtifact(artifact).setRepositories(remoteRepositories);
        try {
            final ArtifactResult result =
                    repositorySystem.resolveArtifact(repositorySystemSession, artifactRequest);
            if (result.isMissing()) {
                throw new IllegalStateException("Can't find " + artifact);
            }
            return result.getArtifact().getFile().toPath();
        } catch (final ArtifactResolutionException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }
    return location.toPath();
}
 
Example #9
Source File: AbstractVertxMojo.java    From vertx-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * this method resolves maven artifact from all configured repositories using the maven coordinates
 *
 * @param artifact - the maven coordinates of the artifact
 * @return {@link Optional} {@link File} pointing to the resolved artifact in local repository
 */
protected Optional<File> resolveArtifact(String artifact) {
    ArtifactRequest artifactRequest = new ArtifactRequest();
    artifactRequest.setArtifact(new org.eclipse.aether.artifact.DefaultArtifact(artifact));
    try {
        ArtifactResult artifactResult = repositorySystem.resolveArtifact(repositorySystemSession, artifactRequest);
        if (artifactResult.isResolved()) {
            getLog().debug("Resolved :" + artifactResult.getArtifact().getArtifactId());
            return Optional.of(artifactResult.getArtifact().getFile());
        } else {
            getLog().error("Unable to resolve:" + artifact);
        }
    } catch (ArtifactResolutionException e) {
        getLog().error("Unable to resolve:" + artifact);
    }

    return Optional.empty();
}
 
Example #10
Source File: ArtifactCacheLoader.java    From unleash-maven-plugin with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Optional<ArtifactResult> load(ArtifactCoordinates coordinates) throws Exception {
  Artifact artifact = new DefaultArtifact(coordinates.toString());

  ArtifactRequest artifactRequest = new ArtifactRequest();
  artifactRequest.setArtifact(artifact);
  artifactRequest.setRepositories(this.remoteProjectRepos);

  ArtifactResult artifactResult;
  try {
    artifactResult = this.repoSystem.resolveArtifact(this.repoSession, artifactRequest);
  } catch (ArtifactResolutionException e) {
    // must not throw the error or log as an error since this is an expected behavior
    artifactResult = null;
  }

  return Optional.fromNullable(artifactResult);
}
 
Example #11
Source File: CachingArtifactResolvingHelper.java    From thorntail with Apache License 2.0 6 votes vote down vote up
private File resolveArtifactFile(ArtifactSpec spec) {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(artifact(spec));

    remoteRepositories.forEach(request::addRepository);

    try {
        ArtifactResult artifactResult = repoSystem.resolveArtifact(session, request);

        return artifactResult.isResolved()
                ? artifactResult.getArtifact().getFile()
                : null;
    } catch (ArtifactResolutionException e) {
        e.printStackTrace();
        return null;
    }
}
 
Example #12
Source File: MavenArtifactResolvingHelper.java    From thorntail with Apache License 2.0 6 votes vote down vote up
@Override
public ArtifactSpec resolve(ArtifactSpec spec) {
    if (spec.file == null) {
        final DefaultArtifact artifact = new DefaultArtifact(spec.groupId(), spec.artifactId(), spec.classifier(),
                typeToExtension(spec.type()), spec.version(), new DefaultArtifactType(spec.type()));

        final LocalArtifactResult localResult = this.session.getLocalRepositoryManager()
                .find(this.session, new LocalArtifactRequest(artifact, this.remoteRepositories, null));
        if (localResult.isAvailable()) {
            spec.file = localResult.getFile();
        } else {
            try {
                final ArtifactResult result = resolver.resolveArtifact(this.session,
                        new ArtifactRequest(artifact, this.remoteRepositories, null));
                if (result.isResolved()) {
                    spec.file = result.getArtifact().getFile();
                }
            } catch (ArtifactResolutionException e) {
                e.printStackTrace();
            }
        }
    }

    return spec.file != null ? spec : null;
}
 
Example #13
Source File: Aether.java    From pro with GNU General Public License v3.0 6 votes vote down vote up
public List<ArtifactDescriptor> download(List<ArtifactInfo> unresolvedArtifacts) throws IOException {
  var repositories = this.remoteRepositories;
  var artifactRequests = unresolvedArtifacts.stream()
      .map(dependency -> {
        ArtifactRequest artifactRequest = new ArtifactRequest();
        artifactRequest.setArtifact(dependency.artifact);
        artifactRequest.setRepositories(repositories);
        return artifactRequest;
      })
      .collect(Collectors.toUnmodifiableList());

  List<ArtifactResult> artifactResults;
  try {
    artifactResults = system.resolveArtifacts(session, artifactRequests);
  } catch (ArtifactResolutionException e) {
    throw new IOException(e);
  }
  
  return artifactResults.stream()
    .map(result -> new ArtifactDescriptor(result.getArtifact()))
    .collect(Collectors.toUnmodifiableList());
}
 
Example #14
Source File: AetherImporter.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Process the actual import request
 * <p>
 * This method takes the import configuration as is and simply tries to
 * import it. Not manipulating the list of coordinates any more
 * </p>
 */
public static Collection<ArtifactResult> processImport ( final Path tmpDir, final ImportConfiguration cfg ) throws ArtifactResolutionException
{
    Objects.requireNonNull ( tmpDir );
    Objects.requireNonNull ( cfg );

    final RepositoryContext ctx = new RepositoryContext ( tmpDir, cfg.getRepositoryUrl () );

    final Collection<ArtifactRequest> requests = new LinkedList<> ();

    for ( final MavenCoordinates coords : cfg.getCoordinates () )
    {
        // main artifact

        final DefaultArtifact main = new DefaultArtifact ( coords.toString () );
        requests.add ( makeRequest ( ctx.getRepositories (), main ) );
    }

    // process

    return ctx.getSystem ().resolveArtifacts ( ctx.getSession (), requests );
}
 
Example #15
Source File: ArtifactHelper.java    From LicenseScout with Apache License 2.0 6 votes vote down vote up
/**
 * Calculates the set of transitive dependencies of the passed artifacts.
 * 
 * @param repositoryParameters
 * @param artifacts
 * @param artifactScope
 * @return a list of File locations where the JARs of the dependencies are located in the local file system
 * @throws DependencyResolutionException
 */
public static List<File> getDependencies(final IRepositoryParameters repositoryParameters,
                                         final List<ArtifactItem> artifacts, final ArtifactScope artifactScope)
        throws DependencyResolutionException {
    final RepositorySystem system = repositoryParameters.getRepositorySystem();
    final RepositorySystemSession session = repositoryParameters.getRepositorySystemSession();
    final DependencyFilter classpathFlter = DependencyFilterUtils.classpathFilter(artifactScope.getScopeValue());
    final Set<File> artifactFiles = new HashSet<>();
    for (final ArtifactItem artifactItem : artifacts) {
        Artifact artifact = createDefaultArtifact(artifactItem);
        final CollectRequest collectRequest = new CollectRequest();
        collectRequest.setRoot(new Dependency(artifact, artifactScope.getScopeValue()));
        collectRequest.setRepositories(repositoryParameters.getRemoteRepositories());
        final DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFlter);
        final DependencyResult dependencyResult = system.resolveDependencies(session, dependencyRequest);
        final List<ArtifactResult> artifactResults = dependencyResult.getArtifactResults();
        for (final ArtifactResult artifactResult : artifactResults) {
            artifactFiles.add(artifactResult.getArtifact().getFile());
        }
    }
    return new ArrayList<>(artifactFiles);
}
 
Example #16
Source File: DependencyFinder.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the specified artifact (using the : separated syntax).
 *
 * @param mojo   the mojo
 * @param coords the coordinates ot the artifact to resolve using the : separated syntax -
 *               {@code <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}, must not be {@code null}
 * @return the artifact's file if it can be revolved. The file is located in the local maven repository.
 * @throws MojoExecutionException if the artifact cannot be resolved
 */
public static File resolve(AbstractWisdomMojo mojo, String coords) throws MojoExecutionException {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(
            new DefaultArtifact(coords));
    request.setRepositories(mojo.remoteRepos);

    mojo.getLog().info("Resolving artifact " + coords +
            " from " + mojo.remoteRepos);

    ArtifactResult result;
    try {
        result = mojo.repoSystem.resolveArtifact(mojo.repoSession, request);
    } catch (ArtifactResolutionException e) {
        mojo.getLog().error("Cannot resolve " + coords);
        throw new MojoExecutionException(e.getMessage(), e);
    }

    mojo.getLog().info("Resolved artifact " + coords + " to " +
            result.getArtifact().getFile() + " from "
            + result.getRepository());

    return result.getArtifact().getFile();
}
 
Example #17
Source File: DependencyFinder.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves the specified artifact (using its GAV, classifier and packaging).
 *
 * @param mojo       the mojo
 * @param groupId    the groupId of the artifact to resolve
 * @param artifactId the artifactId of the artifact to resolve
 * @param version    the version
 * @param type       the type
 * @param classifier the classifier
 * @return the artifact's file if it can be revolved. The file is located in the local maven repository.
 * @throws MojoExecutionException if the artifact cannot be resolved
 */
public static File resolve(AbstractWisdomMojo mojo, String groupId, String artifactId, String version,
                           String type, String classifier) throws MojoExecutionException {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(
            new DefaultArtifact(groupId, artifactId, classifier, type, version));
    request.setRepositories(mojo.remoteRepos);

    mojo.getLog().info("Resolving artifact " + artifactId +
            " from " + mojo.remoteRepos);

    ArtifactResult result;
    try {
        result = mojo.repoSystem.resolveArtifact(mojo.repoSession, request);
    } catch (ArtifactResolutionException e) {
        mojo.getLog().error("Cannot resolve " + groupId + ":" + artifactId + ":" + version + ":" + type);
        throw new MojoExecutionException(e.getMessage(), e);
    }

    mojo.getLog().info("Resolved artifact " + artifactId + " to " +
            result.getArtifact().getFile() + " from "
            + result.getRepository());

    return result.getArtifact().getFile();
}
 
Example #18
Source File: OpenSourceLicenseCheckMojo.java    From license-check with MIT License 6 votes vote down vote up
/**
 * Uses Aether to retrieve an artifact from the repository.
 *
 * @param coordinates as in groupId:artifactId:version
 * @return the located artifact
 */
Artifact retrieveArtifact(final String coordinates)
{

  final ArtifactRequest request = new ArtifactRequest();
  request.setArtifact(new DefaultArtifact(coordinates));
  request.setRepositories(remoteRepos);

  ArtifactResult result = null;
  try {
    result = repoSystem.resolveArtifact(repoSession, request);
  } catch (final ArtifactResolutionException e) {
    getLog().error("Could not resolve parent artifact (" + coordinates + "): " + e.getMessage());
  }

  if (result != null) {
    return RepositoryUtils.toArtifact(result.getArtifact());
  }
  return null;
}
 
Example #19
Source File: DependencyResolver.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public List<File> resolveArtifactsAndDependencies(List<Artifact> artifacts) throws DependencyResolutionException {
    List<Dependency> dependencies = new ArrayList<Dependency>();

    for (Artifact artifact : artifacts) {
        dependencies.add(new Dependency(artifact, JavaScopes.RUNTIME));
    }

    CollectRequest collectRequest = new CollectRequest((Dependency) null, dependencies, repositories);
    DependencyFilter classpathFilter = DependencyFilterUtils.classpathFilter(JavaScopes.RUNTIME);
    DependencyRequest dependencyRequest = new DependencyRequest(collectRequest, classpathFilter);
    DependencyResult result = system.resolveDependencies(session, dependencyRequest);

    List<File> files = new ArrayList<File>();

    for (ArtifactResult artifactResult : result.getArtifactResults()) {
        files.add(artifactResult.getArtifact().getFile());
    }

    return files;
}
 
Example #20
Source File: ResolveMojoTest.java    From roboconf-platform with Apache License 2.0 6 votes vote down vote up
private RepositorySystem newRepositorySystem() {

		RepositorySystem repoSystem = new DefaultRepositorySystem() {
			@Override
			public ArtifactResult resolveArtifact( RepositorySystemSession session, ArtifactRequest request )
			throws ArtifactResolutionException {

				ArtifactResult res = new ArtifactResult( request );
				Artifact mavenArtifact = ResolveMojoTest.this.artifactIdToArtifact.get( request.getArtifact().getArtifactId());
				if( mavenArtifact == null )
					throw new ArtifactResolutionException( new ArrayList<ArtifactResult>( 0 ), "Error in test wrapper and settings." );

				org.eclipse.aether.artifact.DefaultArtifact art =
						new org.eclipse.aether.artifact.DefaultArtifact(
								"groupId", "artifactId", "classifier", "extension", "version",
								null, mavenArtifact.getFile());

				res.setArtifact( art );
				return res;
			}
		};

		return repoSystem;
	}
 
Example #21
Source File: DevServerMojo.java    From yawp with MIT License 6 votes vote down vote up
public String resolveDevServerJar() {
    String version = getYawpVersion();
    List<RemoteRepository> allRepos = ImmutableList.copyOf(Iterables.concat(getProjectRepos()));

    ArtifactRequest request = new ArtifactRequest(new DefaultArtifact(YAWP_GROUP_ID, YAWP_DEVSERVER_ARTIFACT_ID, "jar", version), allRepos,
            null);

    ArtifactResult result;
    try {
        result = repoSystem.resolveArtifact(repoSession, request);
    } catch (ArtifactResolutionException e) {
        throw new RuntimeException("Could not resolve DevServer artifact in Maven.");
    }

    return result.getArtifact().getFile().getPath();
}
 
Example #22
Source File: JAXRSAnalyzerMojo.java    From jaxrs-analyzer-maven-plugin with Apache License 2.0 6 votes vote down vote up
private Path fetchDependency(final String artifactIdentifier) throws MojoExecutionException {
    ArtifactRequest request = new ArtifactRequest();
    final DefaultArtifact artifact = new DefaultArtifact(artifactIdentifier);
    request.setArtifact(artifact);
    request.setRepositories(remoteRepos);

    LogProvider.debug("Resolving artifact " + artifact + " from " + remoteRepos);

    ArtifactResult result;
    try {
        result = repoSystem.resolveArtifact(repoSession, request);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }

    LogProvider.debug("Resolved artifact " + artifact + " to " + result.getArtifact().getFile() + " from " + result.getRepository());
    return result.getArtifact().getFile().toPath();
}
 
Example #23
Source File: RemotePluginLoader.java    From digdag with Apache License 2.0 6 votes vote down vote up
private ClassLoader buildPluginClassLoader(List<ArtifactResult> artifactResults)
{
    ImmutableList.Builder<URL> urls = ImmutableList.builder();
    for (ArtifactResult artifactResult : artifactResults) {
        URL url;
        try {
            url = artifactResult.getArtifact().getFile().toPath().toUri().toURL();
        }
        catch (MalformedURLException ex) {
            throw Throwables.propagate(ex);
        }
        urls.add(url);
    }
    return new PluginClassLoader(urls.build(), RemotePluginLoader.class.getClassLoader(),
            PARENT_FIRST_PACKAGES, PARENT_FIRST_RESOURCES);
}
 
Example #24
Source File: MeecrowaveBundleMojo.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private File resolve(final String group, final String artifact, final String version, final String classifier) {
    final DefaultArtifact art = new DefaultArtifact(group, artifact, classifier, "jar", version);
    final ArtifactRequest artifactRequest = new ArtifactRequest().setArtifact(art).setRepositories(remoteRepositories);

    final LocalRepositoryManager lrm = session.getLocalRepositoryManager();
    art.setFile(new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifactRequest.getArtifact())));

    try {
        final ArtifactResult result = repositorySystem.resolveArtifact(session, artifactRequest);
        if (result.isMissing()) {
            throw new IllegalStateException("Can't find commons-cli, please add it to the pom.");
        }
        return result.getArtifact().getFile();
    } catch (final ArtifactResolutionException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #25
Source File: MyArtifactResolver.java    From elasticsearch-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Resolves an Artifact from the repositories.
 * 
 * @param coordinates The artifact coordinates
 * @return The local file resolved/downloaded for the given coordinates
 * @throws ArtifactException If the artifact cannot be resolved
 */
@Override
public File resolveArtifact(String coordinates) throws ArtifactException
{
    ArtifactRequest request = new ArtifactRequest();
    Artifact artifact = new DefaultArtifact(coordinates);
    request.setArtifact(artifact);
    request.setRepositories(remoteRepositories);

    log.debug(String.format("Resolving artifact %s from %s", artifact, remoteRepositories));

    ArtifactResult result;
    try
    {
        result = repositorySystem.resolveArtifact(repositorySession, request);
    }
    catch (ArtifactResolutionException e)
    {
        throw new ArtifactException(e.getMessage(), e);
    }

    log.debug(String.format("Resolved artifact %s to %s from %s",
            artifact,
            result.getArtifact().getFile(),
            result.getRepository()));

    return result.getArtifact().getFile();
}
 
Example #26
Source File: VersionSubstitutingModelResolverTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubstitution() throws ArtifactResolutionException, ModelBuildingException {

  // Google-cloud-bom 0.121.0-alpha imports google-auth-library-bom 0.19.0.
  DefaultArtifact googleCloudBom =
      new DefaultArtifact("com.google.cloud:google-cloud-bom:pom:0.121.0-alpha");
  ArtifactResult bomResult =
      repositorySystem.resolveArtifact(
          session, new ArtifactRequest(googleCloudBom, ImmutableList.of(CENTRAL), null));

  ImmutableMap<String, String> substitution =
      ImmutableMap.of(
          "com.google.auth:google-auth-library-bom",
          "0.18.0" // This is intentionally different from 0.19.0
          );
  VersionSubstitutingModelResolver resolver =
      new VersionSubstitutingModelResolver(
          session,
          null,
          repositorySystem,
          remoteRepositoryManager,
          ImmutableList.of(CENTRAL), // Needed when parent pom is not locally available
          substitution);

  ModelBuildingRequest modelRequest = new DefaultModelBuildingRequest();
  modelRequest.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
  modelRequest.setPomFile(bomResult.getArtifact().getFile());
  modelRequest.setModelResolver(resolver);
  modelRequest.setSystemProperties(System.getProperties()); // for Java version property

  ModelBuildingResult result = modelBuilder.build(modelRequest);
  DependencyManagement dependencyManagement =
      result.getEffectiveModel().getDependencyManagement();

  Truth.assertWithMessage(
          "Google-cloud-bom's google-auth-library part should be substituted for a different version.")
      .that(dependencyManagement.getDependencies())
      .comparingElementsUsing(dependencyToCoordinates)
      .contains("com.google.auth:google-auth-library-credentials:0.18.0");
}
 
Example #27
Source File: AetherResolver.java    From onos with Apache License 2.0 5 votes vote down vote up
private Artifact resolveArtifact(Artifact artifact) throws Exception {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(artifact);
    request.setRepositories(repositories());
    ArtifactResult result = system.resolveArtifact(session, request);
    return result.getArtifact();
}
 
Example #28
Source File: MavenPluginLocation.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public Path getVersionJar(String version) throws ArtifactResolutionException {
	Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "jar", version.toString());
	
	ArtifactRequest request = new ArtifactRequest();
	request.setArtifact(versionArtifact);
	request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
	ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
	
	return resolveArtifact.getArtifact().getFile().toPath();
}
 
Example #29
Source File: StagerMojo.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
@Override
public Path resolve(ArtifactGAV gav) {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(gav.groupId(), gav.artifactId(), gav.classifier(),
            gav.type(), gav.version()));
    request.setRepositories(remoteRepos);
    ArtifactResult result = null;
    try {
        result = repoSystem.resolveArtifact(repoSession, request);
    } catch (ArtifactResolutionException ex) {
        throw new RuntimeException(ex);
    }
    return result.getArtifact().getFile().toPath();
}
 
Example #30
Source File: CatalogMojo.java    From helidon-build-tools with Apache License 2.0 5 votes vote down vote up
/**
 * Resolve an archetype artifact.
 *
 * @param archetypeEntry archetype entry
 * @return File
 */
private File resolveArchetype(ArchetypeCatalog.ArchetypeEntry archetypeEntry) {
    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(archetypeEntry.groupId(), archetypeEntry.artifactId(), null,
            "jar", archetypeEntry.version()));
    request.setRepositories(remoteRepos);
    ArtifactResult result = null;
    try {
        result = repoSystem.resolveArtifact(repoSession, request);
    } catch (ArtifactResolutionException ex) {
        throw new RuntimeException(ex);
    }
    return result.getArtifact().getFile();
}