Search in sources :

Example 16 with ModuleDescriptor

use of org.apache.ivy.core.module.descriptor.ModuleDescriptor 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 17 with ModuleDescriptor

use of org.apache.ivy.core.module.descriptor.ModuleDescriptor in project ant-ivy by apache.

the class DeliverEngine method deliver.

/**
 * Delivers a resolved ivy file based upon last resolve call status. If resolve report file
 * cannot be found in cache, then it throws an IllegalStateException (maybe resolve has not been
 * called before ?).
 *
 * @param mrid
 *            the module revision id of the module to deliver
 * @param revision
 *            the revision to which the module should be delivered
 * @param destIvyPattern
 *            the pattern to which the delivered ivy file should be written
 * @param options
 *            the options with which deliver should be done
 * @throws IOException if something goes wrong
 * @throws ParseException if something goes wrong
 */
public void deliver(ModuleRevisionId mrid, String revision, String destIvyPattern, DeliverOptions options) throws IOException, ParseException {
    Message.info(":: delivering :: " + mrid + " :: " + revision + " :: " + options.getStatus() + " :: " + options.getPubdate());
    Message.verbose("\toptions = " + options);
    long start = System.currentTimeMillis();
    destIvyPattern = settings.substitute(destIvyPattern);
    // 1) find the resolved module descriptor in cache
    ModuleDescriptor md = getCache().getResolvedModuleDescriptor(mrid);
    md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(md.getModuleRevisionId(), options.getPubBranch() == null ? mrid.getBranch() : options.getPubBranch(), revision));
    md.setResolvedPublicationDate(options.getPubdate());
    // 2) parse resolvedRevisions From properties file
    // Map (ModuleId -> String revision)
    Map<ModuleRevisionId, String> resolvedRevisions = new HashMap<>();
    // Map (ModuleId -> String branch)
    Map<ModuleRevisionId, String> resolvedBranches = new HashMap<>();
    // Map (ModuleId -> String status)
    Map<ModuleRevisionId, String> dependenciesStatus = new HashMap<>();
    File ivyProperties = getCache().getResolvedIvyPropertiesInCache(mrid);
    if (!ivyProperties.exists()) {
        throw new IllegalStateException("ivy properties not found in cache for " + mrid + "; please resolve dependencies before delivering!");
    }
    Properties props = new Properties();
    FileInputStream in = new FileInputStream(ivyProperties);
    props.load(in);
    in.close();
    for (Object o : props.keySet()) {
        String depMridStr = (String) o;
        String[] parts = props.getProperty(depMridStr).split(" ");
        ModuleRevisionId decodedMrid = ModuleRevisionId.decode(depMridStr);
        if (options.isResolveDynamicRevisions()) {
            resolvedRevisions.put(decodedMrid, parts[0]);
            if (parts.length >= 4) {
                if (parts[3] != null && !"null".equals(parts[3])) {
                    resolvedBranches.put(decodedMrid, parts[3]);
                }
            }
        }
        dependenciesStatus.put(decodedMrid, parts[1]);
        if (options.isReplaceForcedRevisions()) {
            if (parts.length <= 2) {
                // so it is possible that this part doesn't exist.
                throw new IllegalStateException("ivy properties file generated by an older" + " version of Ivy which doesn't support replacing forced revisions!");
            }
            resolvedRevisions.put(decodedMrid, parts[2]);
        }
    }
    // 3) use pdrResolver to resolve dependencies info
    Map<ModuleRevisionId, String> resolvedDependencies = new HashMap<>();
    // Map (ModuleRevisionId -> String revision)
    for (DependencyDescriptor dependency : md.getDependencies()) {
        String rev = resolvedRevisions.get(dependency.getDependencyRevisionId());
        if (rev == null) {
            rev = dependency.getDependencyRevisionId().getRevision();
        }
        String bra = resolvedBranches.get(dependency.getDependencyRevisionId());
        if (bra == null || "null".equals(bra)) {
            bra = dependency.getDependencyRevisionId().getBranch();
        }
        String depStatus = dependenciesStatus.get(dependency.getDependencyRevisionId());
        ModuleRevisionId mrid2 = (bra == null) ? ModuleRevisionId.newInstance(dependency.getDependencyRevisionId(), rev) : ModuleRevisionId.newInstance(dependency.getDependencyRevisionId(), bra, rev);
        resolvedDependencies.put(dependency.getDependencyRevisionId(), options.getPdrResolver().resolve(md, options.getStatus(), mrid2, depStatus));
    }
    // 4) copy the source resolved ivy to the destination specified,
    // updating status, revision and dependency revisions obtained by
    // PublishingDependencyRevisionResolver
    File publishedIvy = settings.resolveFile(IvyPatternHelper.substitute(destIvyPattern, md.getResolvedModuleRevisionId()));
    Message.info("\tdelivering ivy file to " + publishedIvy);
    String[] confs = ConfigurationUtils.replaceWildcards(options.getConfs(), md);
    Set<String> confsToRemove = new HashSet<>(Arrays.asList(md.getConfigurationsNames()));
    confsToRemove.removeAll(Arrays.asList(confs));
    try {
        UpdateOptions opts = new UpdateOptions().setSettings(settings).setResolvedRevisions(resolvedDependencies).setStatus(options.getStatus()).setRevision(revision).setBranch(options.getPubBranch()).setPubdate(options.getPubdate()).setGenerateRevConstraint(options.isGenerateRevConstraint()).setMerge(options.isMerge()).setMergedDescriptor(md).setConfsToExclude(confsToRemove.toArray(new String[confsToRemove.size()]));
        if (!resolvedBranches.isEmpty()) {
            opts = opts.setResolvedBranches(resolvedBranches);
        }
        Resource res = md.getResource();
        XmlModuleDescriptorUpdater.update(res.openStream(), res, publishedIvy, opts);
    } catch (SAXException ex) {
        throw new RuntimeException("bad ivy file in cache for " + mrid, ex);
    }
    Message.verbose("\tdeliver done (" + (System.currentTimeMillis() - start) + "ms)");
}
Also used : HashMap(java.util.HashMap) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) Resource(org.apache.ivy.plugins.repository.Resource) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) UpdateOptions(org.apache.ivy.plugins.parser.xml.UpdateOptions) SAXException(org.xml.sax.SAXException) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) File(java.io.File) HashSet(java.util.HashSet)

