Search in sources :

Example 56 with ResolvedModuleRevision

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

Example 57 with ResolvedModuleRevision

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

the class DefaultRepositoryCacheManager method cacheModuleDescriptor.

public ResolvedModuleRevision cacheModuleDescriptor(DependencyResolver resolver, final ResolvedResource mdRef, DependencyDescriptor dd, Artifact moduleArtifact, ResourceDownloader downloader, CacheMetadataOptions options) throws ParseException {
    Date cachedPublicationDate = null;
    ArtifactDownloadReport report;
    ModuleRevisionId mrid = moduleArtifact.getModuleRevisionId();
    if (!lockMetadataArtifact(mrid)) {
        Message.error("impossible to acquire lock for " + mrid);
        return null;
    }
    BackupResourceDownloader backupDownloader = new BackupResourceDownloader(downloader);
    try {
        if (!moduleArtifact.isMetadata()) {
            // just make sure the old artifacts are deleted...
            if (isChanging(dd, mrid, options)) {
                long repoLastModified = mdRef.getLastModified();
                Artifact transformedArtifact = NameSpaceHelper.transform(moduleArtifact, options.getNamespace().getToSystemTransformer());
                ArtifactOrigin origin = getSavedArtifactOrigin(transformedArtifact);
                File artFile = getArchiveFileInCache(transformedArtifact, origin, false);
                if (artFile.exists() && repoLastModified > artFile.lastModified()) {
                    // artifacts have changed, they should be downloaded again
                    Message.verbose(mrid + " has changed: deleting old artifacts");
                    Message.debug("deleting " + artFile);
                    if (!artFile.delete()) {
                        Message.error("Couldn't delete outdated artifact from cache: " + artFile);
                        return null;
                    }
                    removeSavedArtifactOrigin(transformedArtifact);
                }
            }
            return null;
        }
        // now let's see if we can find it in cache and if it is up to date
        ResolvedModuleRevision rmr = doFindModuleInCache(mrid, options, null);
        if (rmr != null) {
            if (rmr.getDescriptor().isDefault() && rmr.getResolver() != resolver) {
                Message.verbose("\t" + getName() + ": found revision in cache: " + mrid + " (resolved by " + rmr.getResolver().getName() + "): but it's a default one, maybe we can find a better one");
            } else {
                if (!isCheckmodified(dd, mrid, options) && !isChanging(dd, mrid, options)) {
                    Message.verbose("\t" + getName() + ": revision in cache: " + mrid);
                    rmr.getReport().setSearched(true);
                    return rmr;
                }
                long repLastModified = mdRef.getLastModified();
                long cacheLastModified = rmr.getDescriptor().getLastModified();
                if (!rmr.getDescriptor().isDefault() && repLastModified <= cacheLastModified) {
                    Message.verbose("\t" + getName() + ": revision in cache (not updated): " + mrid);
                    rmr.getReport().setSearched(true);
                    return rmr;
                }
                Message.verbose("\t" + getName() + ": revision in cache is not up to date: " + mrid);
                if (isChanging(dd, mrid, options)) {
                    // ivy file has been updated, we should see if it has a new publication
                    // date to see if a new download is required (in case the dependency is
                    // a changing one)
                    cachedPublicationDate = rmr.getDescriptor().getResolvedPublicationDate();
                }
            }
        }
        Artifact originalMetadataArtifact = getOriginalMetadataArtifact(moduleArtifact);
        // now download module descriptor and parse it
        report = download(originalMetadataArtifact, new ArtifactResourceResolver() {

            public ResolvedResource resolve(Artifact artifact) {
                return mdRef;
            }
        }, backupDownloader, new CacheDownloadOptions().setListener(options.getListener()).setForce(true));
        Message.verbose("\t" + report);
        if (report.getDownloadStatus() == DownloadStatus.FAILED) {
            Message.warn("problem while downloading module descriptor: " + mdRef.getResource() + ": " + report.getDownloadDetails() + " (" + report.getDownloadTimeMillis() + "ms)");
            return null;
        }
        try {
            ModuleDescriptorParser parser = ModuleDescriptorParserRegistry.getInstance().getParser(mdRef.getResource());
            ParserSettings parserSettings = settings;
            if (resolver instanceof AbstractResolver) {
                parserSettings = ((AbstractResolver) resolver).getParserSettings();
            }
            ModuleDescriptor md = getStaledMd(parser, options, report.getLocalFile(), parserSettings);
            if (md == null) {
                throw new IllegalStateException("module descriptor parser returned a null module descriptor, which is not allowed. parser=" + parser + "; parser class=" + parser.getClass().getName() + "; module descriptor resource=" + mdRef.getResource());
            }
            Message.debug("\t" + getName() + ": parsed downloaded md file for " + mrid + "; parsed=" + md.getModuleRevisionId());
            // check if we should delete old artifacts
            boolean deleteOldArtifacts = false;
            if (cachedPublicationDate != null && !cachedPublicationDate.equals(md.getResolvedPublicationDate())) {
                // artifacts have changed, they should be downloaded again
                Message.verbose(mrid + " has changed: deleting old artifacts");
                deleteOldArtifacts = true;
            }
            if (deleteOldArtifacts) {
                for (String conf : md.getConfigurationsNames()) {
                    for (Artifact art : md.getArtifacts(conf)) {
                        Artifact transformedArtifact = NameSpaceHelper.transform(art, options.getNamespace().getToSystemTransformer());
                        ArtifactOrigin origin = getSavedArtifactOrigin(transformedArtifact);
                        File artFile = getArchiveFileInCache(transformedArtifact, origin, false);
                        if (artFile.exists()) {
                            Message.debug("deleting " + artFile);
                            if (!artFile.delete()) {
                                // Old artifacts couldn't get deleted!
                                // Restore the original ivy file so the next time we
                                // resolve the old artifacts are deleted again
                                backupDownloader.restore();
                                Message.error("Couldn't delete outdated artifact from cache: " + artFile);
                                return null;
                            }
                        }
                        removeSavedArtifactOrigin(transformedArtifact);
                    }
                }
            } else if (isChanging(dd, mrid, options)) {
                Message.verbose(mrid + " is changing, but has not changed: will trust cached artifacts if any");
            }
            MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(md.getMetadataArtifact());
            madr.setSearched(true);
            madr.setDownloadStatus(report.getDownloadStatus());
            madr.setDownloadDetails(report.getDownloadDetails());
            madr.setArtifactOrigin(report.getArtifactOrigin());
            madr.setDownloadTimeMillis(report.getDownloadTimeMillis());
            madr.setOriginalLocalFile(report.getLocalFile());
            madr.setSize(report.getSize());
            Artifact transformedMetadataArtifact = NameSpaceHelper.transform(md.getMetadataArtifact(), options.getNamespace().getToSystemTransformer());
            saveArtifactOrigin(transformedMetadataArtifact, report.getArtifactOrigin());
            return new ResolvedModuleRevision(resolver, resolver, md, madr);
        } catch (IOException ex) {
            Message.warn("io problem while parsing ivy file: " + mdRef.getResource(), ex);
            return null;
        }
    } finally {
        unlockMetadataArtifact(mrid);
        backupDownloader.cleanUp();
    }
}
Also used : MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) IOException(java.io.IOException) ArtifactResourceResolver(org.apache.ivy.plugins.repository.ArtifactResourceResolver) Date(java.util.Date) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser) XmlModuleDescriptorParser(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ParserSettings(org.apache.ivy.plugins.parser.ParserSettings) AbstractResolver(org.apache.ivy.plugins.resolver.AbstractResolver)

Example 58 with ResolvedModuleRevision

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

the class CheckEngine method check.

/**
 * Checks the given ivy file using current settings to see if all dependencies are available,
 * with good confs. If a resolver name is given, it also checks that the declared publications
 * are available in the corresponding resolver. Note that the check is not performed
 * recursively, i.e. if a dependency has itself dependencies badly described or not available,
 * this check will not discover it.
 *
 * @param ivyFile URL
 * @param resolvername String
 * @return boolean
 */
public boolean check(URL ivyFile, String resolvername) {
    try {
        boolean result = true;
        // parse ivy file
        ModuleDescriptor md = ModuleDescriptorParserRegistry.getInstance().parseDescriptor(settings, ivyFile, settings.doValidate());
        // check publications if possible
        if (resolvername != null) {
            DependencyResolver resolver = settings.getResolver(resolvername);
            Set<Artifact> artifacts = new HashSet<>();
            for (String conf : md.getConfigurationsNames()) {
                artifacts.addAll(Arrays.asList(md.getArtifacts(conf)));
            }
            for (Artifact artifact : artifacts) {
                if (!resolver.exists(artifact)) {
                    Message.info("declared publication not found: " + artifact);
                    result = false;
                }
            }
        }
        // check dependencies
        ResolveData data = new ResolveData(resolveEngine, new ResolveOptions());
        for (DependencyDescriptor dd : md.getDependencies()) {
            // check master confs
            for (String masterConf : dd.getModuleConfigurations()) {
                if (!"*".equals(masterConf.trim()) && md.getConfiguration(masterConf) == null) {
                    Message.info("dependency required in non existing conf for " + ivyFile + " \n\tin " + dd + ": " + masterConf);
                    result = false;
                }
            }
            // resolve
            DependencyResolver resolver = settings.getResolver(dd.getDependencyRevisionId());
            ResolvedModuleRevision rmr = resolver.getDependency(dd, data);
            if (rmr == null) {
                Message.info("dependency not found in " + ivyFile + ":\n\t" + dd);
                result = false;
            } else {
                for (String depConf : dd.getDependencyConfigurations(md.getConfigurationsNames())) {
                    if (!Arrays.asList(rmr.getDescriptor().getConfigurationsNames()).contains(depConf)) {
                        Message.info("dependency configuration is missing for " + ivyFile + "\n\tin " + dd + ": " + depConf);
                        result = false;
                    }
                    for (Artifact art : rmr.getDescriptor().getArtifacts(depConf)) {
                        if (!resolver.exists(art)) {
                            Message.info("dependency artifact is missing for " + ivyFile + "\n\t in " + dd + ": " + art);
                            result = false;
                        }
                    }
                }
            }
        }
        return result;
    } catch (ParseException e) {
        Message.info("parse problem on " + ivyFile, e);
        return false;
    } catch (IOException e) {
        Message.info("io problem on " + ivyFile, e);
        return false;
    } catch (Exception e) {
        Message.info("problem on " + ivyFile, e);
        return false;
    }
}
Also used : DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) IOException(java.io.IOException) Artifact(org.apache.ivy.core.module.descriptor.Artifact) IOException(java.io.IOException) ParseException(java.text.ParseException) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ResolveData(org.apache.ivy.core.resolve.ResolveData) ParseException(java.text.ParseException) ResolveOptions(org.apache.ivy.core.resolve.ResolveOptions) HashSet(java.util.HashSet)

