Java Code Examples for org.eclipse.aether.resolution.ArtifactRequest#setRepositories()

The following examples show how to use org.eclipse.aether.resolution.ArtifactRequest#setRepositories() . 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: EclipseAetherArtifactResolver.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 6 votes vote down vote up
@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 2
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 3
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 4
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 5
Source File: MavenPluginLocation.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] getVersionIcon(String version) throws ArtifactResolutionException, IOException {
	Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "icon", "png", version.toString());
	
	ArtifactRequest request = new ArtifactRequest();
	request.setArtifact(versionArtifact);
	request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
	ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
	
	return FileUtils.readFileToByteArray(resolveArtifact.getArtifact().getFile());
}
 
Example 6
Source File: ArtifactHelper.java    From LicenseScout with 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 7
Source File: MavenPluginLocation.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public String getRepository(String version) throws ArtifactResolutionException {
	Artifact pomArtifact = new DefaultArtifact(groupId, artifactId, "pom", version.toString());

	ArtifactRequest request = new ArtifactRequest();
	request.setArtifact(pomArtifact);
	request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
	ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
	
	return resolveArtifact.getRepository().toString();
}
 
Example 8
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 9
Source File: MavenPluginLocation.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public byte[] getVersionPluginXml(String version) throws ArtifactResolutionException, IOException {
//		return getJarContent(version, "plugin/plugin.xml");

		Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "plugin", "xml", version.toString());
		
		ArtifactRequest request = new ArtifactRequest();
		request.setArtifact(versionArtifact);
		request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
		ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
		
		byte[] bytes = FileUtils.readFileToByteArray(resolveArtifact.getArtifact().getFile());
		return bytes;
	}
 
Example 10
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 11
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 12
Source File: ArtemisAbstractPlugin.java    From activemq-artemis with Apache License 2.0 5 votes vote down vote up
protected File resolveArtifact(Artifact artifact) throws MojoExecutionException, DependencyCollectionException {
   ArtifactRequest request = new ArtifactRequest();
   request.setArtifact(artifact);
   request.setRepositories(remoteRepos);

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

   return result.getArtifact().getFile();
}
 
Example 13
Source File: MavenPluginLocation.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public GregorianCalendar getVersionDate(String version) throws ArtifactResolutionException, ParseException, IOException {
//		byte[] jarContent = getJarContent(version, "plugin/version.properties");
//		if (jarContent != null) {
//			Properties properties = new Properties();
//			properties.load(new ByteArrayInputStream(jarContent));
//			String buildDate = properties.getProperty("build.date");
//			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
//			try {
//				Date parse = dateFormat.parse(buildDate);
//				GregorianCalendar gregorianCalendar = new GregorianCalendar();
//				gregorianCalendar.setTimeInMillis(parse.getTime());
//				return gregorianCalendar;
//			} catch (ParseException e) {
//				return null;
//			}
//		}
//		return null;

		Artifact versionArtifact = new DefaultArtifact(groupId, artifactId, "version", "properties", version.toString());
		
		ArtifactRequest request = new ArtifactRequest();
		request.setArtifact(versionArtifact);
		request.setRepositories(mavenPluginRepository.getRepositoriesAsList());
		ArtifactResult resolveArtifact = mavenPluginRepository.getSystem().resolveArtifact(mavenPluginRepository.getSession(), request);
		
		byte[] bytes = FileUtils.readFileToByteArray(resolveArtifact.getArtifact().getFile());
		
		Properties properties = new Properties();
		properties.load(new ByteArrayInputStream(bytes));
		String buildDate = properties.getProperty("build.date");
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
		try {
			Date parse = dateFormat.parse(buildDate);
			GregorianCalendar gregorianCalendar = new GregorianCalendar();
			gregorianCalendar.setTimeInMillis(parse.getTime());
			return gregorianCalendar;
		} catch (ParseException e) {
			return null;
		}
	}
 