Example 18 with ModuleDescriptor

use of org.apache.ivy.core.module.descriptor.ModuleDescriptor in project ant-ivy by apache.

the class IvyPublishTest method testSimple.

@Test
public void testSimple() throws IOException, ParseException {
    project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-multiconf.xml");
    IvyResolve res = new IvyResolve();
    res.setProject(project);
    res.execute();
    publish.setPubrevision("1.2");
    publish.setResolver("1");
    File art = new File("build/test/publish/resolve-simple-1.2.jar");
    FileUtil.copy(new File("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
    publish.execute();
    // should have do the ivy delivering
    assertTrue(new File("build/test/publish/ivy-1.2.xml").exists());
    // should have published the files with "1" resolver
    assertTrue(new File("test/repositories/1/apache/resolve-simple/ivys/ivy-1.2.xml").exists());
    assertTrue(new File("test/repositories/1/apache/resolve-simple/jars/resolve-simple-1.2.jar").exists());
    // should have updated published ivy version
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), new File("test/repositories/1/apache/resolve-simple/ivys/ivy-1.2.xml").toURI().toURL(), false);
    assertEquals("1.2", md.getModuleRevisionId().getRevision());
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) IvySettings(org.apache.ivy.core.settings.IvySettings) File(java.io.File) Test(org.junit.Test)

Example 19 with ModuleDescriptor

use of org.apache.ivy.core.module.descriptor.ModuleDescriptor in project ant-ivy by apache.

the class IvyPublishTest method testForceDeliver.

