Search in sources :

Example 1 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project che by eclipse.

the class CheArtifactResolver method resolve.

// ------------------------------------------------------------------------
//
// ------------------------------------------------------------------------
public ArtifactResolutionResult resolve(ArtifactResolutionRequest request) {
    Artifact rootArtifact = request.getArtifact();
    Set<Artifact> artifacts = request.getArtifactDependencies();
    Map<String, Artifact> managedVersions = request.getManagedVersionMap();
    List<ResolutionListener> listeners = request.getListeners();
    ArtifactFilter collectionFilter = request.getCollectionFilter();
    ArtifactFilter resolutionFilter = request.getResolutionFilter();
    RepositorySystemSession session = getSession(request.getLocalRepository());
    //TODO: hack because metadata isn't generated in m2e correctly and i want to run the maven i have in the workspace
    if (source == null) {
        try {
            source = container.lookup(ArtifactMetadataSource.class);
        } catch (ComponentLookupException e) {
        // won't happen
        }
    }
    if (listeners == null) {
        listeners = new ArrayList<ResolutionListener>();
        if (logger.isDebugEnabled()) {
            listeners.add(new DebugResolutionListener(logger));
        }
        listeners.add(new WarningResolutionListener(logger));
    }
    ArtifactResolutionResult result = new ArtifactResolutionResult();
    if (request.isResolveRoot()) /* && rootArtifact.getFile() == null */
    {
        try {
            resolve(rootArtifact, request.getRemoteRepositories(), session);
        } catch (ArtifactResolutionException e) {
            result.addErrorArtifactException(e);
            return result;
        } catch (ArtifactNotFoundException e) {
            result.addMissingArtifact(request.getArtifact());
            return result;
        }
    }
    ArtifactResolutionRequest collectionRequest = request;
    if (request.isResolveTransitively()) {
        MetadataResolutionRequest metadataRequest = new DefaultMetadataResolutionRequest(request);
        metadataRequest.setArtifact(rootArtifact);
        metadataRequest.setResolveManagedVersions(managedVersions == null);
        try {
            ResolutionGroup resolutionGroup = source.retrieve(metadataRequest);
            if (managedVersions == null) {
                managedVersions = resolutionGroup.getManagedVersions();
            }
            Set<Artifact> directArtifacts = resolutionGroup.getArtifacts();
            if (artifacts == null || artifacts.isEmpty()) {
                artifacts = directArtifacts;
            } else {
                List<Artifact> allArtifacts = new ArrayList<Artifact>();
                allArtifacts.addAll(artifacts);
                allArtifacts.addAll(directArtifacts);
                Map<String, Artifact> mergedArtifacts = new LinkedHashMap<String, Artifact>();
                for (Artifact artifact : allArtifacts) {
                    String conflictId = artifact.getDependencyConflictId();
                    if (!mergedArtifacts.containsKey(conflictId)) {
                        mergedArtifacts.put(conflictId, artifact);
                    }
                }
                artifacts = new LinkedHashSet<Artifact>(mergedArtifacts.values());
            }
            collectionRequest = new ArtifactResolutionRequest(request);
            collectionRequest.setServers(request.getServers());
            collectionRequest.setMirrors(request.getMirrors());
            collectionRequest.setProxies(request.getProxies());
            collectionRequest.setRemoteRepositories(resolutionGroup.getResolutionRepositories());
        } catch (ArtifactMetadataRetrievalException e) {
            ArtifactResolutionException are = new ArtifactResolutionException("Unable to get dependency information for " + rootArtifact.getId() + ": " + e.getMessage(), rootArtifact, metadataRequest.getRemoteRepositories(), e);
            result.addMetadataResolutionException(are);
            return result;
        }
    }
    if (artifacts == null || artifacts.isEmpty()) {
        if (request.isResolveRoot()) {
            result.addArtifact(rootArtifact);
        }
        return result;
    }
    // After the collection we will have the artifact object in the result but they will not be resolved yet.
    result = artifactCollector.collect(artifacts, rootArtifact, managedVersions, collectionRequest, source, collectionFilter, listeners, null);
    if (result.hasMetadataResolutionExceptions() || result.hasVersionRangeViolations() || result.hasCircularDependencyExceptions()) {
        return result;
    }
    if (result.getArtifactResolutionNodes() != null) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        CountDownLatch latch = new CountDownLatch(result.getArtifactResolutionNodes().size());
        for (ResolutionNode node : result.getArtifactResolutionNodes()) {
            Artifact artifact = node.getArtifact();
            if (resolutionFilter == null || resolutionFilter.include(artifact)) {
                executor.execute(new ResolveTask(classLoader, latch, artifact, session, node.getRemoteRepositories(), result));
            } else {
                latch.countDown();
            }
        }
        try {
            latch.await();
        } catch (InterruptedException e) {
            result.addErrorArtifactException(new ArtifactResolutionException("Resolution interrupted", rootArtifact, e));
        }
    }
    // have been resolved.
    if (request.isResolveRoot()) {
        // Add the root artifact (as the first artifact to retain logical order of class path!)
        Set<Artifact> allArtifacts = new LinkedHashSet<Artifact>();
        allArtifacts.add(rootArtifact);
        allArtifacts.addAll(result.getArtifacts());
        result.setArtifacts(allArtifacts);
    }
    return result;
}
Also used : DefaultMetadataResolutionRequest(org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest) MetadataResolutionRequest(org.apache.maven.repository.legacy.metadata.MetadataResolutionRequest) LinkedHashSet(java.util.LinkedHashSet) ArtifactMetadataRetrievalException(org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException) ArrayList(java.util.ArrayList) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) LinkedHashMap(java.util.LinkedHashMap) DefaultMetadataResolutionRequest(org.apache.maven.repository.legacy.metadata.DefaultMetadataResolutionRequest) AbstractArtifactResolutionException(org.apache.maven.artifact.resolver.AbstractArtifactResolutionException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) DebugResolutionListener(org.apache.maven.artifact.resolver.DebugResolutionListener) ArtifactResolutionRequest(org.apache.maven.artifact.resolver.ArtifactResolutionRequest) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) ResolutionListener(org.apache.maven.artifact.resolver.ResolutionListener) DebugResolutionListener(org.apache.maven.artifact.resolver.DebugResolutionListener) WarningResolutionListener(org.apache.maven.artifact.resolver.WarningResolutionListener) RepositorySystemSession(org.eclipse.aether.RepositorySystemSession) ResolutionNode(org.apache.maven.artifact.resolver.ResolutionNode) ResolutionGroup(org.apache.maven.artifact.metadata.ResolutionGroup) CountDownLatch(java.util.concurrent.CountDownLatch) Artifact(org.apache.maven.artifact.Artifact) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolutionResult(org.apache.maven.artifact.resolver.ArtifactResolutionResult) WarningResolutionListener(org.apache.maven.artifact.resolver.WarningResolutionListener) ArtifactMetadataSource(org.apache.maven.artifact.metadata.ArtifactMetadataSource)

