Search in sources :

Example 1 with IvyContext

use of org.apache.ivy.core.IvyContext in project ant-ivy by apache.

the class BasicResolver method findResource.

/**
 * When the resolver has many choices, this function helps choosing one
 *
 * @param rress
 *            the list of resolved resource which the resolver found to fit the requirement
 * @param rmdparser
 *            the parser of module descriptor
 * @param mrid
 *            the module being resolved
 * @param date
 *            the current date
 * @return the selected resource
 */
public ResolvedResource findResource(ResolvedResource[] rress, ResourceMDParser rmdparser, ModuleRevisionId mrid, Date date) {
    String name = getName();
    VersionMatcher versionMatcher = getSettings().getVersionMatcher();
    ResolvedResource found = null;
    List<ArtifactInfo> sorted = getLatestStrategy().sort(rress);
    List<String> rejected = new ArrayList<>();
    List<ModuleRevisionId> foundBlacklisted = new ArrayList<>();
    IvyContext context = IvyContext.getContext();
    ListIterator<ArtifactInfo> iter = sorted.listIterator(sorted.size());
    while (iter.hasPrevious()) {
        ResolvedResource rres = (ResolvedResource) iter.previous();
        // name, blacklisting and first level version matching
        if (filterNames(new ArrayList<>(Collections.singleton(rres.getRevision()))).isEmpty()) {
            Message.debug("\t" + name + ": filtered by name: " + rres);
            continue;
        }
        ModuleRevisionId foundMrid = ModuleRevisionId.newInstance(mrid, rres.getRevision());
        ResolveData data = context.getResolveData();
        if (data != null && data.getReport() != null && data.isBlacklisted(data.getReport().getConfiguration(), foundMrid)) {
            Message.debug("\t" + name + ": blacklisted: " + rres);
            rejected.add(rres.getRevision() + " (blacklisted)");
            foundBlacklisted.add(foundMrid);
            continue;
        }
        if (!versionMatcher.accept(mrid, foundMrid)) {
            Message.debug("\t" + name + ": rejected by version matcher: " + rres);
            rejected.add(rres.getRevision());
            continue;
        }
        if (rres.getResource() != null && !rres.getResource().exists()) {
            Message.debug("\t" + name + ": unreachable: " + rres + "; res=" + rres.getResource());
            rejected.add(rres.getRevision() + " (unreachable)");
            continue;
        }
        if (date != null && rres.getLastModified() > date.getTime()) {
            Message.verbose("\t" + name + ": too young: " + rres);
            rejected.add(rres.getRevision() + " (" + rres.getLastModified() + ")");
            continue;
        }
        if (versionMatcher.needModuleDescriptor(mrid, foundMrid)) {
            ResolvedResource r = rmdparser.parse(rres.getResource(), rres.getRevision());
            if (r == null) {
                Message.debug("\t" + name + ": impossible to get module descriptor resource: " + rres);
                rejected.add(rres.getRevision() + " (no or bad MD)");
                continue;
            }
            ModuleDescriptor md = ((MDResolvedResource) r).getResolvedModuleRevision().getDescriptor();
            if (md.isDefault()) {
                Message.debug("\t" + name + ": default md rejected by version matcher" + "requiring module descriptor: " + rres);
                rejected.add(rres.getRevision() + " (MD)");
                continue;
            }
            if (!versionMatcher.accept(mrid, md)) {
                Message.debug("\t" + name + ": md rejected by version matcher: " + rres);
                rejected.add(rres.getRevision() + " (MD)");
                continue;
            }
            found = r;
        } else {
            found = rres;
        }
        if (found != null) {
            break;
        }
    }
    if (found == null && !rejected.isEmpty()) {
        logAttempt(rejected.toString());
    }
    if (found == null && !foundBlacklisted.isEmpty()) {
        // all acceptable versions have been blacklisted, this means that an unsolvable conflict
        // has been found
        DependencyDescriptor dd = context.getDependencyDescriptor();
        IvyNode parentNode = context.getResolveData().getNode(dd.getParentRevisionId());
        ConflictManager cm = parentNode.getConflictManager(mrid.getModuleId());
        cm.handleAllBlacklistedRevisions(dd, foundBlacklisted);
    }
    return found;
}
Also used : DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) ArrayList(java.util.ArrayList) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolveData(org.apache.ivy.core.resolve.ResolveData) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) MDResolvedResource(org.apache.ivy.plugins.resolver.util.MDResolvedResource) ArtifactInfo(org.apache.ivy.plugins.latest.ArtifactInfo) VersionMatcher(org.apache.ivy.plugins.version.VersionMatcher) IvyContext(org.apache.ivy.core.IvyContext) ConflictManager(org.apache.ivy.plugins.conflict.ConflictManager) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 2 with IvyContext

