use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project maven-plugins by apache.
the class ResourceResolver method resolveBundlesFromArtifacts.
private List<JavadocBundle> resolveBundlesFromArtifacts(final SourceResolverConfig config, final List<Artifact> artifacts) throws IOException {
final List<Artifact> toResolve = new ArrayList<>(artifacts.size());
for (final Artifact artifact : artifacts) {
if (config.filter() != null && !new ArtifactIncludeFilterTransformer().transform(config.filter()).include(artifact)) {
continue;
}
if (config.includeCompileSources()) {
toResolve.add(createResourceArtifact(artifact, AbstractJavadocMojo.JAVADOC_RESOURCES_ATTACHMENT_CLASSIFIER, config));
}
if (config.includeTestSources()) {
toResolve.add(createResourceArtifact(artifact, AbstractJavadocMojo.TEST_JAVADOC_RESOURCES_ATTACHMENT_CLASSIFIER, config));
}
}
List<String> dirs = new ArrayList<>(toResolve.size());
try {
for (Map.Entry<String, String> entry : resolveAndUnpack(toResolve, config, RESOURCE_VALID_CLASSIFIERS, false)) {
dirs.add(entry.getValue());
}
} catch (ArtifactResolutionException e) {
if (getLogger().isDebugEnabled()) {
getLogger().debug(e.getMessage(), e);
}
} catch (ArtifactNotFoundException e) {
if (getLogger().isDebugEnabled()) {
getLogger().debug(e.getMessage(), e);
}
}
List<JavadocBundle> result = new ArrayList<>();
for (String d : dirs) {
File dir = new File(d);
File resources = new File(dir, ResourcesBundleMojo.RESOURCES_DIR_PATH);
JavadocOptions options = null;
File javadocOptions = new File(dir, ResourcesBundleMojo.BUNDLE_OPTIONS_PATH);
if (javadocOptions.exists()) {
try (FileInputStream reader = new FileInputStream(javadocOptions)) {
options = new JavadocOptionsXpp3Reader().read(reader);
} catch (XmlPullParserException e) {
IOException error = new IOException("Failed to parse javadoc options: " + e.getMessage());
error.initCause(e);
throw error;
}
}
result.add(new JavadocBundle(options, resources));
}
return result;
}
use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project maven-plugins by apache.
the class ResourceResolver method resolveAndUnpack.
/**
* @param artifacts the artifacts to resolve
* @param config the configuration
* @param validClassifiers
* @param propagateErrors
* @return list of <dependencyConflictId, absolutePath>
* @throws ArtifactResolutionException if an exception occurs
* @throws ArtifactNotFoundException if an exception occurs
*/
private Collection<Map.Entry<String, String>> resolveAndUnpack(final List<Artifact> artifacts, final SourceResolverConfig config, final List<String> validClassifiers, final boolean propagateErrors) throws ArtifactResolutionException, ArtifactNotFoundException {
// NOTE: Since these are '-sources' and '-test-sources' artifacts, they won't actually
// resolve transitively...this is just used to aggregate resolution failures into a single
// exception.
final Set<Artifact> artifactSet = new LinkedHashSet<>(artifacts);
final ArtifactFilter filter;
if (config.filter() != null) {
filter = new ArtifactIncludeFilterTransformer().transform(config.filter());
} else {
filter = null;
}
final List<Map.Entry<String, String>> result = new ArrayList<>(artifacts.size());
for (final Artifact a : artifactSet) {
if (!validClassifiers.contains(a.getClassifier()) || (filter != null && !filter.include(a))) {
continue;
}
Artifact resolvedArtifact;
try {
resolvedArtifact = resolver.resolveArtifact(config.getBuildingRequest(), a).getArtifact();
} catch (ArtifactResolverException e1) {
continue;
}
final File d = new File(config.outputBasedir(), a.getArtifactId() + "-" + a.getVersion() + "-" + a.getClassifier());
if (!d.exists()) {
d.mkdirs();
}
try {
final UnArchiver unArchiver = archiverManager.getUnArchiver(a.getType());
unArchiver.setDestDirectory(d);
unArchiver.setSourceFile(resolvedArtifact.getFile());
unArchiver.extract();
result.add(new AbstractMap.SimpleEntry<String, String>(a.getDependencyConflictId(), d.getAbsolutePath()));
} catch (final NoSuchArchiverException e) {
if (propagateErrors) {
throw new ArtifactResolutionException("Failed to retrieve valid un-archiver component: " + a.getType(), a, e);
}
} catch (final ArchiverException e) {
if (propagateErrors) {
throw new ArtifactResolutionException("Failed to unpack: " + a.getId(), a, e);
}
}
}
return result;
}
use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project tomee by apache.
the class JarsTxtMojo method execute.
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
if (!outputFile.getParentFile().exists()) {
FileUtils.mkdir(outputFile.getParentFile().getAbsolutePath());
}
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(outputFile));
final TreeSet<String> set = new TreeSet<>();
for (final Artifact a : (Set<Artifact>) project.getArtifacts()) {
if (!acceptScope(a.getScope()) || !acceptType(a.getType())) {
continue;
}
a.setScope(Artifact.SCOPE_PROVIDED);
final StringBuilder line = new StringBuilder("mvn:").append(a.getGroupId()).append("/").append(a.getArtifactId()).append("/").append(version(a));
final boolean isJar = JAR.equals(a.getType());
if (!isJar) {
line.append("/").append(a.getType());
}
if (a.getClassifier() != null) {
if (isJar) {
line.append("/").append(JAR);
}
line.append("/").append(a.getClassifier());
}
if (hashAlgo != null) {
final Artifact artifact = factory.createDependencyArtifact(a.getGroupId(), a.getArtifactId(), VersionRange.createFromVersion(a.getVersion()), a.getType(), a.getClassifier(), a.getScope());
try {
resolver.resolve(artifact, remoteRepos, local);
} catch (final ArtifactResolutionException | ArtifactNotFoundException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
final File file = artifact.getFile();
line.append("|").append(Files.hash((Set<URL>) Collections.singleton(file.toURI().toURL()), hashAlgo)).append("|").append(hashAlgo);
}
set.add(line.toString());
}
if (additionals != null) {
if (placeHolders == null) {
placeHolders = new HashMap<>();
}
final StrSubstitutor lookup = new StrSubstitutor(StrLookup.mapLookup(placeHolders));
for (final String line : additionals) {
final StringBuilder builder = new StringBuilder(line);
if (hashAlgo != null) {
builder.append("|").append(Files.hash(urls(line, lookup), hashAlgo)).append("|").append(hashAlgo);
}
set.add(builder.toString());
}
}
// written after to be sorted, more readable
for (final String line : set) {
writer.write(line);
writer.write("\n");
}
writer.flush();
} catch (final IOException e) {
getLog().error(e.getMessage(), e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (final IOException e) {
// no-op
}
}
}
}
use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project che by eclipse.
the class CheArtifactResolver method resolveOrig.
private void resolveOrig(Artifact artifact, List<ArtifactRepository> remoteRepositories, RepositorySystemSession session) throws ArtifactResolutionException, ArtifactNotFoundException {
if (artifact == null) {
return;
}
if (Artifact.SCOPE_SYSTEM.equals(artifact.getScope())) {
File systemFile = artifact.getFile();
if (systemFile == null) {
throw new ArtifactNotFoundException("System artifact: " + artifact + " has no file attached", artifact);
}
if (!systemFile.exists()) {
throw new ArtifactNotFoundException("System artifact: " + artifact + " not found in path: " + systemFile, artifact);
}
if (!systemFile.isFile()) {
throw new ArtifactNotFoundException("System artifact: " + artifact + " is not a file: " + systemFile, artifact);
}
artifact.setResolved(true);
return;
}
if (!artifact.isResolved()) {
ArtifactResult result;
try {
ArtifactRequest artifactRequest = new ArtifactRequest();
artifactRequest.setArtifact(RepositoryUtils.toArtifact(artifact));
artifactRequest.setRepositories(RepositoryUtils.toRepos(remoteRepositories));
// Maven 2.x quirk: an artifact always points at the local repo, regardless whether resolved or not
LocalRepositoryManager lrm = session.getLocalRepositoryManager();
String path = lrm.getPathForLocalArtifact(artifactRequest.getArtifact());
artifact.setFile(new File(lrm.getRepository().getBasedir(), path));
result = repoSystem.resolveArtifact(session, artifactRequest);
} catch (org.eclipse.aether.resolution.ArtifactResolutionException e) {
if (e.getCause() instanceof org.eclipse.aether.transfer.ArtifactNotFoundException) {
throw new ArtifactNotFoundException(e.getMessage(), artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(), artifact.getClassifier(), remoteRepositories, artifact.getDownloadUrl(), artifact.getDependencyTrail(), e);
} else {
throw new ArtifactResolutionException(e.getMessage(), artifact, remoteRepositories, e);
}
}
artifact.selectVersion(result.getArtifact().getVersion());
artifact.setFile(result.getArtifact().getFile());
artifact.setResolved(true);
if (artifact.isSnapshot()) {
Matcher matcher = Artifact.VERSION_FILE_PATTERN.matcher(artifact.getVersion());
if (matcher.matches()) {
Snapshot snapshot = new Snapshot();
snapshot.setTimestamp(matcher.group(2));
try {
snapshot.setBuildNumber(Integer.parseInt(matcher.group(3)));
artifact.addMetadata(new SnapshotArtifactRepositoryMetadata(artifact, snapshot));
} catch (NumberFormatException e) {
logger.warn("Invalid artifact version " + artifact.getVersion() + ": " + e.getMessage());
}
}
}
}
}
use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project maven-plugins by apache.
the class AbstractJavadocMojo method getClasspath.
/**
* Method that sets the classpath elements that will be specified in the javadoc <code>-classpath</code>
* parameter. Since we have all the sources of the current reactor, it is sufficient to consider the
* dependencies of the reactor modules, excluding the module artifacts which may not yet be available
* when the reactor project is built for the first time.
*
* @return a String that contains the concatenated classpath elements, separated by the System pathSeparator
* string (colon (<code>:</code>) on Solaris or semi-colon (<code>;</code>) on Windows).
* @throws MavenReportException if any.
* @see File#pathSeparator
*/
private String getClasspath() throws MavenReportException {
List<String> classpathElements = new ArrayList<String>();
Map<String, Artifact> compileArtifactMap = new HashMap<String, Artifact>();
if (isTest()) {
classpathElements.addAll(getProjectBuildOutputDirs(project));
}
populateCompileArtifactMap(compileArtifactMap, getProjectArtifacts(project));
if (isAggregator() && project.isExecutionRoot()) {
List<Artifact> reactorArtifacts = new ArrayList<Artifact>();
for (MavenProject p : reactorProjects) {
reactorArtifacts.add(p.getArtifact());
}
try {
for (MavenProject subProject : reactorProjects) {
if (subProject != project) {
classpathElements.addAll(getProjectBuildOutputDirs(subProject));
Set<Artifact> dependencyArtifacts = subProject.createArtifacts(factory, null, null);
// do not attempt to resolve artifacts of the current reactor which may not exist yet
dependencyArtifacts.removeAll(reactorArtifacts);
if (!dependencyArtifacts.isEmpty()) {
ArtifactResolutionResult result = null;
try {
result = resolver.resolveTransitively(dependencyArtifacts, subProject.getArtifact(), subProject.getManagedVersionMap(), localRepository, subProject.getRemoteArtifactRepositories(), artifactMetadataSource);
} catch (ArtifactNotFoundException e) {
throw new MavenReportException(e.getMessage(), e);
} catch (ArtifactResolutionException e) {
throw new MavenReportException(e.getMessage(), e);
}
if (result == null) {
continue;
}
populateCompileArtifactMap(compileArtifactMap, getCompileArtifacts(result.getArtifacts()));
if (getLog().isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("Compiled artifacts for ");
sb.append(subProject.getGroupId()).append(":");
sb.append(subProject.getArtifactId()).append(":");
sb.append(subProject.getVersion()).append('\n');
for (Artifact a : compileArtifactMap.values()) {
sb.append(a.getFile()).append('\n');
}
getLog().debug(sb.toString());
}
}
}
}
} catch (InvalidDependencyVersionException e) {
throw new MavenReportException(e.getMessage(), e);
}
}
for (Artifact a : compileArtifactMap.values()) {
classpathElements.add(a.getFile().toString());
}
if (additionalDependencies != null) {
for (Dependency dependency : additionalDependencies) {
Artifact artifact = resolveDependency(dependency);
String path = artifact.getFile().toString();
getLog().debug("add additional artifact with path " + path);
classpathElements.add(path);
}
}
return StringUtils.join(classpathElements.iterator(), File.pathSeparator);
}
Aggregations