Example 59 with ResolvedModuleRevision

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

the class IvyFindRevision method doExecute.

public void doExecute() throws BuildException {
    if (organisation == null) {
        throw new BuildException("no organisation provided for ivy findrevision task");
    }
    if (module == null) {
        throw new BuildException("no module name provided for ivy findrevision task");
    }
    if (revision == null) {
        throw new BuildException("no revision provided for ivy findrevision task");
    }
    Ivy ivy = getIvyInstance();
    IvySettings settings = ivy.getSettings();
    if (branch == null) {
        branch = settings.getDefaultBranch(new ModuleId(organisation, module));
    }
    ResolvedModuleRevision rmr = ivy.findModule(ModuleRevisionId.newInstance(organisation, module, branch, revision));
    if (rmr != null) {
        getProject().setProperty(property, rmr.getId().getRevision());
    }
}
Also used : ModuleId(org.apache.ivy.core.module.id.ModuleId) IvySettings(org.apache.ivy.core.settings.IvySettings) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) BuildException(org.apache.tools.ant.BuildException) Ivy(org.apache.ivy.Ivy)

Example 60 with ResolvedModuleRevision

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

the class PomModuleDescriptorParserTest method testParentProperties.

@Test
public void testParentProperties() throws ParseException, IOException {
    settings.setDictatorResolver(new MockResolver() {

        public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException {
            try {
                ModuleDescriptor moduleDescriptor = PomModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-version.pom"), false);
                return new ResolvedModuleRevision(null, null, moduleDescriptor, null);
            } catch (IOException e) {
                throw new AssertionError(e);
            }
        }
    });
    ModuleDescriptor md = PomModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-parent-properties.pom"), false);
    assertNotNull(md);
    assertEquals("1.0", md.getRevision());
    DependencyDescriptor[] dds = md.getDependencies();
    assertNotNull(dds);
    assertEquals(3, dds.length);
    // 2 are inherited from parent. Only the first one is important for this test
    assertEquals(ModuleRevisionId.newInstance("org.apache", "test-version-other", "5.76"), dds[0].getDependencyRevisionId());
// present in the pom using a property defined in the parent
}
Also used : ResolveData(org.apache.ivy.core.resolve.ResolveData) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) MockResolver(org.apache.ivy.plugins.resolver.MockResolver) ParseException(java.text.ParseException) IOException(java.io.IOException) XmlModuleDescriptorParserTest(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest) Test(org.junit.Test)

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