use of org.apache.maven.shared.artifact.resolve.ArtifactResolverException in project maven-dependency-plugin by apache.
the class CopyDependenciesMojo method getResolvedPomArtifact.
/**
* @param artifact {@link Artifact}
* @return {@link Artifact}
*/
protected Artifact getResolvedPomArtifact(Artifact artifact) {
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(artifact.getGroupId());
coordinate.setArtifactId(artifact.getArtifactId());
coordinate.setVersion(artifact.getVersion());
coordinate.setExtension("pom");
Artifact pomArtifact = null;
// Resolve the pom artifact using repos
try {
ProjectBuildingRequest buildingRequest = newResolveArtifactProjectBuildingRequest();
pomArtifact = getArtifactResolver().resolveArtifact(buildingRequest, coordinate).getArtifact();
} catch (ArtifactResolverException e) {
getLog().info(e.getMessage());
}
return pomArtifact;
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResolverException in project maven-dependency-plugin by apache.
the class ResolvePluginsMojo method doExecute.
/**
* Main entry into mojo. Gets the list of dependencies and iterates through displaying the resolved version.
*
* @throws MojoExecutionException with a message if an error occurs.
*/
@Override
protected void doExecute() throws MojoExecutionException {
try {
// ideally this should either be DependencyCoordinates or DependencyNode
final Set<Artifact> plugins = resolvePluginArtifacts();
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append("The following plugins have been resolved:\n");
if (plugins == null || plugins.isEmpty()) {
sb.append(" none\n");
} else {
for (Artifact plugin : plugins) {
String artifactFilename = null;
if (outputAbsoluteArtifactFilename) {
try {
// we want to print the absolute file name here
artifactFilename = plugin.getFile().getAbsoluteFile().getPath();
} catch (NullPointerException e) {
// ignore the null pointer, we'll output a null string
artifactFilename = null;
}
}
String id = plugin.toString();
sb.append(" " + id + (outputAbsoluteArtifactFilename ? ":" + artifactFilename : "") + "\n");
if (!excludeTransitive) {
DefaultDependableCoordinate pluginCoordinate = new DefaultDependableCoordinate();
pluginCoordinate.setGroupId(plugin.getGroupId());
pluginCoordinate.setArtifactId(plugin.getArtifactId());
pluginCoordinate.setVersion(plugin.getVersion());
for (final Artifact artifact : resolveArtifactDependencies(pluginCoordinate)) {
artifactFilename = null;
if (outputAbsoluteArtifactFilename) {
try {
// we want to print the absolute file name here
artifactFilename = artifact.getFile().getAbsoluteFile().getPath();
} catch (NullPointerException e) {
// ignore the null pointer, we'll output a null string
artifactFilename = null;
}
}
id = artifact.toString();
sb.append(" " + id + (outputAbsoluteArtifactFilename ? ":" + artifactFilename : "") + "\n");
}
}
}
sb.append("\n");
String output = sb.toString();
if (outputFile == null) {
DependencyUtil.log(output, getLog());
} else {
DependencyUtil.write(output, outputFile, appendOutput, getLog());
}
}
} catch (final IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (final ArtifactFilterException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (ArtifactResolverException e) {
throw new MojoExecutionException(e.getMessage(), e);
} catch (DependencyResolverException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResolverException in project webservices-axiom by apache.
the class PostProcessMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (project.getPackaging().equals("pom")) {
return;
}
Set<String> sources = new HashSet<>();
try (JarInputStream in = new JarInputStream(new FileInputStream(project.getArtifact().getFile()))) {
JarEntry entry;
while ((entry = in.getNextJarEntry()) != null) {
String name = entry.getName();
if (name.endsWith(".class")) {
new ClassReader(in).accept(new SourceExtractor(sources, name.substring(0, name.lastIndexOf('/') + 1)), ClassReader.SKIP_CODE);
}
}
} catch (IOException ex) {
throw new MojoExecutionException("Error reading jar: " + ex.getMessage(), ex);
}
File sourcesJar = new File(outputDirectory, finalName + "-sources.jar");
File postProcessedSourcesJar = new File(outputDirectory, finalName + "-post-processed-sources.jar");
try (JarOutputStream out = new JarOutputStream(new FileOutputStream(postProcessedSourcesJar))) {
processSourceJar(sourcesJar, sources, true, out);
ArtifactFilter filter = new ScopeArtifactFilter(Artifact.SCOPE_RUNTIME);
for (Artifact artifact : project.getArtifacts()) {
if (sources.isEmpty()) {
break;
}
if (filter.include(artifact)) {
DefaultArtifactCoordinate coordinate = new DefaultArtifactCoordinate();
coordinate.setGroupId(artifact.getGroupId());
coordinate.setArtifactId(artifact.getArtifactId());
coordinate.setVersion(artifact.getVersion());
coordinate.setExtension("jar");
coordinate.setClassifier("sources");
Artifact resolvedArtifact;
try {
resolvedArtifact = artifactResolver.resolveArtifact(session.getProjectBuildingRequest(), coordinate).getArtifact();
} catch (ArtifactResolverException ex) {
getLog().warn("Could not get sources for " + artifact);
continue;
}
if (resolvedArtifact.isResolved()) {
processSourceJar(resolvedArtifact.getFile(), sources, false, out);
}
}
}
} catch (IOException ex) {
throw new MojoExecutionException("Error writing jar: " + ex.getMessage(), ex);
}
sourcesJar.delete();
postProcessedSourcesJar.renameTo(sourcesJar);
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResolverException in project syndesis by syndesisio.
the class ExtractConnectorDescriptorsMojo method execute.
@Override
@SuppressWarnings({ "PMD.EmptyCatchBlock", "PMD.CyclomaticComplexity" })
public void execute() throws MojoExecutionException, MojoFailureException {
ArrayNode root = new ArrayNode(JsonNodeFactory.instance);
URLClassLoader classLoader = null;
try {
PluginDescriptor desc = (PluginDescriptor) getPluginContext().get("pluginDescriptor");
List<Artifact> artifacts = desc.getArtifacts();
ProjectBuildingRequest buildingRequest = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
buildingRequest.setRemoteRepositories(remoteRepositories);
for (Artifact artifact : artifacts) {
ArtifactResult result = artifactResolver.resolveArtifact(buildingRequest, artifact);
File jar = result.getArtifact().getFile();
classLoader = createClassLoader(jar);
if (classLoader == null) {
throw new IOException("Can not create classloader for " + jar);
}
ObjectNode entry = new ObjectNode(JsonNodeFactory.instance);
addConnectorMeta(entry, classLoader);
addComponentMeta(entry, classLoader);
if (entry.size() > 0) {
addGav(entry, artifact);
root.add(entry);
}
}
if (root.size() > 0) {
saveCamelMetaData(root);
}
} catch (ArtifactResolverException | IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
if (classLoader != null) {
try {
classLoader.close();
} catch (IOException ignored) {
}
}
}
}
use of org.apache.maven.shared.artifact.resolve.ArtifactResolverException in project component-runtime by Talend.
the class ArtifactTransformer method hasTransformedResource.
@Override
public boolean hasTransformedResource() {
artifacts = new ArrayList<>();
final ArtifactFilter filter = getFilter();
if (userArtifacts != null && !userArtifacts.isEmpty()) {
final ArtifactResolver resolver;
final DependencyGraphBuilder graphBuilder;
final ProjectBuilder projectBuilder;
final PlexusContainer container = session.getContainer();
try {
resolver = ArtifactResolver.class.cast(container.lookup(ArtifactResolver.class, "default"));
projectBuilder = ProjectBuilder.class.cast(container.lookup(ProjectBuilder.class, "default"));
graphBuilder = includeTransitiveDependencies ? DependencyGraphBuilder.class.cast(container.lookup(DependencyGraphBuilder.class, "default")) : null;
} catch (final ComponentLookupException e) {
throw new IllegalArgumentException(e);
}
artifacts.addAll(userArtifacts.stream().flatMap(coords -> {
try {
final String type = ofNullable(coords.type).filter(s -> !s.isEmpty()).orElse("jar");
final Artifact art = new DefaultArtifact(coords.groupId, coords.artifactId, coords.version, coords.scope, type, ofNullable(coords.classifier).orElse(""), new DefaultArtifactHandler() {
{
setExtension(type);
}
});
final Artifact artifact = resolver.resolveArtifact(session.getProjectBuildingRequest(), art).getArtifact();
if (includeTransitiveDependencies) {
final DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest(session.getProjectBuildingRequest());
try {
final ProjectBuildingResult projectBuildingResult = projectBuilder.build(artifact, true, request);
final DependencyNode transitives = graphBuilder.buildDependencyGraph(projectBuildingResult.getProject(), filter);
final CollectingDependencyNodeVisitor visitor = new CollectingDependencyNodeVisitor();
transitives.accept(new FilteringDependencyNodeVisitor(visitor, new ArtifactDependencyNodeFilter(filter)));
return Stream.concat(Stream.of(artifact), visitor.getNodes().stream().map(DependencyNode::getArtifact).map(a -> {
try {
return resolver.resolveArtifact(request, a).getArtifact();
} catch (final ArtifactResolverException e) {
throw new IllegalStateException(e);
}
}));
} catch (final ProjectBuildingException | DependencyGraphBuilderException e) {
throw new IllegalStateException(e);
}
}
return Stream.of(artifact);
} catch (final ArtifactResolverException e) {
throw new IllegalArgumentException(e);
}
}).collect(toSet()));
}
if (includeProjectComponentDependencies) {
// this is a weird category of projects but can exist
final MavenProject project = session.getCurrentProject();
if (project.getArtifacts() != null && !project.getArtifacts().isEmpty()) {
project.setArtifactFilter(filter);
try {
artifacts.addAll(project.getArtifacts());
} finally {
// shade plugin uses it OOTB so reset it for the end of the execution (in case
// another transformer needs it)
project.setArtifactFilter(new CumulativeScopeArtifactFilter(singletonList("runtime")));
}
}
}
return !artifacts.isEmpty();
}
Aggregations