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

The following examples show how to use org.eclipse.aether.resolution.ArtifactRequest#setArtifact() . 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: 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 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: 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 5
Source File: ArtifactTransporter.java    From jenkins-build-monitor-plugin with 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 6
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 7
Source File: MavenPluginLocation.java    From BIMserver with GNU Affero General Public License v3.0 5 votes vote down vote up
public Path getVersionPom(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.getArtifact().getFile().toPath();
}
 
Example 8
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 9
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 10
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 11
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 12
Source File: AetherImporter.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static ArtifactRequest makeRequest ( final List<RemoteRepository> repositories, final Artifact artifact )
{
    final ArtifactRequest artifactRequest = new ArtifactRequest ();
    artifactRequest.setArtifact ( artifact );
    artifactRequest.setRepositories ( repositories );
    return artifactRequest;
}
 
Example 13
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 14
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 15
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 16
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 17
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 18
Source File: ClassPathEntryTest.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
private static Artifact resolveArtifact(String coordinates) throws ArtifactResolutionException {
  RepositorySystem system = RepositoryUtility.newRepositorySystem();
  RepositorySystemSession session = RepositoryUtility.newSession(system);

  Artifact artifact = new DefaultArtifact(coordinates);    
  ArtifactRequest artifactRequest = new ArtifactRequest();
  artifactRequest.setArtifact(artifact);
  ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);
  
  return artifactResult.getArtifact();
}
 
Example 19
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;
}
 
Example 20
Source File: RemotePluginRepository.java    From BIMserver with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) throws ArtifactResolutionException {
	System.out.println("------------------------------------------------------------");
	System.out.println(RemotePluginRepository.class.getSimpleName());

	RepositorySystem system = newRepositorySystem();

	RepositorySystemSession session = newRepositorySystemSession(system);

	Artifact artifact = new DefaultArtifact("org.eclipse.aether:aether-util:1.0.0.v20140518");

	ArtifactRequest artifactRequest = new ArtifactRequest();
	artifactRequest.setArtifact(artifact);
	artifactRequest.setRepositories(newRepositories(system, session));

	ArtifactResult artifactResult = system.resolveArtifact(session, artifactRequest);

	artifact = artifactResult.getArtifact();

	System.out.println(artifact + " resolved to  " + artifact.getFile());
}