use of org.apache.ivy.core.IvyContext in project ant-ivy by apache.

the class BasicResolver method getDependency.

public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException {
    IvyContext context = IvyContext.pushNewCopyContext();
    try {
        ResolvedModuleRevision mr = data.getCurrentResolvedModuleRevision();
        if (mr != null && shouldReturnResolvedModule(dd, mr)) {
            return mr;
        }
        if (isForce()) {
            dd = dd.clone(ModuleRevisionId.newInstance(dd.getDependencyRevisionId(), "latest.integration"));
        }
        DependencyDescriptor systemDd = dd;
        DependencyDescriptor nsDd = fromSystem(dd);
        context.setDependencyDescriptor(systemDd);
        context.setResolveData(data);
        clearIvyAttempts();
        clearArtifactAttempts();
        ModuleRevisionId systemMrid = systemDd.getDependencyRevisionId();
        ModuleRevisionId nsMrid = nsDd.getDependencyRevisionId();
        checkRevision(systemMrid);
        boolean isDynamic = getAndCheckIsDynamic(systemMrid);
        // we first search for the dependency in cache
        ResolvedModuleRevision rmr = findModuleInCache(systemDd, data);
        if (rmr != null) {
            if (rmr.getDescriptor().isDefault() && rmr.getResolver() != this) {
                Message.verbose("\t" + getName() + ": found revision in cache: " + systemMrid + " (resolved by " + rmr.getResolver().getName() + "): but it's a default one, maybe we can find a better one");
            } else if (isForce() && rmr.getResolver() != this) {
                Message.verbose("\t" + getName() + ": found revision in cache: " + systemMrid + " (resolved by " + rmr.getResolver().getName() + "): but we are in force mode, let's try to find one ourselves");
            } else {
                Message.verbose("\t" + getName() + ": revision in cache: " + systemMrid);
                return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
            }
        }
        if (data.getOptions().isUseCacheOnly()) {
            throw new UnresolvedDependencyException("\t" + getName() + " (useCacheOnly) : no ivy file found for " + systemMrid, false);
        }
        checkInterrupted();
        ResolvedResource ivyRef = findIvyFileRef(nsDd, data);
        checkInterrupted();
        // get module descriptor
        ModuleDescriptor nsMd;
        ModuleDescriptor systemMd = null;
        if (ivyRef == null) {
            if (!isAllownomd()) {
                throw new UnresolvedDependencyException("\t" + getName() + ": no ivy file found for " + systemMrid, false);
            }
            nsMd = DefaultModuleDescriptor.newDefaultInstance(nsMrid, nsDd.getAllDependencyArtifacts());
            ResolvedResource artifactRef = findFirstArtifactRef(nsMd, nsDd, data);
            checkInterrupted();
            if (artifactRef == null) {
                throw new UnresolvedDependencyException("\t" + getName() + ": no ivy file nor artifact found for " + systemMrid, false);
            }
            long lastModified = artifactRef.getLastModified();
            if (lastModified != 0 && nsMd instanceof DefaultModuleDescriptor) {
                ((DefaultModuleDescriptor) nsMd).setLastModified(lastModified);
            }
            Message.verbose("\t" + getName() + ": no ivy file found for " + systemMrid + ": using default data");
            if (isDynamic) {
                nsMd.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(nsMrid, artifactRef.getRevision()));
            }
            systemMd = toSystem(nsMd);
            MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(systemMd.getMetadataArtifact());
            madr.setDownloadStatus(DownloadStatus.NO);
            madr.setSearched(true);
            rmr = new ResolvedModuleRevision(this, this, systemMd, madr, isForce());
            getRepositoryCacheManager().cacheModuleDescriptor(this, artifactRef, toSystem(dd), systemMd.getAllArtifacts()[0], null, getCacheOptions(data));
        } else {
            if (ivyRef instanceof MDResolvedResource) {
                rmr = ((MDResolvedResource) ivyRef).getResolvedModuleRevision();
            }
            if (rmr == null) {
                rmr = parse(ivyRef, systemDd, data);
                if (rmr == null) {
                    throw new UnresolvedDependencyException();
                }
            }
            if (!rmr.getReport().isDownloaded() && rmr.getReport().getLocalFile() != null) {
                return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
            }
            nsMd = rmr.getDescriptor();
            // check descriptor data is in sync with resource revision and names
            systemMd = toSystem(nsMd);
            if (isCheckconsistency()) {
                checkDescriptorConsistency(systemMrid, systemMd, ivyRef);
                checkDescriptorConsistency(nsMrid, nsMd, ivyRef);
            } else {
                if (systemMd instanceof DefaultModuleDescriptor) {
                    DefaultModuleDescriptor defaultMd = (DefaultModuleDescriptor) systemMd;
                    ModuleRevisionId revision = getRevision(ivyRef, systemMrid, systemMd);
                    defaultMd.setModuleRevisionId(revision);
                    defaultMd.setResolvedModuleRevisionId(revision);
                } else {
                    Message.warn("consistency disabled with instance of non DefaultModuleDescriptor... module info can't be updated, so consistency check will be done");
                    checkDescriptorConsistency(nsMrid, nsMd, ivyRef);
                    checkDescriptorConsistency(systemMrid, systemMd, ivyRef);
                }
            }
            rmr = new ResolvedModuleRevision(this, this, systemMd, toSystem(rmr.getReport()), isForce());
        }
        resolveAndCheckRevision(systemMd, systemMrid, ivyRef, isDynamic);
        resolveAndCheckPublicationDate(systemDd, systemMd, systemMrid, data);
        checkNotConvertedExclusionRule(systemMd, ivyRef, data);
        if (ivyRef == null || ivyRef.getResource() != null) {
            cacheModuleDescriptor(systemMd, systemMrid, ivyRef, rmr);
        }
        return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
    } catch (UnresolvedDependencyException ex) {
        if (!ex.getMessage().isEmpty()) {
            if (ex.isError()) {
                Message.error(ex.getMessage());
            } else {
                Message.verbose(ex.getMessage());
            }
        }
        return data.getCurrentResolvedModuleRevision();
    } finally {
        IvyContext.popContext();
    }
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) MDResolvedResource(org.apache.ivy.plugins.resolver.util.MDResolvedResource) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) IvyContext(org.apache.ivy.core.IvyContext) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) MDResolvedResource(org.apache.ivy.plugins.resolver.util.MDResolvedResource)

