Search in sources :

Example 1 with DependencyResolver

use of org.apache.ivy.plugins.resolver.DependencyResolver in project ant-ivy by apache.

the class DefaultRepositoryCacheManager method doFindModuleInCache.

private ResolvedModuleRevision doFindModuleInCache(ModuleRevisionId mrid, CacheMetadataOptions options, String expectedResolver) {
    if (!lockMetadataArtifact(mrid)) {
        Message.error("impossible to acquire lock for " + mrid);
        return null;
    }
    boolean unlock = true;
    try {
        if (settings.getVersionMatcher().isDynamic(mrid)) {
            String resolvedRevision = getResolvedRevision(expectedResolver, mrid, options);
            if (resolvedRevision == null) {
                return null;
            }
            Message.verbose("found resolved revision in cache: " + mrid + " => " + resolvedRevision);
            // we have found another module in the cache, make sure we unlock
            // the original module
            unlockMetadataArtifact(mrid);
            mrid = ModuleRevisionId.newInstance(mrid, resolvedRevision);
            // don't forget to request a lock on the new module!
            if (!lockMetadataArtifact(mrid)) {
                Message.error("impossible to acquire lock for " + mrid);
                // we couldn't lock the new module, so no need to unlock it
                unlock = false;
                return null;
            }
        }
        File ivyFile = getIvyFileInCache(mrid);
        if (ivyFile.exists()) {
            // found in cache !
            try {
                ModuleDescriptorParser parser = getModuleDescriptorParser(ivyFile);
                ModuleDescriptor depMD = getMdFromCache(parser, options, ivyFile);
                String resolverName = getSavedResolverName(depMD);
                String artResolverName = getSavedArtResolverName(depMD);
                DependencyResolver resolver = settings.getResolver(resolverName);
                if (resolver == null) {
                    Message.debug("\tresolver not found: " + resolverName + " => trying to use the one configured for " + mrid);
                    resolver = settings.getResolver(depMD.getResolvedModuleRevisionId());
                    if (resolver != null) {
                        Message.debug("\tconfigured resolver found for " + depMD.getResolvedModuleRevisionId() + ": " + resolver.getName() + ": saving this data");
                        saveResolver(depMD, resolver.getName());
                    }
                }
                DependencyResolver artResolver = settings.getResolver(artResolverName);
                if (artResolver == null) {
                    artResolver = resolver;
                }
                if (resolver != null) {
                    Message.debug("\tfound ivy file in cache for " + mrid + " (resolved by " + resolver.getName() + "): " + ivyFile);
                    if (expectedResolver == null || expectedResolver.equals(resolver.getName())) {
                        MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(depMD.getMetadataArtifact());
                        madr.setDownloadStatus(DownloadStatus.NO);
                        madr.setSearched(false);
                        madr.setLocalFile(ivyFile);
                        madr.setSize(ivyFile.length());
                        madr.setArtifactOrigin(getSavedArtifactOrigin(depMD.getMetadataArtifact()));
                        if (madr.getArtifactOrigin().isExists()) {
                            if (madr.getArtifactOrigin().isLocal() && madr.getArtifactOrigin().getArtifact().getUrl() != null) {
                                madr.setOriginalLocalFile(new File(madr.getArtifactOrigin().getArtifact().getUrl().toURI()));
                            } else {
                                // find locally cached file
                                madr.setOriginalLocalFile(getArchiveFileInCache(madr.getArtifactOrigin().getArtifact()));
                            }
                        }
                        return new ResolvedModuleRevision(resolver, artResolver, depMD, madr);
                    }
                    Message.debug("found module in cache but with a different resolver: " + "discarding: " + mrid + "; expected resolver=" + expectedResolver + "; resolver=" + resolver.getName());
                } else {
                    Message.debug("\tresolver not found: " + resolverName + " => cannot use cached ivy file for " + mrid);
                }
            } catch (Exception e) {
                // will try with resolver
                Message.debug("\tproblem while parsing cached ivy file for: " + mrid, e);
            }
        } else {
            Message.debug("\tno ivy file in cache for " + mrid + ": tried " + ivyFile);
        }
    } finally {
        if (unlock) {
            unlockMetadataArtifact(mrid);
        }
    }
    return null;
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser) XmlModuleDescriptorParser(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser) ParseException(java.text.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver)

Example 2 with DependencyResolver

use of org.apache.ivy.plugins.resolver.DependencyResolver in project ant-ivy by apache.

the class InstallEngine method install.

