use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.
the class AbstractResolveMojo method resolveArtifactDependencies.
/**
* This method resolves all transitive dependencies of an artifact.
*
* @param artifact the artifact used to retrieve dependencies
* @return resolved set of dependencies
* @throws DependencyResolverException
*/
protected Set<Artifact> resolveArtifactDependencies(final DependableCoordinate artifact) throws DependencyResolverException {
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
Iterable<ArtifactResult> artifactResults = getDependencyResolver().resolveDependencies(buildingRequest, artifact, null);
Set<Artifact> artifacts = new LinkedHashSet<Artifact>();
for (final ArtifactResult artifactResult : artifactResults) {
artifacts.add(artifactResult.getArtifact());
}
return artifacts;
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.
the class ShadeMojoTest method testShadeWithFilter.
/**
* Tests if a Filter is installed correctly, also if createSourcesJar is set to true.
*
* @throws Exception
*/
public void testShadeWithFilter() throws Exception {
ShadeMojo mojo = new ShadeMojo();
// set createSourcesJar = true
Field createSourcesJar = ShadeMojo.class.getDeclaredField("createSourcesJar");
createSourcesJar.setAccessible(true);
createSourcesJar.set(mojo, Boolean.TRUE);
// configure artifactResolver (mocked) for mojo
ArtifactResolver mockArtifactResolver = new ArtifactResolver() {
@Override
public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final Artifact art) throws ArtifactResolverException {
return new ArtifactResult() {
@Override
public Artifact getArtifact() {
art.setResolved(true);
String fileName = art.getArtifactId() + "-" + art.getVersion() + (art.getClassifier() != null ? "-" + art.getClassifier() : "") + ".jar";
art.setFile(new File(fileName));
return art;
}
};
}
@Override
public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final ArtifactCoordinate coordinate) throws ArtifactResolverException {
return new ArtifactResult() {
@Override
public Artifact getArtifact() {
Artifact art = mock(Artifact.class);
when(art.getGroupId()).thenReturn(coordinate.getGroupId());
when(art.getArtifactId()).thenReturn(coordinate.getArtifactId());
when(art.getType()).thenReturn(coordinate.getExtension());
when(art.getClassifier()).thenReturn(coordinate.getClassifier());
when(art.isResolved()).thenReturn(true);
String fileName = coordinate.getArtifactId() + "-" + coordinate.getVersion() + (coordinate.getClassifier() != null ? "-" + coordinate.getClassifier() : "") + ".jar";
when(art.getFile()).thenReturn(new File(fileName));
return art;
}
};
}
};
Field artifactResolverField = ShadeMojo.class.getDeclaredField("artifactResolver");
artifactResolverField.setAccessible(true);
artifactResolverField.set(mojo, mockArtifactResolver);
// create and configure MavenProject
MavenProject project = new MavenProject();
ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE);
Artifact artifact = new DefaultArtifact("org.apache.myfaces.core", "myfaces-impl", VersionRange.createFromVersion("2.0.1-SNAPSHOT"), "compile", "jar", null, artifactHandler);
// setFile and setResolved
artifact = mockArtifactResolver.resolveArtifact(null, artifact).getArtifact();
project.setArtifact(artifact);
Field projectField = ShadeMojo.class.getDeclaredField("project");
projectField.setAccessible(true);
projectField.set(mojo, project);
// create and configure the ArchiveFilter
ArchiveFilter archiveFilter = new ArchiveFilter();
Field archiveFilterArtifact = ArchiveFilter.class.getDeclaredField("artifact");
archiveFilterArtifact.setAccessible(true);
archiveFilterArtifact.set(archiveFilter, "org.apache.myfaces.core:myfaces-impl");
// add ArchiveFilter to mojo
Field filtersField = ShadeMojo.class.getDeclaredField("filters");
filtersField.setAccessible(true);
filtersField.set(mojo, new ArchiveFilter[] { archiveFilter });
Field sessionField = ShadeMojo.class.getDeclaredField("session");
sessionField.setAccessible(true);
sessionField.set(mojo, mock(MavenSession.class));
// invoke getFilters()
Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", new Class[0]);
getFilters.setAccessible(true);
List<Filter> filters = (List<Filter>) getFilters.invoke(mojo);
// assertions - there must be one filter
assertEquals(1, filters.size());
// the filter must be able to filter the binary and the sources jar
Filter filter = filters.get(0);
// binary jar
assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT.jar")));
// sources jar
assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT-sources.jar")));
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.
the class AbstractJavadocMojo method getArtifactsAbsolutePath.
/**
* Return the Javadoc artifact path and its transitive dependencies path from the local repository
*
* @param javadocArtifact not null
* @return a list of locale artifacts absolute path
* @throws MavenReportException if any
*/
private List<String> getArtifactsAbsolutePath(JavadocPathArtifact javadocArtifact) throws MavenReportException {
if ((StringUtils.isEmpty(javadocArtifact.getGroupId())) && (StringUtils.isEmpty(javadocArtifact.getArtifactId())) && (StringUtils.isEmpty(javadocArtifact.getVersion()))) {
return Collections.emptyList();
}
List<String> path = new ArrayList<String>();
try {
Artifact artifact = createAndResolveArtifact(javadocArtifact);
path.add(artifact.getFile().getAbsolutePath());
DefaultDependableCoordinate coordinate = new DefaultDependableCoordinate();
coordinate.setGroupId(javadocArtifact.getGroupId());
coordinate.setArtifactId(javadocArtifact.getArtifactId());
coordinate.setVersion(javadocArtifact.getVersion());
Iterable<ArtifactResult> deps = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), coordinate, ScopeFilter.including("compile", "provided"));
for (ArtifactResult a : deps) {
path.add(a.getArtifact().getFile().getAbsolutePath());
}
return path;
} catch (ArtifactResolverException e) {
throw new MavenReportException("Unable to resolve artifact:" + javadocArtifact, e);
} catch (DependencyResolverException e) {
throw new MavenReportException("Unable to resolve dependencies for:" + javadocArtifact, e);
}
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.
the class PurgeLocalRepositoryMojo method getFilteredResolvedArtifacts.
private Set<Artifact> getFilteredResolvedArtifacts(MavenProject project, Set<Artifact> artifacts, TransformableFilter filter) {
try {
Iterable<ArtifactResult> results = dependencyResolver.resolveDependencies(session.getProjectBuildingRequest(), project.getModel(), filter);
Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
for (ArtifactResult artResult : results) {
resolvedArtifacts.add(artResult.getArtifact());
}
return resolvedArtifacts;
} catch (DependencyResolverException e) {
getLog().info("Unable to resolve all dependencies for : " + project.getGroupId() + ":" + project.getArtifactId() + ":" + project.getVersion() + ". Falling back to non-transitive mode for initial artifact resolution.");
}
Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
ArtifactFilter artifactFilter = filter.transform(new ArtifactIncludeFilterTransformer());
// purge anyway
for (Artifact artifact : artifacts) {
if (artifactFilter.include(artifact)) {
try {
ArtifactResult artifactResult = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), artifact);
resolvedArtifacts.add(artifactResult.getArtifact());
} catch (ArtifactResolverException e) {
getLog().debug("Unable to resolve artifact: " + artifact);
}
}
}
return resolvedArtifacts;
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResult in project maven-plugins by apache.
the class DefaultRepositoryAssembler method assembleRepositoryArtifacts.
private void assembleRepositoryArtifacts(ProjectBuildingRequest buildingRequest, Iterable<ArtifactResult> result, ArtifactFilter filter, Map<String, GroupVersionAlignment> groupVersionAlignments) throws RepositoryAssemblyException {
try {
for (ArtifactResult ar : result) {
Artifact a = ar.getArtifact();
if (filter.include(a)) {
getLogger().debug("Re-resolving: " + a + " for repository assembly.");
setAlignment(a, groupVersionAlignments);
artifactResolver.resolveArtifact(buildingRequest, TransferUtils.toArtifactCoordinate(a));
a.setVersion(a.getBaseVersion());
File targetFile = new File(repositoryManager.getLocalRepositoryBasedir(buildingRequest), repositoryManager.getPathForLocalArtifact(buildingRequest, a));
FileUtils.copyFile(a.getFile(), targetFile);
// writeChecksums( targetFile );
}
}
} catch (ArtifactResolverException e) {
throw new RepositoryAssemblyException("Error resolving artifacts: " + e.getMessage(), e);
} catch (IOException e) {
throw new RepositoryAssemblyException("Error writing artifact metdata.", e);
}
}
Aggregations