Example 3 with IvyContext

use of org.apache.ivy.core.IvyContext in project ant-ivy by apache.

the class PomModuleDescriptorParser method parseDescriptor.

public ModuleDescriptor parseDescriptor(ParserSettings ivySettings, URL descriptorURL, Resource res, boolean validate) throws ParseException, IOException {
    PomModuleDescriptorBuilder mdBuilder = new PomModuleDescriptorBuilder(this, res, ivySettings);
    try {
        final IvyContext ivyContext = IvyContext.pushNewCopyContext();
        HashSet<ModuleRevisionId> parents = ivyContext.get(PARENT_MAP_KEY);
        if (parents == null) {
            parents = new LinkedHashSet<>();
            ivyContext.set(PARENT_MAP_KEY, parents);
        }
        PomReader domReader = new PomReader(descriptorURL, res);
        domReader.setProperty("parent.version", domReader.getParentVersion());
        domReader.setProperty("parent.groupId", domReader.getParentGroupId());
        domReader.setProperty("project.parent.version", domReader.getParentVersion());
        domReader.setProperty("project.parent.groupId", domReader.getParentGroupId());
        Message.debug("parent.groupId: " + domReader.getParentGroupId());
        Message.debug("parent.artifactId: " + domReader.getParentArtifactId());
        Message.debug("parent.version: " + domReader.getParentVersion());
        for (final Map.Entry<String, String> prop : domReader.getPomProperties().entrySet()) {
            domReader.setProperty(prop.getKey(), prop.getValue());
            mdBuilder.addProperty(prop.getKey(), prop.getValue());
        }
        final List<PomProfileElement> activeProfiles = new ArrayList<>();
        // add profile specific properties
        for (final PomProfileElement profile : domReader.getProfiles()) {
            if (!profile.isActive()) {
                continue;
            }
            // keep track of this active profile for later use
            activeProfiles.add(profile);
            final Map<String, String> profileProps = profile.getProfileProperties();
            if (profileProps.isEmpty()) {
                continue;
            }
            for (final Map.Entry<String, String> entry : profileProps.entrySet()) {
                domReader.setProperty(entry.getKey(), entry.getValue());
                mdBuilder.addProperty(entry.getKey(), entry.getValue());
            }
        }
        ModuleDescriptor parentDescr = null;
        if (domReader.hasParent()) {
            // Is there any other parent properties?
            ModuleRevisionId parentModRevID = ModuleRevisionId.newInstance(domReader.getParentGroupId(), domReader.getParentArtifactId(), domReader.getParentVersion());
            // check for cycles
            if (parents.contains(parentModRevID)) {
                throw new CircularDependencyException(parents);
            } else {
                parents.add(parentModRevID);
            }
            ResolvedModuleRevision parentModule = parseOtherPom(ivySettings, parentModRevID);
            if (parentModule == null) {
                throw new IOException("Impossible to load parent for " + res.getName() + ". Parent=" + parentModRevID);
            }
            parentDescr = parentModule.getDescriptor();
            if (parentDescr != null) {
                for (Map.Entry<String, String> prop : extractPomProperties(parentDescr.getExtraInfos()).entrySet()) {
                    domReader.setProperty(prop.getKey(), prop.getValue());
                }
            }
        }
        String groupId = domReader.getGroupId();
        String artifactId = domReader.getArtifactId();
        String version = domReader.getVersion();
        mdBuilder.setModuleRevId(groupId, artifactId, version);
        mdBuilder.setHomePage(domReader.getHomePage());
        mdBuilder.setDescription(domReader.getDescription());
        // if this module doesn't have an explicit license, use the parent's license (if any)
        final License[] licenses = domReader.getLicenses();
        if (licenses != null && licenses.length > 0) {
            mdBuilder.setLicenses(licenses);
        } else if (parentDescr != null) {
            mdBuilder.setLicenses(parentDescr.getLicenses());
        }
        ModuleRevisionId relocation = domReader.getRelocation();
        if (relocation != null) {
            if (groupId != null && artifactId != null && artifactId.equals(relocation.getName()) && groupId.equals(relocation.getOrganisation())) {
                Message.error("Relocation to an other version number not supported in ivy : " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " relocated to " + relocation + ". Please update your dependency to directly use the right version.");
                Message.warn("Resolution will only pick dependencies of the relocated element." + "  Artifact and other metadata will be ignored.");
                ResolvedModuleRevision relocatedModule = parseOtherPom(ivySettings, relocation);
                if (relocatedModule == null) {
                    throw new ParseException("impossible to load module " + relocation + " to which " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " has been relocated", 0);
                }
                for (DependencyDescriptor dd : relocatedModule.getDescriptor().getDependencies()) {
                    mdBuilder.addDependency(dd);
                }
            } else {
                Message.info(mdBuilder.getModuleDescriptor().getModuleRevisionId() + " is relocated to " + relocation + ". Please update your dependencies.");
                Message.verbose("Relocated module will be considered as a dependency");
                DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(mdBuilder.getModuleDescriptor(), relocation, true, false, true);
                /* Map all public dependencies */
                for (Configuration m2Conf : MAVEN2_CONFIGURATIONS) {
                    if (PUBLIC.equals(m2Conf.getVisibility())) {
                        dd.addDependencyConfiguration(m2Conf.getName(), m2Conf.getName());
                    }
                }
                mdBuilder.addDependency(dd);
            }
        } else {
            domReader.setProperty("project.groupId", groupId);
            domReader.setProperty("pom.groupId", groupId);
            domReader.setProperty("groupId", groupId);
            domReader.setProperty("project.artifactId", artifactId);
            domReader.setProperty("pom.artifactId", artifactId);
            domReader.setProperty("artifactId", artifactId);
            domReader.setProperty("project.version", version);
            domReader.setProperty("pom.version", version);
            domReader.setProperty("version", version);
            if (parentDescr != null) {
                mdBuilder.addExtraInfos(parentDescr.getExtraInfos());
                // add dependency management info from parent
                for (PomDependencyMgt dep : getDependencyManagements(parentDescr)) {
                    if (dep instanceof PomDependencyMgtElement) {
                        dep = domReader.new PomDependencyMgtElement((PomDependencyMgtElement) dep);
                    }
                    mdBuilder.addDependencyMgt(dep);
                }
                // add plugins from parent
                for (PomDependencyMgt pomDependencyMgt : getPlugins(parentDescr)) {
                    mdBuilder.addPlugin(pomDependencyMgt);
                }
            }
            for (PomDependencyMgt dep : domReader.getDependencyMgt()) {
                addTo(mdBuilder, dep, ivySettings);
            }
            for (PomDependencyData dep : domReader.getDependencies()) {
                mdBuilder.addDependency(res, dep);
            }
            for (PomPluginElement plugin : domReader.getPlugins()) {
                mdBuilder.addPlugin(plugin);
            }
            // consult active profiles:
            for (final PomProfileElement activeProfile : activeProfiles) {
                for (PomDependencyMgt dep : activeProfile.getDependencyMgt()) {
                    addTo(mdBuilder, dep, ivySettings);
                }
                for (PomDependencyData dep : activeProfile.getDependencies()) {
                    mdBuilder.addDependency(res, dep);
                }
                for (PomPluginElement plugin : activeProfile.getPlugins()) {
                    mdBuilder.addPlugin(plugin);
                }
            }
            if (parentDescr != null) {
                for (DependencyDescriptor descriptor : parentDescr.getDependencies()) {
                    if (descriptor instanceof PomDependencyDescriptor) {
                        PomDependencyData parentDep = ((PomDependencyDescriptor) descriptor).getPomDependencyData();
                        PomDependencyData dep = domReader.new PomDependencyData(parentDep);
                        mdBuilder.addDependency(res, dep);
                    } else {
                        mdBuilder.addDependency(descriptor);
                    }
                }
            }
            mdBuilder.addMainArtifact(artifactId, domReader.getPackaging());
            addSourcesAndJavadocArtifactsIfPresent(mdBuilder, ivySettings);
        }
    } catch (SAXException e) {
        throw newParserException(e);
    } finally {
        IvyContext.popContext();
    }
    return mdBuilder.getModuleDescriptor();
}
Also used : Configuration(org.apache.ivy.core.module.descriptor.Configuration) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) PomDependencyDescriptor(org.apache.ivy.plugins.parser.m2.PomModuleDescriptorBuilder.PomDependencyDescriptor) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) ArrayList(java.util.ArrayList) License(org.apache.ivy.core.module.descriptor.License) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) SAXException(org.xml.sax.SAXException) PomDependencyMgtElement(org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyMgtElement) IvyContext(org.apache.ivy.core.IvyContext) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) PomProfileElement(org.apache.ivy.plugins.parser.m2.PomReader.PomProfileElement) CircularDependencyException(org.apache.ivy.plugins.circular.CircularDependencyException) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) IOException(java.io.IOException) PomPluginElement(org.apache.ivy.plugins.parser.m2.PomReader.PomPluginElement) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) PomDependencyDescriptor(org.apache.ivy.plugins.parser.m2.PomModuleDescriptorBuilder.PomDependencyDescriptor) PomDependencyData(org.apache.ivy.plugins.parser.m2.PomReader.PomDependencyData) ParseException(java.text.ParseException) Map(java.util.Map)