public ResolveReport install(ModuleRevisionId mrid, String from, String to, InstallOptions options) throws IOException {
    DependencyResolver fromResolver = settings.getResolver(from);
    DependencyResolver toResolver = settings.getResolver(to);
    if (fromResolver == null) {
        throw new IllegalArgumentException("unknown resolver " + from + ". Available resolvers are: " + settings.getResolverNames());
    }
    if (toResolver == null) {
        throw new IllegalArgumentException("unknown resolver " + to + ". Available resolvers are: " + settings.getResolverNames());
    }
    PatternMatcher matcher = settings.getMatcher(options.getMatcherName());
    if (matcher == null) {
        throw new IllegalArgumentException("unknown matcher " + options.getMatcherName() + ". Available matchers are: " + settings.getMatcherNames());
    }
    // build module file declaring the dependency
    Message.info(":: installing " + mrid + " ::");
    DependencyResolver oldDictator = resolveEngine.getDictatorResolver();
    boolean log = settings.logNotConvertedExclusionRule();
    try {
        settings.setLogNotConvertedExclusionRule(true);
        resolveEngine.setDictatorResolver(fromResolver);
        DefaultModuleDescriptor md = new DefaultModuleDescriptor(ModuleRevisionId.newInstance("apache", "ivy-install", "1.0"), settings.getStatusManager().getDefaultStatus(), new Date());
        String resolveId = ResolveOptions.getDefaultResolveId(md);
        md.addConfiguration(new Configuration("default"));
        md.addConflictManager(new ModuleId(ExactPatternMatcher.ANY_EXPRESSION, ExactPatternMatcher.ANY_EXPRESSION), ExactPatternMatcher.INSTANCE, new NoConflictManager());
        for (String dc : options.getConfs()) {
            final String depConf = dc.trim();
            if (MatcherHelper.isExact(matcher, mrid)) {
                DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, mrid, false, false, options.isTransitive());
                dd.addDependencyConfiguration("default", depConf);
                md.addDependency(dd);
            } else {
                for (ModuleRevisionId imrid : searchEngine.listModules(fromResolver, mrid, matcher)) {
                    Message.info("\tfound " + imrid + " to install: adding to the list");
                    DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(md, imrid, false, false, options.isTransitive());
                    dd.addDependencyConfiguration("default", depConf);
                    md.addDependency(dd);
                }
            }
        }
        // resolve using appropriate resolver
        ResolveReport report = new ResolveReport(md, resolveId);
        Message.info(":: resolving dependencies ::");
        ResolveOptions resolveOptions = new ResolveOptions().setResolveId(resolveId).setConfs(new String[] { "default" }).setValidate(options.isValidate());
        IvyNode[] dependencies = resolveEngine.getDependencies(md, resolveOptions, report);
        report.setDependencies(Arrays.asList(dependencies), options.getArtifactFilter());
        Message.info(":: downloading artifacts to cache ::");
        resolveEngine.downloadArtifacts(report, options.getArtifactFilter(), new DownloadOptions());
        // now that everything is in cache, we can publish all these modules
        Message.info(":: installing in " + to + " ::");
        for (IvyNode dependency : dependencies) {
            ModuleDescriptor depmd = dependency.getDescriptor();
            if (depmd != null) {
                ModuleRevisionId depMrid = depmd.getModuleRevisionId();
                Message.verbose("installing " + depMrid);
                boolean successfullyPublished = false;
                try {
                    toResolver.beginPublishTransaction(depMrid, options.isOverwrite());
                    // publish artifacts
                    for (ArtifactDownloadReport artifact : report.getArtifactsReports(depMrid)) {
                        if (artifact.getLocalFile() != null) {
                            toResolver.publish(artifact.getArtifact(), artifact.getLocalFile(), options.isOverwrite());
                        }
                    }
                    // publish metadata
                    MetadataArtifactDownloadReport artifactDownloadReport = dependency.getModuleRevision().getReport();
                    File localIvyFile = artifactDownloadReport.getLocalFile();
                    toResolver.publish(depmd.getMetadataArtifact(), localIvyFile, options.isOverwrite());
                    if (options.isInstallOriginalMetadata()) {
                        if (artifactDownloadReport.getArtifactOrigin() != null && artifactDownloadReport.getArtifactOrigin().isExists() && !ArtifactOrigin.isUnknown(artifactDownloadReport.getArtifactOrigin()) && artifactDownloadReport.getArtifactOrigin().getArtifact() != null && artifactDownloadReport.getArtifactOrigin().getArtifact().getType().endsWith(".original") && !artifactDownloadReport.getArtifactOrigin().getArtifact().getType().equals(depmd.getMetadataArtifact().getType() + ".original")) {
                            // publish original metadata artifact, too, as it has a different
                            // type
                            toResolver.publish(artifactDownloadReport.getArtifactOrigin().getArtifact(), artifactDownloadReport.getOriginalLocalFile(), options.isOverwrite());
                        }
                    }
                    // end module publish
                    toResolver.commitPublishTransaction();
                    successfullyPublished = true;
                } finally {
                    if (!successfullyPublished) {
                        toResolver.abortPublishTransaction();
                    }
                }
            }
        }
        Message.info(":: install resolution report ::");
        // output report
        resolveEngine.outputReport(report, settings.getResolutionCacheManager(), resolveOptions);
        return report;
    } finally {
        // IVY-834: log the problems if there were any...
        Message.sumupProblems();
        resolveEngine.setDictatorResolver(oldDictator);
        settings.setLogNotConvertedExclusionRule(log);
    }
}
Also used : MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) Configuration(org.apache.ivy.core.module.descriptor.Configuration) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) Date(java.util.Date) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) ModuleId(org.apache.ivy.core.module.id.ModuleId) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) NoConflictManager(org.apache.ivy.plugins.conflict.NoConflictManager) ResolveReport(org.apache.ivy.core.report.ResolveReport) DownloadOptions(org.apache.ivy.core.resolve.DownloadOptions) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ExactPatternMatcher(org.apache.ivy.plugins.matcher.ExactPatternMatcher) PatternMatcher(org.apache.ivy.plugins.matcher.PatternMatcher) ResolveOptions(org.apache.ivy.core.resolve.ResolveOptions) File(java.io.File) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 3 with DependencyResolver

