Java Code Examples for org.eclipse.aether.resolution.ArtifactResolutionException

The following examples show how to use org.eclipse.aether.resolution.ArtifactResolutionException. These examples are extracted from open source projects. 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 Project: vertx-maven-plugin   Source File: AbstractVertxMojo.java    License: 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 2
Source Project: cloud-opensource-java   Source File: LinkageMonitorTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildModelWithSnapshotBom_noSnapshotUpdate()
    throws ModelBuildingException, ArtifactResolutionException {
  Model model =
      LinkageMonitor.buildModelWithSnapshotBom(
          system, session, "com.google.cloud:libraries-bom:2.2.1", ImmutableMap.of());
  List<Dependency> dependencies = model.getDependencyManagement().getDependencies();

  assertEquals(
      "There should not be a duplicate",
      dependencies.stream().distinct().count(),
      dependencies.size());

  // This number is different from the number appearing in BOM dashboard because model from
  // buildModelWithSnapshotBom still contains unnecessary artifacts that would be filtered by
  // RepositoryUtility.shouldSkipBomMember
  assertEquals(214, dependencies.size());
}
 
Example 3
Source Project: cloud-opensource-java   Source File: ClassPathEntryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetClassNames() throws IOException, ArtifactResolutionException {
  // copy into the local repository so we can read the jar file
  Artifact artifact = resolveArtifact("com.google.truth.extensions:truth-java8-extension:1.0.1");
  
  ClassPathEntry entry = new ClassPathEntry(artifact);
  ImmutableSet<String> classFileNames = entry.getFileNames();
  
  Truth.assertThat(classFileNames).containsExactly(
      "com.google.common.truth.IntStreamSubject",
      "com.google.common.truth.LongStreamSubject",
      "com.google.common.truth.OptionalDoubleSubject",
      "com.google.common.truth.OptionalSubject",
      "com.google.common.truth.OptionalIntSubject",
      "com.google.common.truth.OptionalLongSubject",
      "com.google.common.truth.PathSubject",
      "com.google.common.truth.Truth8",
      "com.google.common.truth.StreamSubject");
}
 
Example 4
Source Project: cloud-opensource-java   Source File: ClassPathEntryTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetClassNames_innerClasses()
    throws IOException, ArtifactResolutionException, URISyntaxException {

  ClassPathEntry entry = TestHelper.classPathEntryOfResource(
      "testdata/conscrypt-openjdk-uber-1.4.2.jar");
  ImmutableSet<String> classFileNames = entry.getFileNames();
  Truth.assertThat(classFileNames).containsAtLeast(
      "org.conscrypt.OpenSSLSignature$1",
      "org.conscrypt.OpenSSLContextImpl$TLSv1",
      "org.conscrypt.TrustManagerImpl$1",
      "org.conscrypt.PeerInfoProvider",
      "org.conscrypt.PeerInfoProvider$1",
      "org.conscrypt.ExternalSession$Provider",
      "org.conscrypt.OpenSSLMac");
}
 
Example 5
Source Project: botsing   Source File: CommonBehaviorMojo.java    License: 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 6
Source Project: component-runtime   Source File: SingerMojo.java    License: 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 7
Source Project: vertx-maven-plugin   Source File: AbstractVertxMojo.java    License: 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 8
@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 9
Source Project: thorntail   Source File: CachingArtifactResolvingHelper.java    License: 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 10
Source Project: thorntail   Source File: MavenArtifactResolvingHelper.java    License: 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 11
Source Project: pro   Source File: Aether.java    License: 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 12
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 13
Source Project: packagedrone   Source File: AetherImporter.java    License: Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Prepare a plain import process
 * <p>
 * Prepare a simple import request with a specific list of coordinates
 * </p>
 */
public static AetherResult preparePlain ( final Path tmpDir, final ImportConfiguration cfg ) throws ArtifactResolutionException
{
    Objects.requireNonNull ( tmpDir );
    Objects.requireNonNull ( cfg );

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

    // extend

    final List<ArtifactRequest> requests = extendRequests ( cfg.getCoordinates ().stream ().map ( c -> {
        final DefaultArtifact artifact = new DefaultArtifact ( c.toString () );
        return makeRequest ( ctx.getRepositories (), artifact );
    } ), ctx, cfg );

    // process

    return asResult ( resolve ( ctx, requests ), cfg, empty () );
}
 
