Search in sources :

Example 1 with ArtifactInfo

use of org.apache.ivy.plugins.latest.ArtifactInfo 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 ArtifactInfo

use of org.apache.ivy.plugins.latest.ArtifactInfo in project ant-ivy by apache.

the class OsgiLatestStrategyTest method testFindLatest.

@Test
public void testFindLatest() {
    ArtifactInfo[] revs = toMockAI(new String[] { "0.2.0.a", "0.2.0.b", "0.2.0.rc1", "0.2.0.final", "1.0.0.dev1", "1.0.0.dev2", "1.0.0.alpha1", "1.0.0.alpha2", "1.0.0.beta1", "1.0.0.beta2", "1.0.0.gamma", "1.0.0.rc1", "1.0.0.rc2", "1.0", "1.0.1", "2.0" });
    List<ArtifactInfo> shuffled = new ArrayList<>(Arrays.asList(revs));
    Collections.shuffle(shuffled);
    ArtifactInfo[] shuffledRevs = shuffled.toArray(new ArtifactInfo[revs.length]);
    OsgiLatestStrategy latestRevisionStrategy = new OsgiLatestStrategy();
    ArtifactInfo latest = latestRevisionStrategy.findLatest(shuffledRevs, new Date());
    assertNotNull(latest);
    assertEquals("2.0", latest.getRevision());
}
Also used : ArtifactInfo(org.apache.ivy.plugins.latest.ArtifactInfo) ArrayList(java.util.ArrayList) Date(java.util.Date) Test(org.junit.Test)

Example 3 with ArtifactInfo

use of org.apache.ivy.plugins.latest.ArtifactInfo in project ant-ivy by apache.

the class IvyBuildNumber method doExecute.

