Search in sources :

Example 1 with VersionMatcher

use of org.apache.ivy.plugins.version.VersionMatcher 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 VersionMatcher

use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.

the class AbstractWorkspaceResolver method checkCandidate.

protected ResolvedModuleRevision checkCandidate(DependencyDescriptor dd, ModuleDescriptor md, String workspaceModuleName) {
    if (workspaceModuleName == null) {
        workspaceModuleName = dd.getDependencyId().toString();
    }
    ModuleRevisionId dependencyMrid = dd.getDependencyRevisionId();
    String org = dependencyMrid.getModuleId().getOrganisation();
    String module = dependencyMrid.getModuleId().getName();
    VersionMatcher versionMatcher = getSettings().getVersionMatcher();
    ModuleRevisionId candidateMrid = md.getModuleRevisionId();
    switch(org) {
        case BundleInfo.BUNDLE_TYPE:
            // looking for an OSGi bundle via its symbolic name
            String sn = md.getExtraInfoContentByTagName("Bundle-SymbolicName");
            if (sn == null || !module.equals(sn)) {
                // not found, skip to next
                return null;
            }
            break;
        case BundleInfo.PACKAGE_TYPE:
            // looking for an OSGi bundle via its exported package
            String exportedPackages = md.getExtraInfoContentByTagName("Export-Package");
            if (exportedPackages == null) {
                // not found, skip to next
                return null;
            }
            boolean found = false;
            String version = null;
            ManifestHeaderValue exportElements;
            try {
                exportElements = new ManifestHeaderValue(exportedPackages);
            } catch (ParseException e) {
                // wrong OSGi header: skip it
                return null;
            }
            for (ManifestHeaderElement exportElement : exportElements.getElements()) {
                if (exportElement.getValues().contains(module)) {
                    found = true;
                    version = exportElement.getAttributes().get("version");
                    break;
                }
            }
            if (!found) {
                // not found, skip to next
                return null;
            }
            if (version == null) {
                // no version means anything can match. Let's trick the version matcher by
                // setting the exact expected version
                version = dependencyMrid.getRevision();
            }
            md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(org, module, version));
            break;
        default:
            if (!candidateMrid.getModuleId().equals(dependencyMrid.getModuleId())) {
                // it doesn't match org#module, skip to next
                return null;
            }
            break;
    }
    Message.verbose("Workspace resolver found potential matching workspace module " + workspaceModuleName + " with module " + candidateMrid + " for module " + dependencyMrid);
    if (!ignoreBranch) {
        ModuleId mid = dependencyMrid.getModuleId();
        String defaultBranch = getSettings().getDefaultBranch(mid);
        String dependencyBranch = dependencyMrid.getBranch();
        String candidateBranch = candidateMrid.getBranch();
        if (dependencyBranch == null) {
            dependencyBranch = defaultBranch;
        }
        if (candidateBranch == null) {
            candidateBranch = defaultBranch;
        }
        if (dependencyBranch != candidateBranch) {
            // Both cannot be null
            if (dependencyBranch == null || candidateBranch == null) {
                Message.verbose("\t\trejected since branches doesn't match (one is set, the other isn't)");
                return null;
            }
            if (!dependencyBranch.equals(candidateBranch)) {
                Message.verbose("\t\trejected since branches doesn't match");
                return null;
            }
        }
    }
    // Found one; check if it is for the module we need
    if (!ignoreVersion && !md.getModuleRevisionId().getRevision().equals(Ivy.getWorkingRevision()) && !versionMatcher.accept(dd.getDependencyRevisionId(), md)) {
        Message.verbose("\t\treject as version didn't match");
        return null;
    }
    if (ignoreVersion) {
        Message.verbose("\t\tmatched (version are ignored)");
    } else {
        Message.verbose("\t\tversion matched");
    }
    WorkspaceModuleDescriptor workspaceMd = createWorkspaceMd(md);
    Artifact mdaf = md.getMetadataArtifact();
    if (mdaf == null) {
        mdaf = new DefaultArtifact(md.getModuleRevisionId(), md.getPublicationDate(), workspaceModuleName, "ivy", "");
    }
    MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(mdaf);
    madr.setDownloadStatus(DownloadStatus.SUCCESSFUL);
    madr.setSearched(true);
    return new ResolvedModuleRevision(this, this, workspaceMd, madr);
}
Also used : MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ManifestHeaderValue(org.apache.ivy.osgi.core.ManifestHeaderValue) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) DefaultWorkspaceModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultWorkspaceModuleDescriptor) WorkspaceModuleDescriptor(org.apache.ivy.core.module.descriptor.WorkspaceModuleDescriptor) ModuleId(org.apache.ivy.core.module.id.ModuleId) ManifestHeaderElement(org.apache.ivy.osgi.core.ManifestHeaderElement) VersionMatcher(org.apache.ivy.plugins.version.VersionMatcher) ParseException(java.text.ParseException) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact)

Example 3 with VersionMatcher

use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.

the class RepositoryResolver method findResourceUsingPattern.