Example 14
Source Project: packagedrone   Source File: AetherImporter.java    License: 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
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 16
Source Project: yawp   Source File: DevServerMojo.java    License: 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 17
Source Project: roboconf-platform   Source File: ResolveMojoTest.java    License: 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 18
Source Project: pinpoint   Source File: AbstractPinpointPluginTestSuite.java    License: Apache License 2.0 6 votes vote down vote up
public AbstractPinpointPluginTestSuite(Class<?> testClass) throws InitializationError, ArtifactResolutionException, DependencyResolutionException {
    super(testClass, Collections.<Runner> emptyList());

    PinpointAgent agent = testClass.getAnnotation(PinpointAgent.class);
    this.agentJar = resolveAgentPath(agent);

    PinpointConfig config = testClass.getAnnotation(PinpointConfig.class);
    this.configFile = config == null ? null : resolveConfigFileLocation(config.value());

    PinpointProfile profile = testClass.getAnnotation(PinpointProfile.class);
    this.profile = resolveProfile(profile);

    JvmArgument jvmArgument = testClass.getAnnotation(JvmArgument.class);
    this.jvmArguments = getJvmArguments(jvmArgument);

    JvmVersion jvmVersion = testClass.getAnnotation(JvmVersion.class);
    this.jvmVersions = jvmVersion == null ? new int[] { NO_JVM_VERSION } : jvmVersion.value();

    ImportPlugin importPlugin = testClass.getAnnotation(ImportPlugin.class);
    this.importPluginIds = getImportPlugin(importPlugin);

    this.requiredLibraries = getClassPathList(REQUIRED_CLASS_PATHS);
    this.mavenDependencyLibraries = getClassPathList(MAVEN_DEPENDENCY_CLASS_PATHS);
    this.testClassLocation = resolveTestClassLocation(testClass);
    this.debug = isDebugMode();
}
 
Example 19
Source Project: pinpoint   Source File: PinpointPluginTestSuite.java    License: Apache License 2.0 6 votes vote down vote up
private List<PinpointPluginTestInstance> createCasesWithDependencies(PinpointPluginTestContext context) throws ArtifactResolutionException, DependencyResolutionException {
    List<PinpointPluginTestInstance> cases = new ArrayList<PinpointPluginTestInstance>();

    DependencyResolver resolver = getDependencyResolver(repositories);
    Map<String, List<Artifact>> dependencyCases = resolver.resolveDependencySets(dependencies);

    for (Map.Entry<String, List<Artifact>> dependencyCase : dependencyCases.entrySet()) {
        List<String> libs = new ArrayList<String>();

        for (File lib : resolver.resolveArtifactsAndDependencies(dependencyCase.getValue())) {
            libs.add(lib.getAbsolutePath());
        }

        if (testOnSystemClassLoader) {
            cases.add(new NormalPluginTestCase(context, dependencyCase.getKey(), libs, true));
        }

        if (testOnChildClassLoader) {
            cases.add(new NormalPluginTestCase(context, dependencyCase.getKey(), libs, false));
        }
    }

    return cases;
}
 
Example 20
Source Project: pinpoint   Source File: DependencyResolverTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void resolveArtifactsAndDependencies() throws DependencyResolutionException, ArtifactResolutionException {
    DependencyResolverFactory factory = new DependencyResolverFactory();
    DependencyResolver resolver = factory.get();

    Map<String, List<Artifact>> sets = resolver.resolveDependencySets("org.eclipse.aether:aether-util:[0,)", "org.eclipse.aether:aether-spi");

    int i = 0;
    for (Map.Entry<String, List<Artifact>> set : sets.entrySet()) {
        logger.debug("{}", i++);
        List<File> results = resolver.resolveArtifactsAndDependencies(set.getValue());

        logger.debug(set.getKey());

        for (File result : results) {
            logger.debug("{}", result);
        }
    }
}
 