use of org.apache.ivy.plugins.resolver.DependencyResolver in project ant-ivy by apache.

the class IvyConfigureTest method testDefault.

@Test
public void testDefault() {
    // by default settings look in the current directory for an ivysettings.xml file...
    // but Ivy itself has one, and we don't want to use it
    configure.getProject().setProperty("ivy.settings.file", "no/settings/will/use/default.xml");
    configure.execute();
    IvySettings settings = getIvyInstance().getSettings();
    assertNotNull(settings.getDefaultResolver());
    DependencyResolver publicResolver = settings.getResolver("public");
    assertNotNull(publicResolver);
    assertTrue(publicResolver instanceof IBiblioResolver);
    IBiblioResolver ibiblio = (IBiblioResolver) publicResolver;
    assertTrue(ibiblio.isM2compatible());
}
Also used : IBiblioResolver(org.apache.ivy.plugins.resolver.IBiblioResolver) IvySettings(org.apache.ivy.core.settings.IvySettings) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) Test(org.junit.Test)

Example 4 with DependencyResolver

use of org.apache.ivy.plugins.resolver.DependencyResolver in project ant-ivy by apache.

the class IvyConfigureTest method testDefault14.

@Test
public void testDefault14() {
    // by default settings look in the current directory for an ivysettings.xml file...
    // but Ivy itself has one, and we don't want to use it
    configure.getProject().setProperty("ivy.settings.file", "no/settings/will/use/default.xml");
    configure.getProject().setProperty("ivy.14.compatible", "true");
    configure.execute();
    IvySettings settings = getIvyInstance().getSettings();
    assertNotNull(settings.getDefaultResolver());
    DependencyResolver publicResolver = settings.getResolver("public");
    assertTrue(publicResolver instanceof IvyRepResolver);
}
Also used : IvySettings(org.apache.ivy.core.settings.IvySettings) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) IvyRepResolver(org.apache.ivy.plugins.resolver.IvyRepResolver) Test(org.junit.Test)

Example 5 with DependencyResolver

use of org.apache.ivy.plugins.resolver.DependencyResolver in project ant-ivy by apache.

the class BuildOBRTask method doExecute.

