use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.
the class AbstractDependencyFilterMojo method resolve.
protected Set<Artifact> resolve(Set<ArtifactCoordinate> coordinates, boolean stopOnFailure) throws MojoExecutionException {
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
Set<Artifact> resolvedArtifacts = new LinkedHashSet<Artifact>();
for (ArtifactCoordinate coordinate : coordinates) {
try {
Artifact artifact = artifactResolver.resolveArtifact(buildingRequest, coordinate).getArtifact();
resolvedArtifacts.add(artifact);
} catch (ArtifactResolverException ex) {
// an error occurred during resolution, log it an continue
getLog().debug("error resolving: " + coordinate);
getLog().debug(ex);
if (stopOnFailure) {
throw new MojoExecutionException("error resolving: " + coordinate, ex);
}
}
}
return resolvedArtifacts;
}
use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.
the class TestClassifierTypeTranslator method testClassifierAndType.
public void testClassifierAndType() {
String classifier = "jdk14";
String type = "sources";
ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
Set<ArtifactCoordinate> results = at.translate(artifacts, log);
for (Artifact artifact : artifacts) {
Iterator<ArtifactCoordinate> resultIter = results.iterator();
boolean found = false;
while (!found && resultIter.hasNext()) {
ArtifactCoordinate translatedArtifact = resultIter.next();
if (artifact.getArtifactId() == translatedArtifact.getArtifactId() && artifact.getGroupId() == translatedArtifact.getGroupId()) /*&& artifact.getScope() == translatedArtifact.getScope()*/
{
assertEquals(translatedArtifact.getClassifier(), classifier);
assertEquals(translatedArtifact.getExtension(), type);
found = true;
break;
}
}
assertTrue(found);
}
}
use of org.apache.maven.shared.artifact.ArtifactCoordinate in project maven-plugins by apache.
the class TestClassifierTypeTranslator method doTestNullEmptyClassifier.
public void doTestNullEmptyClassifier(String classifier) {
String type = "zip";
ArtifactTranslator at = new ClassifierTypeTranslator(artifactHandlerManager, classifier, type);
Set<ArtifactCoordinate> results = at.translate(artifacts, log);
for (Artifact artifact : artifacts) {
Iterator<ArtifactCoordinate> resultIter = results.iterator();
boolean found = false;
while (resultIter.hasNext()) {
ArtifactCoordinate translatedArtifact = resultIter.next();
if (artifact.getArtifactId().equals(translatedArtifact.getArtifactId()) && artifact.getGroupId().equals(translatedArtifact.getGroupId())) /*&& artifact.getScope().equals(translatedArtifact.getScope())*/
{
// classifier is null, should be the same as the artifact
assertEquals(artifact.getClassifier(), translatedArtifact.getClassifier());
assertEquals(type, translatedArtifact.getExtension());
found = true;
break;
}
}
assertTrue(found);
}
}
use of org.apache.maven.shared.artifact.ArtifactCoordinate 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.ArtifactCoordinate in project maven-plugins by apache.
the class EvaluateMojo method getArtifactFile.
/**
* @param isPom <code>true</code> to lookup the <code>maven-model</code> artifact jar, <code>false</code> to lookup
* the <code>maven-settings</code> artifact jar.
* @return the <code>org.apache.maven:maven-model|maven-settings</code> artifact jar file for this current
* HelpPlugin pom.
* @throws MojoExecutionException if any
* @throws ProjectBuildingException if any
* @throws ArtifactResolverException if any
*/
private File getArtifactFile(boolean isPom) throws MojoExecutionException, ProjectBuildingException, ArtifactResolverException {
List<Dependency> dependencies = getHelpPluginPom().getDependencies();
for (Dependency depependency : dependencies) {
if (!(depependency.getGroupId().equals("org.apache.maven"))) {
continue;
}
if (isPom) {
if (!(depependency.getArtifactId().equals("maven-model"))) {
continue;
}
} else {
if (!(depependency.getArtifactId().equals("maven-settings"))) {
continue;
}
}
ArtifactCoordinate coordinate = getArtifactCoordinate(depependency.getGroupId(), depependency.getArtifactId(), depependency.getVersion(), "jar");
ProjectBuildingRequest pbr = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
pbr.setRemoteRepositories(remoteRepositories);
return artifactResolver.resolveArtifact(pbr, coordinate).getArtifact().getFile();
}
throw new MojoExecutionException("Unable to find the 'org.apache.maven:" + (isPom ? "maven-model" : "maven-settings") + "' artifact");
}
Aggregations