@Override
protected ResolvedResource findResourceUsingPattern(ModuleRevisionId mrid, String pattern, Artifact artifact, ResourceMDParser rmdparser, Date date) {
    String name = getName();
    VersionMatcher versionMatcher = getSettings().getVersionMatcher();
    try {
        if (!versionMatcher.isDynamic(mrid) || isAlwaysCheckExactRevision()) {
            String resourceName = IvyPatternHelper.substitute(pattern, mrid, artifact);
            Message.debug("\t trying " + resourceName);
            logAttempt(resourceName);
            Resource res = repository.getResource(resourceName);
            boolean reachable = res.exists();
            if (reachable) {
                String revision;
                if (pattern.contains(IvyPatternHelper.REVISION_KEY)) {
                    revision = mrid.getRevision();
                } else {
                    if ("ivy".equals(artifact.getType()) || "pom".equals(artifact.getType())) {
                        // we can't determine the revision from the pattern, get it
                        // from the module descriptor itself
                        File temp = File.createTempFile("ivy", artifact.getExt());
                        temp.deleteOnExit();
                        repository.get(res.getName(), temp);
                        ModuleDescriptorParser parser = ModuleDescriptorParserRegistry.getInstance().getParser(res);
                        ModuleDescriptor md = parser.parseDescriptor(getParserSettings(), temp.toURI().toURL(), res, false);
                        revision = md.getRevision();
                        if (isNullOrEmpty(revision)) {
                            revision = "working@" + name;
                        }
                    } else {
                        revision = "working@" + name;
                    }
                }
                return new ResolvedResource(res, revision);
            } else if (versionMatcher.isDynamic(mrid)) {
                return findDynamicResourceUsingPattern(rmdparser, mrid, pattern, artifact, date);
            } else {
                Message.debug("\t" + name + ": resource not reachable for " + mrid + ": res=" + res);
                return null;
            }
        } else {
            return findDynamicResourceUsingPattern(rmdparser, mrid, pattern, artifact, date);
        }
    } catch (IOException | ParseException ex) {
        throw new RuntimeException(name + ": unable to get resource for " + mrid + ": res=" + IvyPatternHelper.substitute(pattern, mrid, artifact) + ": " + ex, ex);
    }
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) VersionMatcher(org.apache.ivy.plugins.version.VersionMatcher) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) IOException(java.io.IOException) ParseException(java.text.ParseException) File(java.io.File) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser)

Example 4 with VersionMatcher

use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.

the class LatestCompatibleConflictManager method resolveConflicts.

@Override
public Collection<IvyNode> resolveConflicts(IvyNode parent, Collection<IvyNode> conflicts) {
    if (conflicts.size() < 2) {
        return conflicts;
    }
    VersionMatcher versionMatcher = getSettings().getVersionMatcher();
    Iterator<IvyNode> iter = conflicts.iterator();
    IvyNode node = iter.next();
    ModuleRevisionId mrid = node.getResolvedId();
    if (versionMatcher.isDynamic(mrid)) {
        while (iter.hasNext()) {
            IvyNode other = iter.next();
            if (versionMatcher.isDynamic(other.getResolvedId()) || !versionMatcher.accept(mrid, other.getResolvedId()) && !handleIncompatibleConflict(parent, conflicts, node, other)) {
                // or incompatibility found
                return null;
            }
        }
        // no incompatibility nor dynamic version found, let's return the latest static version
        if (conflicts.size() == 2) {
            // very common special case of only two modules in conflict,
            // let's return the second one (static)
            Iterator<IvyNode> it = conflicts.iterator();
            it.next();
            return Collections.singleton(it.next());
        }
        Collection<IvyNode> newConflicts = new LinkedHashSet<>(conflicts);
        newConflicts.remove(node);
        return super.resolveConflicts(parent, newConflicts);
    } else {
        // the first node is a static revision, let's see if all other versions match
        while (iter.hasNext()) {
            IvyNode other = iter.next();
            if (!versionMatcher.accept(other.getResolvedId(), mrid) && !handleIncompatibleConflict(parent, conflicts, node, other)) {
                // incompatibility found
                return null;
            }
        }
        // no incompatibility found, let's return this static version
        return Collections.singleton(node);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VersionMatcher(org.apache.ivy.plugins.version.VersionMatcher) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 5 with VersionMatcher

use of org.apache.ivy.plugins.version.VersionMatcher in project ant-ivy by apache.

the class StrictConflictManager method resolveConflicts.

public Collection<IvyNode> resolveConflicts(IvyNode parent, Collection<IvyNode> conflicts) {
    VersionMatcher versionMatcher = getSettings().getVersionMatcher();
    IvyNode lastNode = null;
    for (IvyNode node : conflicts) {
        if (versionMatcher.isDynamic(node.getResolvedId())) {
            // dynamic revision, not enough information to resolve conflict
            return null;
        }
        if (lastNode != null && !lastNode.equals(node)) {
            throw new StrictConflictException(lastNode, node);
        }
        lastNode = node;
    }
    return Collections.singleton(lastNode);
}
Also used : VersionMatcher(org.apache.ivy.plugins.version.VersionMatcher) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Aggregations

VersionMatcher (org.apache.ivy.plugins.version.VersionMatcher)11 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)5 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)3 IvyNode (org.apache.ivy.core.resolve.IvyNode)3 ModuleDescriptorParserRegistryTest (org.apache.ivy.plugins.parser.ModuleDescriptorParserRegistryTest)3 ChainVersionMatcher (org.apache.ivy.plugins.version.ChainVersionMatcher)3 MavenTimedSnapshotVersionMatcher (org.apache.ivy.plugins.version.MavenTimedSnapshotVersionMatcher)3 MockVersionMatcher (org.apache.ivy.plugins.version.MockVersionMatcher)3 Test (org.junit.Test)3 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)2 ModuleId (org.apache.ivy.core.module.id.ModuleId)2 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)2 ArtifactInfo (org.apache.ivy.plugins.latest.ArtifactInfo)2 ResolvedResource (org.apache.ivy.plugins.resolver.util.ResolvedResource)2 File (java.io.File)1 IOException (java.io.IOException)1 LinkedHashSet (java.util.LinkedHashSet)1 Ivy (org.apache.ivy.Ivy)1