Example 2 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project intellij-community by JetBrains.

the class Maven3ServerEmbedderImpl method resolveTransitively.

@NotNull
@Override
public List<MavenArtifact> resolveTransitively(@NotNull List<MavenArtifactInfo> artifacts, @NotNull List<MavenRemoteRepository> remoteRepositories) throws RemoteException, MavenServerProcessCanceledException {
    try {
        Set<Artifact> toResolve = new LinkedHashSet<Artifact>();
        for (MavenArtifactInfo each : artifacts) {
            toResolve.add(createArtifact(each));
        }
        Artifact project = getComponent(ArtifactFactory.class).createBuildArtifact("temp", "temp", "666", "pom");
        Set<Artifact> res = getComponent(ArtifactResolver.class).resolveTransitively(toResolve, project, Collections.EMPTY_MAP, myLocalRepository, convertRepositories(remoteRepositories), getComponent(ArtifactMetadataSource.class)).getArtifacts();
        return MavenModelConverter.convertArtifacts(res, new THashMap<Artifact, MavenArtifact>(), getLocalRepositoryFile());
    } catch (ArtifactResolutionException e) {
        Maven3ServerGlobals.getLogger().info(e);
    } catch (ArtifactNotFoundException e) {
        Maven3ServerGlobals.getLogger().info(e);
    } catch (Exception e) {
        throw rethrowException(e);
    }
    return Collections.emptyList();
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) ArtifactFactory(org.apache.maven.artifact.factory.ArtifactFactory) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) Artifact(org.apache.maven.artifact.Artifact) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) InitializationException(org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException) ModelInterpolationException(org.apache.maven.project.interpolation.ModelInterpolationException) RemoteException(java.rmi.RemoteException) ComponentLookupException(org.codehaus.plexus.component.repository.exception.ComponentLookupException) ContextException(org.codehaus.plexus.context.ContextException) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) InvalidRepositoryException(org.apache.maven.artifact.InvalidRepositoryException) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project maven-plugins by apache.

the class PdfMojo method getMavenReport.

/**
     * @param mojoDescriptor not null
     * @return the MavenReport instance for the given mojoDescriptor.
     * @throws MojoExecutionException if any
     * @since 1.1
     */
private MavenReport getMavenReport(MojoDescriptor mojoDescriptor) throws MojoExecutionException {
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(mojoDescriptor.getPluginDescriptor().getClassRealm().getClassLoader());
        MojoExecution mojoExecution = new MojoExecution(mojoDescriptor);
        return pluginManager.getReport(project, mojoExecution, session);
    } catch (ArtifactNotFoundException e) {
        throw new MojoExecutionException("ArtifactNotFoundException: " + e.getMessage(), e);
    } catch (ArtifactResolutionException e) {
        throw new MojoExecutionException("ArtifactResolutionException: " + e.getMessage(), e);
    } catch (PluginConfigurationException e) {
        throw new MojoExecutionException("PluginConfigurationException: " + e.getMessage(), e);
    } catch (PluginManagerException e) {
        throw new MojoExecutionException("PluginManagerException: " + e.getMessage(), e);
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
    }
}
Also used : ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoExecution(org.apache.maven.plugin.MojoExecution) PluginConfigurationException(org.apache.maven.plugin.PluginConfigurationException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) PluginManagerException(org.apache.maven.plugin.PluginManagerException)