Example 21
Source Project: jenkins-build-monitor-plugin   Source File: ArtifactTransporter.java    License: MIT License 6 votes vote down vote up
public Path get(@NotNull String groupName, @NotNull String artifactName, @NotNull String version, @NotNull String artifactFileExtension) {
    Artifact artifact = new DefaultArtifact(groupName, artifactName, artifactFileExtension, version);

    RepositorySystem system = newRepositorySystem();
    RepositorySystemSession session = newRepositorySystemSession(system);

    ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(artifact);
    request.setRepositories(repositories(system, session));

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

        artifact = artifactResult.getArtifact();

        Log.info(artifact + " resolved to  " + artifact.getFile());

        return artifact.getFile().toPath();

    } catch (ArtifactResolutionException e) {
        throw new RuntimeException(format("Couldn't resolve a '%s' artifact for '%s:%s:%s'",
                artifactFileExtension, groupName, artifactName, version
        ), e);
    }
}
 
Example 22
Source Project: furnace   Source File: MavenModelResolver.java    License: Eclipse Public License 1.0 6 votes vote down vote up
@Override
public ModelSource resolveModel(String groupId, String artifactId, String version)
         throws UnresolvableModelException
{
   Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "", "pom", version);
   try
   {
      final ArtifactRequest request = new ArtifactRequest(pomArtifact, repositories, null);
      pomArtifact = system.resolveArtifact(session, request).getArtifact();

   }
   catch (ArtifactResolutionException e)
   {
      throw new UnresolvableModelException("Failed to resolve POM for " + groupId + ":" + artifactId + ":"
               + version + " due to " + e.getMessage(), groupId, artifactId, version, e);
   }

   final File pomFile = pomArtifact.getFile();

   return new FileModelSource(pomFile);

}
 
Example 23
Source Project: wisdom   Source File: DependencyFinder.java    License: 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 24
Source Project: wisdom   Source File: DependencyFinder.java    License: 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 25
@Override
public Path resolve(final RepositorySystemSession session, final List<RemoteRepository> repositories, final ArtifactName name) {
    final ArtifactResult result;
    try {
        final ArtifactRequest request = new ArtifactRequest();
        final Artifact defaultArtifact = new DefaultArtifact(name.getGroupId(), name.getArtifactId(), name.getClassifier(), name.getPackaging(), name.getVersion());
        request.setArtifact(defaultArtifact);
        request.setRepositories(repositories);
        result = repoSystem.resolveArtifact(session, request);
    } catch (ArtifactResolutionException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
    if (!result.isResolved()) {
        throw new RuntimeException("Failed to resolve artifact " + name);
    }
    final Artifact artifact = result.getArtifact();
    final File artifactFile;
    if (artifact == null || (artifactFile = artifact.getFile()) == null) {
        throw new RuntimeException("Failed to resolve artifact " + name);
    }
    return artifactFile.toPath();
}
 
Example 26
Source Project: license-check   Source File: OpenSourceLicenseCheckMojo.java    License: 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 27
Source Project: ci.maven   Source File: AbstractLibertySupport.java    License: 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 28
Source Project: LicenseScout   Source File: ArtifactHelper.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Resolves an artifact via the repository system.
 * @param repositoryParameters
 * @param artifactItem
 * @return a file object if a configuration file bundle was found, otherwise null
 * @throws MojoExecutionException 
 */
private static Artifact resolve(final IRepositoryParameters repositoryParameters, final ArtifactItem artifactItem)
        throws MojoExecutionException {
    try {
        final Artifact artifact = createDefaultArtifact(artifactItem);
        final ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(artifact);
        request.setRepositories(repositoryParameters.getRemoteRepositories());
        final ArtifactResult result = repositoryParameters.getRepositorySystem()
                .resolveArtifact(repositoryParameters.getRepositorySystemSession(), request);
        return result.getArtifact();
    } catch (IllegalArgumentException | ArtifactResolutionException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
}
 
Example 29
Source Project: helidon-build-tools   Source File: CatalogMojo.java    License: 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();
}
 
Example 30
Source Project: helidon-build-tools   Source File: StagerMojo.java    License: 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();
}