public void doExecute() throws BuildException {
    if (organisation == null) {
        throw new BuildException("no organisation provided for ivy buildnumber task");
    }
    if (module == null) {
        throw new BuildException("no module name provided for ivy buildnumber task");
    }
    if (prefix == null) {
        throw new BuildException("null prefix not allowed");
    }
    Ivy ivy = getIvyInstance();
    IvySettings settings = ivy.getSettings();
    if (branch == null) {
        branch = settings.getDefaultBranch(new ModuleId(organisation, module));
    }
    if (isNullOrEmpty(revision)) {
        revision = "latest.integration";
    } else if (!revision.endsWith("+")) {
        revision += "+";
    }
    if (!prefix.endsWith(".") && !prefix.isEmpty()) {
        prefix += ".";
    }
    SearchEngine searcher = new SearchEngine(settings);
    PatternMatcher patternMatcher = new PatternMatcher() {

        private PatternMatcher exact = new ExactPatternMatcher();

        private PatternMatcher regexp = new ExactOrRegexpPatternMatcher();

        public Matcher getMatcher(String expression) {
            if (expression.equals(organisation) || expression.equals(module) || expression.equals(branch)) {
                return exact.getMatcher(expression);
            } else {
                return regexp.getMatcher(expression);
            }
        }

        public String getName() {
            return "buildnumber-matcher";
        }
    };
    String revisionPattern = ".*";
    if (revision.endsWith("+")) {
        revisionPattern = Pattern.quote(revision.substring(0, revision.length() - 1)) + ".*";
    }
    ModuleRevisionId mrid = ModuleRevisionId.newInstance(organisation, module, branch, revisionPattern);
    ModuleRevisionId[] revisions;
    if (resolver == null) {
        revisions = searcher.listModules(mrid, patternMatcher);
    } else {
        DependencyResolver depResolver = settings.getResolver(resolver);
        if (depResolver == null) {
            throw new BuildException("Unknown resolver: " + resolver);
        }
        revisions = searcher.listModules(depResolver, mrid, patternMatcher);
    }
    List<ArtifactInfo> infos = new ArrayList<>(revisions.length);
    for (ModuleRevisionId rev : revisions) {
        infos.add(new ResolvedModuleRevisionArtifactInfo(rev));
    }
    VersionMatcher matcher = settings.getVersionMatcher();
    LatestStrategy latestStrategy = settings.getLatestStrategy("latest-revision");
    List<ArtifactInfo> sorted = latestStrategy.sort(infos.toArray(new ArtifactInfo[revisions.length]));
    ModuleRevisionId askedMrid = ModuleRevisionId.newInstance(organisation, module, branch, revision);
    String foundRevision = null;
    ListIterator<ArtifactInfo> iter = sorted.listIterator(sorted.size());
    while (iter.hasPrevious()) {
        ResolvedModuleRevisionArtifactInfo info = (ResolvedModuleRevisionArtifactInfo) iter.previous();
        if (!matcher.accept(askedMrid, info.rmr)) {
            continue;
        }
        if (matcher.needModuleDescriptor(askedMrid, info.rmr)) {
            ResolvedModuleRevision rmr = ivy.findModule(info.rmr);
            if (matcher.accept(askedMrid, rmr.getDescriptor())) {
                foundRevision = info.rmr.getRevision();
            }
        } else {
            foundRevision = info.rmr.getRevision();
        }
        if (foundRevision != null) {
            break;
        }
    }
    NewRevision newRevision = computeNewRevision(foundRevision);
    setProperty("revision", newRevision.getRevision());
    setProperty("new.revision", newRevision.getNewRevision());
    setProperty("build.number", newRevision.getBuildNumber());
    setProperty("new.build.number", newRevision.getNewBuildNumber());
}
Also used : ExactPatternMatcher(org.apache.ivy.plugins.matcher.ExactPatternMatcher) ArrayList(java.util.ArrayList) IvySettings(org.apache.ivy.core.settings.IvySettings) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) Ivy(org.apache.ivy.Ivy) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) ModuleId(org.apache.ivy.core.module.id.ModuleId) ExactOrRegexpPatternMatcher(org.apache.ivy.plugins.matcher.ExactOrRegexpPatternMatcher) SearchEngine(org.apache.ivy.core.search.SearchEngine) ArtifactInfo(org.apache.ivy.plugins.latest.ArtifactInfo) VersionMatcher(org.apache.ivy.plugins.version.VersionMatcher) LatestStrategy(org.apache.ivy.plugins.latest.LatestStrategy) BuildException(org.apache.tools.ant.BuildException) ExactPatternMatcher(org.apache.ivy.plugins.matcher.ExactPatternMatcher) ExactOrRegexpPatternMatcher(org.apache.ivy.plugins.matcher.ExactOrRegexpPatternMatcher) PatternMatcher(org.apache.ivy.plugins.matcher.PatternMatcher)

Aggregations

ArrayList (java.util.ArrayList)3 ArtifactInfo (org.apache.ivy.plugins.latest.ArtifactInfo)3 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)2 VersionMatcher (org.apache.ivy.plugins.version.VersionMatcher)2 Date (java.util.Date)1 Ivy (org.apache.ivy.Ivy)1 IvyContext (org.apache.ivy.core.IvyContext)1 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)1 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)1 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)1 ModuleId (org.apache.ivy.core.module.id.ModuleId)1 IvyNode (org.apache.ivy.core.resolve.IvyNode)1 ResolveData (org.apache.ivy.core.resolve.ResolveData)1 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)1 SearchEngine (org.apache.ivy.core.search.SearchEngine)1 IvySettings (org.apache.ivy.core.settings.IvySettings)1 ConflictManager (org.apache.ivy.plugins.conflict.ConflictManager)1 LatestStrategy (org.apache.ivy.plugins.latest.LatestStrategy)1 ExactOrRegexpPatternMatcher (org.apache.ivy.plugins.matcher.ExactOrRegexpPatternMatcher)1 ExactPatternMatcher (org.apache.ivy.plugins.matcher.ExactPatternMatcher)1