public void doExecute() throws BuildException {
    if (file == null) {
        throw new BuildException("No output file specified: use the attribute 'out'");
    }
    Iterable<ManifestAndLocation> it;
    if (resolverName != null) {
        if (baseDir != null) {
            throw new BuildException("specify only one of 'resolver' or 'baseDir'");
        }
        if (cacheName != null) {
            throw new BuildException("specify only one of 'resolver' or 'cache'");
        }
        Ivy ivy = getIvyInstance();
        IvySettings settings = ivy.getSettings();
        DependencyResolver resolver = settings.getResolver(resolverName);
        if (resolver == null) {
            throw new BuildException("the resolver '" + resolverName + "' was not found");
        }
        if (!(resolver instanceof BasicResolver)) {
            throw new BuildException("the type of resolver '" + resolver.getClass().getName() + "' is not supported.");
        }
        it = new ResolverManifestIterable((BasicResolver) resolver);
    } else if (baseDir != null) {
        if (cacheName != null) {
            throw new BuildException("specify only one of 'baseDir' or 'cache'");
        }
        if (!baseDir.isDirectory()) {
            throw new BuildException(baseDir + " is not a directory");
        }
        it = new FSManifestIterable(baseDir);
    } else if (cacheName != null) {
        Ivy ivy = getIvyInstance();
        RepositoryCacheManager cacheManager = ivy.getSettings().getRepositoryCacheManager(cacheName);
        if (!(cacheManager instanceof DefaultRepositoryCacheManager)) {
            throw new BuildException("the type of cache '" + cacheManager.getClass().getName() + "' is not supported.");
        }
        File basedir = ((DefaultRepositoryCacheManager) cacheManager).getBasedir();
        it = new FSManifestIterable(basedir);
    } else {
        prepareAndCheck();
        try {
            it = new ArtifactReportManifestIterable(getArtifactReports(), sourceTypes);
        } catch (ParseException e) {
            throw new BuildException("Impossible to parse the artifact reports: " + e.getMessage(), e);
        }
    }
    OutputStream out;
    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        throw new BuildException(file + " was not found", e);
    }
    ContentHandler hd;
    try {
        hd = OBRXMLWriter.newHandler(out, encoding, indent);
    } catch (TransformerConfigurationException e) {
        throw new BuildException("Sax configuration error: " + e.getMessage(), e);
    }
    class AntMessageLogger2 extends AntMessageLogger {

        AntMessageLogger2() {
            super(BuildOBRTask.this);
        }
    }
    IvyContext.getContext().getMessageLogger();
    Message.setDefaultLogger(new AntMessageLogger2());
    try {
        OBRXMLWriter.writeManifests(it, hd, quiet);
    } catch (SAXException e) {
        throw new BuildException("Sax serialisation error: " + e.getMessage(), e);
    }
    try {
        out.flush();
        out.close();
    } catch (IOException e) {
    // don't care
    }
    Message.sumupProblems();
}
Also used : DefaultRepositoryCacheManager(org.apache.ivy.core.cache.DefaultRepositoryCacheManager) RepositoryCacheManager(org.apache.ivy.core.cache.RepositoryCacheManager) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IvySettings(org.apache.ivy.core.settings.IvySettings) ArtifactReportManifestIterable(org.apache.ivy.osgi.repo.ArtifactReportManifestIterable) IOException(java.io.IOException) Ivy(org.apache.ivy.Ivy) DefaultRepositoryCacheManager(org.apache.ivy.core.cache.DefaultRepositoryCacheManager) ContentHandler(org.xml.sax.ContentHandler) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) SAXException(org.xml.sax.SAXException) FSManifestIterable(org.apache.ivy.osgi.repo.FSManifestIterable) FileOutputStream(java.io.FileOutputStream) BasicResolver(org.apache.ivy.plugins.resolver.BasicResolver) ResolverManifestIterable(org.apache.ivy.osgi.repo.ResolverManifestIterable) ManifestAndLocation(org.apache.ivy.osgi.repo.ManifestAndLocation) BuildException(org.apache.tools.ant.BuildException) ParseException(java.text.ParseException) File(java.io.File)

Aggregations

DependencyResolver (org.apache.ivy.plugins.resolver.DependencyResolver)50 Test (org.junit.Test)23 ModuleDescriptorParserRegistryTest (org.apache.ivy.plugins.parser.ModuleDescriptorParserRegistryTest)11 ChainResolver (org.apache.ivy.plugins.resolver.ChainResolver)11 File (java.io.File)10 Ivy (org.apache.ivy.Ivy)9 HashMap (java.util.HashMap)8 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)8 URL (java.net.URL)7 HashSet (java.util.HashSet)7 Map (java.util.Map)7 LinkedHashSet (java.util.LinkedHashSet)6 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)6 IvySettings (org.apache.ivy.core.settings.IvySettings)6 FileSystemResolver (org.apache.ivy.plugins.resolver.FileSystemResolver)6 ParseException (java.text.ParseException)5 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)5 IvyRepResolver (org.apache.ivy.plugins.resolver.IvyRepResolver)5 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)4 ModuleId (org.apache.ivy.core.module.id.ModuleId)4