Search in sources :

Example 1 with ResolvedModuleRevision

use of org.apache.ivy.core.resolve.ResolvedModuleRevision 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 2 with ResolvedModuleRevision

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

the class BasicResolver method getDefaultRMDParser.

protected ResourceMDParser getDefaultRMDParser(final ModuleId mid) {
    return new ResourceMDParser() {

        public MDResolvedResource parse(Resource resource, String rev) {
            DefaultModuleDescriptor md = DefaultModuleDescriptor.newDefaultInstance(new ModuleRevisionId(mid, rev));
            MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(md.getMetadataArtifact());
            madr.setDownloadStatus(DownloadStatus.NO);
            madr.setSearched(true);
            return new MDResolvedResource(resource, rev, new ResolvedModuleRevision(BasicResolver.this, BasicResolver.this, md, madr, isForce()));
        }
    };
}
Also used : MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ResourceMDParser(org.apache.ivy.plugins.resolver.util.ResourceMDParser) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) MDResolvedResource(org.apache.ivy.plugins.resolver.util.MDResolvedResource) Resource(org.apache.ivy.plugins.repository.Resource) FileResource(org.apache.ivy.plugins.repository.file.FileResource) URLResource(org.apache.ivy.plugins.repository.url.URLResource) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) MDResolvedResource(org.apache.ivy.plugins.resolver.util.MDResolvedResource)

Example 3 with ResolvedModuleRevision

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

the class ChainResolver method getDependency.

public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException {
    data = new ResolveData(data, doValidate(data));
    List<Exception> errors = new ArrayList<>();
    ResolvedModuleRevision resolved = data.getCurrentResolvedModuleRevision();
    ResolvedModuleRevision mr = resolved;
    if (mr == null) {
        Message.verbose(getName() + ": Checking cache for: " + dd);
        mr = findModuleInCache(dd, data, true);
        if (mr != null) {
            Message.verbose(getName() + ": module revision found in cache: " + mr.getId());
            mr = forcedRevision(mr);
        }
    }
    for (DependencyResolver resolver : chain) {
        LatestStrategy oldLatest = setLatestIfRequired(resolver, getLatestStrategy());
        try {
            ResolvedModuleRevision previouslyResolved = mr;
            data.setCurrentResolvedModuleRevision(previouslyResolved);
            mr = resolver.getDependency(dd, data);
            if (mr != previouslyResolved && isReturnFirst()) {
                mr = forcedRevision(mr);
            }
        } catch (Exception ex) {
            Message.verbose("problem occurred while resolving " + dd + " with " + resolver, ex);
            errors.add(ex);
        } finally {
            if (oldLatest != null) {
                setLatest(resolver, oldLatest);
            }
        }
        checkInterrupted();
    }
    if (mr == null && !errors.isEmpty()) {
        if (errors.size() == 1) {
            Exception ex = errors.get(0);
            if (ex instanceof RuntimeException) {
                throw (RuntimeException) ex;
            } else if (ex instanceof ParseException) {
                throw (ParseException) ex;
            } else {
                throw new RuntimeException(ex.toString(), ex);
            }
        } else {
            StringBuilder err = new StringBuilder();
            for (Exception ex : errors) {
                err.append("\t").append(StringUtils.getErrorMessage(ex)).append("\n");
            }
            err.setLength(err.length() - 1);
            throw new RuntimeException("several problems occurred while resolving " + dd + ":\n" + err);
        }
    }
    if (resolved == mr) {
        // nothing has actually been resolved here, we don't need to touch the returned rmr
        return resolved;
    }
    return resolvedRevision(mr);
}
Also used : ResolveData(org.apache.ivy.core.resolve.ResolveData) ArrayList(java.util.ArrayList) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) HasLatestStrategy(org.apache.ivy.plugins.resolver.util.HasLatestStrategy) LatestStrategy(org.apache.ivy.plugins.latest.LatestStrategy) ParseException(java.text.ParseException) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 4 with ResolvedModuleRevision

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

the class DualResolver method getDependency.

public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException {
    if (ivyResolver == null || artifactResolver == null) {
        throw new IllegalStateException("exactly two resolvers must be added: ivy(1) and artifact(2) one");
    }
    ResolvedModuleRevision resolved = data.getCurrentResolvedModuleRevision();
    data = new ResolveData(data, doValidate(data));
    final ResolvedModuleRevision mr = ivyResolver.getDependency(dd, data);
    if (mr == null) {
        checkInterrupted();
        if (isAllownomd()) {
            Message.verbose("ivy resolver didn't find " + dd + ": trying with artifact resolver");
            return artifactResolver.getDependency(dd, data);
        } else {
            return null;
        }
    } else {
        if (mr == resolved) {
            // nothing has actually been resolved here, we don't need to touch the returned rmr
            return mr;
        }
        return new ResolvedModuleRevision(mr.getResolver(), this, mr.getDescriptor(), mr.getReport(), mr.isForce());
    }
}
Also used : ResolveData(org.apache.ivy.core.resolve.ResolveData) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision)

Example 5 with ResolvedModuleRevision

use of org.apache.ivy.core.resolve.ResolvedModuleRevision 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)

Aggregations

ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)89 Test (org.junit.Test)63 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)61 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)45 Artifact (org.apache.ivy.core.module.descriptor.Artifact)25 GregorianCalendar (java.util.GregorianCalendar)24 File (java.io.File)19 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)19 Date (java.util.Date)18 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)18 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)18 DownloadReport (org.apache.ivy.core.report.DownloadReport)18 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)17 IOException (java.io.IOException)14 ParseException (java.text.ParseException)14 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)13 ResolveData (org.apache.ivy.core.resolve.ResolveData)11 MockResolver (org.apache.ivy.plugins.resolver.MockResolver)9 MetadataArtifactDownloadReport (org.apache.ivy.core.report.MetadataArtifactDownloadReport)8 DownloadOptions (org.apache.ivy.core.resolve.DownloadOptions)8