@Test
public void testForceDeliver() throws IOException, ParseException {
    project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-latest.xml");
    IvyResolve res = new IvyResolve();
    res.setProject(project);
    res.execute();
    publish.setPubrevision("1.3");
    publish.setResolver("1");
    publish.setSrcivypattern("build/test/publish/ivy-1.3.xml");
    publish.setForcedeliver(true);
    FileUtil.copy(new File("test/java/org/apache/ivy/ant/ivy-latest.xml"), new File("build/test/publish/ivy-1.3.xml"), null);
    File art = new File("build/test/publish/resolve-latest-1.3.jar");
    FileUtil.copy(new File("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
    publish.execute();
    // should have published the files with "1" resolver
    assertTrue(new File("test/repositories/1/apache/resolve-latest/ivys/ivy-1.3.xml").exists());
    assertTrue(new File("test/repositories/1/apache/resolve-latest/jars/resolve-latest-1.3.jar").exists());
    // should have updated published ivy version
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), new File("test/repositories/1/apache/resolve-latest/ivys/ivy-1.3.xml").toURI().toURL(), false);
    assertEquals("1.3", md.getModuleRevisionId().getRevision());
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) IvySettings(org.apache.ivy.core.settings.IvySettings) File(java.io.File) Test(org.junit.Test)

Example 20 with ModuleDescriptor

use of org.apache.ivy.core.module.descriptor.ModuleDescriptor in project ant-ivy by apache.

the class IvyPublishTest method testNoDeliverWithBranch.

@Test
public void testNoDeliverWithBranch() throws IOException, ParseException {
    project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-latest.xml");
    IvyResolve res = new IvyResolve();
    res.setProject(project);
    res.execute();
    publish.setUpdate(true);
    publish.setPubrevision("1.3");
    publish.setPubbranch("BRANCH1");
    publish.setResolver("1");
    publish.setSrcivypattern("build/test/publish/ivy-1.3.xml");
    FileUtil.copy(new File("test/java/org/apache/ivy/ant/ivy-publish.xml"), new File("build/test/publish/ivy-1.3.xml"), null);
    File art = new File("build/test/publish/resolve-latest-1.3.jar");
    FileUtil.copy(new File("test/repositories/1/org1/mod1.1/jars/mod1.1-1.0.jar"), art, null);
    publish.execute();
    // should have published the files with "1" resolver
    assertTrue(new File("test/repositories/1/apache/resolve-latest/ivys/ivy-1.3.xml").exists());
    assertTrue(new File("test/repositories/1/apache/resolve-latest/jars/resolve-latest-1.3.jar").exists());
    // the published ivy version should be ok (ok in ivy-publish file)
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), new File("test/repositories/1/apache/resolve-latest/ivys/ivy-1.3.xml").toURI().toURL(), false);
    assertEquals("BRANCH1", md.getModuleRevisionId().getBranch());
    assertEquals("1.3", md.getModuleRevisionId().getRevision());
    // should not have done delivery (replace dynamic revisions with static ones)
    assertEquals("latest.integration", md.getDependencies()[0].getDependencyRevisionId().getRevision());
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) IvySettings(org.apache.ivy.core.settings.IvySettings) File(java.io.File) Test(org.junit.Test)

Aggregations

ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)284 Test (org.junit.Test)233 File (java.io.File)143 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)114 ResolveReport (org.apache.ivy.core.report.ResolveReport)94 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)80 JarFile (java.util.jar.JarFile)76 ConfigurationResolveReport (org.apache.ivy.core.report.ConfigurationResolveReport)76 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)69 IvySettings (org.apache.ivy.core.settings.IvySettings)60 XmlModuleDescriptorParserTest (org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest)44 Configuration (org.apache.ivy.core.module.descriptor.Configuration)30 Artifact (org.apache.ivy.core.module.descriptor.Artifact)28 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)27 IOException (java.io.IOException)24 ParseException (java.text.ParseException)23 Ivy (org.apache.ivy.Ivy)23 BufferedReader (java.io.BufferedReader)19 FileReader (java.io.FileReader)19 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)17