Example 14
Source File: DependencyResolver.java    From spring-cloud-function with Apache License 2.0 5 votes vote down vote up
private List<ArtifactRequest> getArtifactRequests(List<Dependency> dependencies) {
	List<ArtifactRequest> list = new ArrayList<>();
	for (Dependency dependency : dependencies) {
		ArtifactRequest request = new ArtifactRequest(dependency.getArtifact(), null,
				null);
		request.setRepositories(aetherRepositories(new Properties()));
		list.add(request);
	}
	return list;
}
 
Example 15
Source File: RepackageExtensionMojo.java    From syndesis with Apache License 2.0 5 votes vote down vote up
/**
 * @param artifact The artifact coordinates in the format {@code
 * <groupId>:<artifactId>[:<extension>[:<classifier>]]:<version>}, must not
 *            be {@code null}.
 *
 */
private ArtifactResult downloadAndInstallArtifact(final String artifact) {
    final ArtifactRequest request = new ArtifactRequest();
    request.setArtifact(new DefaultArtifact(artifact));
    request.setRepositories(remoteRepos);

    getLog().info("Resolving artifact " + artifact + " from " + remoteRepos);
    try {
        return repository.resolveArtifact(session, request);
    } catch (final ArtifactResolutionException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example 16
Source File: ArtifactDownload.java    From CogniCrypt with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This method fetches the artifact from the remote server using aether library
 * 
 * @param groupId group ID of the artifact
 * @param artifactId artifact ID of the artifact
 * @param version artifact version to be downloaded
 * @param classifier classifier of the artifact
 * @param packaging packaging of the artifact
 * @param localRepository destination path
 * @return location of the downloaded artifact in the local system
 * @throws IOException
 */
public static File getArtifactByAether(String groupId, String artifactId, String version, String classifier, String packaging, File localRepository) throws IOException {
	RepositorySystem repositorySystem = newRepositorySystem();
	RepositorySystemSession session = newSession(repositorySystem, localRepository);

	Artifact artifact = new DefaultArtifact(groupId, artifactId, classifier, packaging, version);
	ArtifactRequest artifactRequest = new ArtifactRequest();
	artifactRequest.setArtifact(artifact);

	List<RemoteRepository> repositories = new ArrayList<>();
	Section ini = Utils.getConfig().get(Constants.INI_URL_HEADER);

	RemoteRepository remoteRepository = new RemoteRepository.Builder("public", "default", ini.get(Constants.INI_NEXUS_SOOT_RELEASE)).build();

	repositories.add(remoteRepository);

	artifactRequest.setRepositories(repositories);
	File result = null;

	try {
		ArtifactResult artifactResult = repositorySystem.resolveArtifact(session, artifactRequest);
		artifact = artifactResult.getArtifact();
		if (artifact != null) {
			result = artifact.getFile();
		}
	}
	catch (ArtifactResolutionException e) {
		throw new IOException("Artifact " + groupId + ":" + artifactId + ":" + version + " could not be downloaded due to " + e.getMessage());
	}

	return result;

}
 
Example 17
Source File: DependencyResolver.java    From spring-init with Apache License 2.0 5 votes vote down vote up
private List<ArtifactRequest> getArtifactRequests(List<Dependency> dependencies) {
	List<ArtifactRequest> list = new ArrayList<>();
	for (Dependency dependency : dependencies) {
		ArtifactRequest request = new ArtifactRequest(dependency.getArtifact(), null,
				null);
		request.setRepositories(aetherRepositories(new Properties()));
		list.add(request);
	}
	return list;
}
 
Example 18
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 19
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();
}
 
Example 20
Source File: RepositoryDownloader.java    From phantomjs-maven-plugin with MIT License 4 votes vote down vote up
private ArtifactRequest createRequest(Archive archive) {
  ArtifactRequest request = new ArtifactRequest();
  request.setArtifact(artifactBuilder.createArtifact(archive));
  request.setRepositories(repositoryDetails.getRemoteRepositories());
  return request;
}