Example 4 with IvyContext

use of org.apache.ivy.core.IvyContext in project ant-ivy by apache.

the class ResolveEngine method computeConflicts.

/**
 * Compute possible conflicts for a node, in the context of an ancestor (a node which has a
 * dependency - direct or indirect - on the node for which conflicts should be computed.
 *
 * @param node
 *            the node for which conflicts should be computed
 * @param ancestor
 *            the ancestor in which conflicts should be computed
 * @param conf
 *            the configuration of the node in which conflicts should be computed
 * @param toevict
 *            a collection of nodes which have been evicted during conflict resolution at lower
 *            level. It may be empty if no conflict resolution has occurred for this node yet,
 *            or if no node has been evicted.
 * @param selectedNodes
 *            a collection of nodes selected during previous conflict resolution for the given
 *            node and ancestor. This collection is updated by this call, removing nodes which
 *            should be evicted.
 * @return a collection of IvyNode which may be in conflict with the given node in the given
 *         ancestor. This collection always contain at least the given node.
 */
private Collection<IvyNode> computeConflicts(VisitNode node, VisitNode ancestor, String conf, Collection<IvyNode> toevict, Collection<IvyNode> selectedNodes) {
    Collection<IvyNode> conflicts = new LinkedHashSet<>();
    conflicts.add(node.getNode());
    /*
         * We first try to remove all evicted nodes from the collection of selected nodes to update
         * this collection. If the collection changes, it means that it contained evicted nodes, and
         * thus is not up to date.
         */
    boolean evictedInSelected = selectedNodes.removeAll(toevict);
    /*
         * Another case where we need to deeply compute selected nodes is when selectedNodes is
         * empty (not computed yet) and we aren't in the context of the direct parent of the node.
         */
    if (evictedInSelected || (selectedNodes.isEmpty() && !node.getParent().getNode().equals(ancestor.getNode()))) {
        IvyContext context = IvyContext.getContext();
        ResolveData data = context.getResolveData();
        VisitNode oldVisitNode = data.getCurrentVisitNode();
        data.setCurrentVisitNode(ancestor);
        try {
            // In this case we need to compute selected nodes again.
            Collection<IvyNode> deps = ancestor.getNode().getDependencies(node.getRootModuleConf(), ancestor.getNode().getConfigurations(node.getRootModuleConf()), ancestor.getRequestedConf());
            for (IvyNode dep : deps) {
                if (dep.getModuleId().equals(node.getModuleId())) {
                    conflicts.add(dep);
                }
                conflicts.addAll(dep.getResolvedNodes(node.getModuleId(), node.getRootModuleConf()));
            }
        } finally {
            data.setCurrentVisitNode(oldVisitNode);
        }
    } else if (selectedNodes.isEmpty()) {
        /*
             * No selected nodes at all yet, and we are in the context of the direct parent
             * (otherwise previous block would have been reached). We can compute conflicts based on
             * the parent direct dependencies in current root module conf.
             */
        VisitNode parent = node.getParent();
        Collection<IvyNode> parentDepIvyNodes = parent.getNode().getDependencies(node.getRootModuleConf(), parent.getNode().getConfigurations(node.getRootModuleConf()), parent.getRequestedConf());
        for (IvyNode parentDep : parentDepIvyNodes) {
            if (parentDep.getModuleId().equals(node.getModuleId())) {
                conflicts.add(parentDep);
            }
        }
    } else {
        conflicts.addAll(selectedNodes);
    }
    return conflicts;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) IvyContext(org.apache.ivy.core.IvyContext) Collection(java.util.Collection)