Example 4 with ArtifactResolutionException

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<Artifact>(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 = null;
    try {
        dirs = resolveAndUnpack(toResolve, config, RESOURCE_VALID_CLASSIFIERS, false);
    } 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<JavadocBundle>();
    if (dirs != null) {
        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()) {
                FileInputStream reader = null;
                try {
                    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;
                } finally {
                    close(reader);
                }
            }
            result.add(new JavadocBundle(options, resources));
        }
    }
    return result;
}
Also used : ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) FileInputStream(java.io.FileInputStream) JavadocOptionsXpp3Reader(org.apache.maven.plugin.javadoc.options.io.xpp3.JavadocOptionsXpp3Reader) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) JavadocOptions(org.apache.maven.plugin.javadoc.options.JavadocOptions) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) ArtifactNotFoundException(org.apache.maven.artifact.resolver.ArtifactNotFoundException) File(java.io.File)

Example 5 with ArtifactResolutionException

use of org.apache.maven.artifact.resolver.ArtifactResolutionException in project maven-plugins by apache.

the class ResourceResolver method resolveAndUnpack.

private List<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<Artifact>(artifacts);
    final Artifact pomArtifact = config.project().getArtifact();
    final ArtifactRepository localRepo = config.localRepository();
    final List<ArtifactRepository> remoteRepos = config.project().getRemoteArtifactRepositories();
    final ArtifactFilter filter;
    if (config.filter() != null) {
        filter = new ArtifactIncludeFilterTransformer().transform(config.filter());
    } else {
        filter = null;
    }
    ArtifactFilter resolutionFilter = null;
    if (filter != null) {
        // Wrap the filter in a ProjectArtifactFilter in order to always include the pomArtifact for resolution.
        // NOTE that this is necessary, b/c the -sources artifacts are added dynamically to the pomArtifact
        // and the resolver also checks the dependency trail with the given filter, thus the pomArtifact has
        // to be explicitly included by the filter, otherwise the -sources artifacts won't be resolved.
        resolutionFilter = new ProjectArtifactFilter(pomArtifact, filter);
    }
    Map<String, Artifact> managed = config.project().getManagedVersionMap();
    final ArtifactResolutionResult resolutionResult = resolver.resolveTransitively(artifactSet, pomArtifact, managed, localRepo, remoteRepos, artifactMetadataSource, resolutionFilter);
    final List<String> result = new ArrayList<String>(artifacts.size());
    for (final Artifact a : (Collection<Artifact>) resolutionResult.getArtifacts()) {
        if (!validClassifiers.contains(a.getClassifier()) || (filter != null && !filter.include(a))) {
            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(a.getFile());
            unArchiver.extract();
            result.add(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;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) ArtifactIncludeFilterTransformer(org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer) ArrayList(java.util.ArrayList) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ArtifactFilter(org.apache.maven.artifact.resolver.filter.ArtifactFilter) ArtifactResolutionException(org.apache.maven.artifact.resolver.ArtifactResolutionException) ArtifactResolutionResult(org.apache.maven.artifact.resolver.ArtifactResolutionResult) Collection(java.util.Collection) File(java.io.File) UnArchiver(org.codehaus.plexus.archiver.UnArchiver) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Aggregations

ArtifactResolutionException (org.apache.maven.artifact.resolver.ArtifactResolutionException)35 Artifact (org.apache.maven.artifact.Artifact)27 ArtifactNotFoundException (org.apache.maven.artifact.resolver.ArtifactNotFoundException)27 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)15 ArrayList (java.util.ArrayList)12 File (java.io.File)11 IOException (java.io.IOException)9 ArtifactIncludeFilterTransformer (org.apache.maven.shared.artifact.filter.resolve.transform.ArtifactIncludeFilterTransformer)7 ArtifactFilter (org.apache.maven.artifact.resolver.filter.ArtifactFilter)6 ArtifactResolutionResult (org.apache.maven.artifact.resolver.ArtifactResolutionResult)5 MojoFailureException (org.apache.maven.plugin.MojoFailureException)5 TransformableFilter (org.apache.maven.shared.artifact.filter.resolve.TransformableFilter)5 DefaultArtifact (org.apache.maven.artifact.DefaultArtifact)4 VersionRange (org.apache.maven.artifact.versioning.VersionRange)4 Dependency (org.apache.maven.model.Dependency)4 HashMap (java.util.HashMap)3 LinkedHashSet (java.util.LinkedHashSet)3 ArtifactMetadataRetrievalException (org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException)3 MavenProject (org.apache.maven.project.MavenProject)3 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)3