Example 5 with IvyContext

use of org.apache.ivy.core.IvyContext in project ant-ivy by apache.

the class ResolveEngine method getDependencies.

/**
 * Resolve the dependencies of a module without downloading corresponding artifacts. The module
 * to resolve is given by its module descriptor. This method requires appropriate configuration
 * of the ivy instance, especially resolvers.
 * <p>
 * The <code>IvyNode</code>s are ordered from the most dependent to the less dependent, so that
 * an IvyNode is always found in the list after all IvyNode depending directly on it.
 *
 * @param md
 *            the descriptor of the module for which we want to get dependencies - must not be
 *            null
 * @param options
 *            the resolve options to use to resolve the dependencies
 * @param report
 *            a resolve report to fill during resolution - may be null
 * @return an array of the resolved Dependencies
 */
public IvyNode[] getDependencies(ModuleDescriptor md, ResolveOptions options, ResolveReport report) {
    // check parameters
    if (md == null) {
        throw new NullPointerException("module descriptor must not be null");
    }
    String[] confs = options.getConfs(md);
    Collection<String> missingConfs = new ArrayList<>();
    for (String conf : confs) {
        if (conf == null) {
            throw new NullPointerException("null conf not allowed: confs where: " + Arrays.asList(confs));
        }
        if (md.getConfiguration(conf) == null) {
            missingConfs.add(" '" + conf + "' ");
        }
    }
    if (!missingConfs.isEmpty()) {
        throw new IllegalArgumentException("requested configuration" + (missingConfs.size() > 1 ? "s" : "") + " not found in " + md.getModuleRevisionId() + ": " + missingConfs);
    }
    IvyContext context = IvyContext.pushNewCopyContext();
    try {
        options.setConfs(confs);
        Date reportDate = new Date();
        ResolveData data = context.getResolveData();
        if (data == null) {
            data = new ResolveData(this, options);
            context.setResolveData(data);
        }
        IvyNode rootNode = new IvyNode(data, md);
        for (String conf : confs) {
            Message.verbose("resolving dependencies for configuration '" + conf + "'");
            // for each configuration we clear the cache of what's been fetched
            fetchedSet.clear();
            ConfigurationResolveReport confReport = null;
            if (report != null) {
                confReport = report.getConfigurationReport(conf);
                if (confReport == null) {
                    confReport = new ConfigurationResolveReport(this, md, conf, reportDate, options);
                    report.addReport(conf, confReport);
                }
            }
            // we reuse the same resolve data with a new report for each conf
            data.setReport(confReport);
            // update the root module conf we are about to fetch
            VisitNode root = new VisitNode(data, rootNode, null, conf, null);
            root.setRequestedConf(conf);
            rootNode.updateConfsToFetch(Collections.singleton(conf));
            // go fetch !
            boolean fetched = false;
            while (!fetched) {
                try {
                    fetchDependencies(root, conf, false);
                    fetched = true;
                } catch (RestartResolveProcess restart) {
                    Message.verbose("====================================================");
                    Message.verbose("=           RESTARTING RESOLVE PROCESS");
                    Message.verbose("= " + restart.getMessage());
                    Message.verbose("====================================================");
                    fetchedSet.clear();
                }
            }
            // clean data
            for (IvyNode dep : data.getNodes()) {
                dep.clean();
            }
        }
        // prune and reverse sort fetched dependencies
        Collection<IvyNode> nodes = data.getNodes();
        // use a Set to avoid duplicates, linked to preserve order
        Collection<IvyNode> dependencies = new LinkedHashSet<>(nodes.size());
        for (IvyNode node : nodes) {
            if (node != null && !node.isRoot() && !node.isCompletelyBlacklisted()) {
                dependencies.add(node);
            }
        }
        List<IvyNode> sortedDependencies = sortEngine.sortNodes(dependencies, SortOptions.SILENT);
        Collections.reverse(sortedDependencies);
        handleTransitiveEviction(md, confs, data, sortedDependencies);
        return dependencies.toArray(new IvyNode[dependencies.size()]);
    } finally {
        IvyContext.popContext();
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArrayList(java.util.ArrayList) Date(java.util.Date) IvyContext(org.apache.ivy.core.IvyContext) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport)

Aggregations

IvyContext (org.apache.ivy.core.IvyContext)6 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)4 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)4 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)4 ArrayList (java.util.ArrayList)3 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)3 LinkedHashSet (java.util.LinkedHashSet)2 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)2 ConfigurationResolveReport (org.apache.ivy.core.report.ConfigurationResolveReport)2 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)2 MDResolvedResource (org.apache.ivy.plugins.resolver.util.MDResolvedResource)2 ResolvedResource (org.apache.ivy.plugins.resolver.util.ResolvedResource)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1