Search in sources :

Example 46 with ModuleDescriptor

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

the class IvyNodeCallers method addCaller.

/**
 * @param rootModuleConf ditto
 * @param callerNode IvyNode
 * @param callerConf ditto
 * @param requestedConf ditto
 * @param dependencyConfs
 *            '*' must have been resolved
 * @param dd
 *            the dependency revision id asked by the caller
 */
public void addCaller(String rootModuleConf, IvyNode callerNode, String callerConf, String requestedConf, String[] dependencyConfs, DependencyDescriptor dd) {
    ModuleDescriptor md = callerNode.getDescriptor();
    ModuleRevisionId mrid = callerNode.getResolvedId();
    if (mrid.getModuleId().equals(node.getId().getModuleId())) {
        throw new IllegalArgumentException("a module is not authorized to depend on itself: " + node.getId());
    }
    Map<ModuleRevisionId, Caller> callers = callersByRootConf.get(rootModuleConf);
    if (callers == null) {
        callers = new HashMap<>();
        callersByRootConf.put(rootModuleConf, callers);
    }
    Caller caller = callers.get(mrid);
    if (caller == null) {
        caller = new Caller(md, mrid, dd, callerNode.canExclude(rootModuleConf));
        callers.put(mrid, caller);
    }
    caller.addConfiguration(requestedConf, dependencyConfs);
    IvyNode parent = callerNode.getRealNode();
    for (ModuleId mid : parent.getAllCallersModuleIds()) {
        allCallers.put(mid, parent);
    }
    allCallers.put(mrid.getModuleId(), callerNode);
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ModuleId(org.apache.ivy.core.module.id.ModuleId) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId)

Example 47 with ModuleDescriptor

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

the class ResolveEngine method resolve.

/**
 * Resolve dependencies of a module described by an ivy file.
 *
 * @param ivySource URL
 * @param options ResolveOptions
 * @return ResolveReport
 * @throws ParseException if something goes wrong
 * @throws IOException if something goes wrong
 */
public ResolveReport resolve(URL ivySource, ResolveOptions options) throws ParseException, IOException {
    URLResource res = new URLResource(ivySource);
    ModuleDescriptorParser parser = ModuleDescriptorParserRegistry.getInstance().getParser(res);
    Message.verbose("using " + parser + " to parse " + ivySource);
    ModuleDescriptor md = parser.parseDescriptor(settings, ivySource, options.isValidate());
    String revision = options.getRevision();
    if (revision == null && md.getResolvedModuleRevisionId().getRevision() == null) {
        revision = Ivy.getWorkingRevision();
    }
    if (revision != null) {
        md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(md.getModuleRevisionId(), revision));
    }
    return resolve(md, options);
}
Also used : URLResource(org.apache.ivy.plugins.repository.url.URLResource) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ModuleDescriptorParser(org.apache.ivy.plugins.parser.ModuleDescriptorParser)

Example 48 with ModuleDescriptor

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

the class PublishEngine method publish.

/**
 * Publishes a module to the repository. The publish can update the ivy file to publish if
 * update is set to true. In this case it will use the given pubrevision, pubdate and status. If
 * pubdate is null it will default to the current date. If status is null it will default to the
 * current ivy file status (which itself defaults to integration if none is found). If update is
 * false, then if the revision is not the same in the ivy file than the one expected (given as
 * parameter), this method will fail with an IllegalArgumentException. pubdate and status are
 * not used if update is false. extra artifacts can be used to publish more artifacts than
 * actually declared in the ivy file. This can be useful to publish additional metadata or
 * reports. The extra artifacts array can be null (= no extra artifacts), and if non null only
 * the name, type, ext url and extra attributes of the artifacts are really used. Other methods
 * can return null safely.
 *
 * @param mrid ModuleRevisionId
 * @param srcArtifactPattern a Collection of String
 * @param resolverName String
 * @param options PublishOptions
 * @return Collection&lt;Artifact&gt;
 * @throws IOException if something goes wrong
 */
public Collection<Artifact> publish(ModuleRevisionId mrid, Collection<String> srcArtifactPattern, String resolverName, PublishOptions options) throws IOException {
    Message.info(":: publishing :: " + mrid.getModuleId());
    Message.verbose("\tvalidate = " + options.isValidate());
    long start = System.currentTimeMillis();
    options.setSrcIvyPattern(settings.substitute(options.getSrcIvyPattern()));
    if (options.getPubBranch() == null) {
        options.setPubbranch(mrid.getBranch());
    }
    if (options.getPubrevision() == null) {
        options.setPubrevision(mrid.getRevision());
    }
    ModuleRevisionId pubmrid = ModuleRevisionId.newInstance(mrid, options.getPubBranch(), options.getPubrevision());
    // let's find the resolved module descriptor
    ModuleDescriptor md = null;
    if (options.getSrcIvyPattern() != null) {
        File ivyFile = settings.resolveFile(IvyPatternHelper.substitute(options.getSrcIvyPattern(), DefaultArtifact.newIvyArtifact(pubmrid, new Date())));
        if (!ivyFile.exists()) {
            throw new IllegalArgumentException("ivy file to publish not found for " + mrid + ": call deliver before (" + ivyFile + ")");
        }
        URL ivyFileURL = ivyFile.toURI().toURL();
        try {
            md = XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, ivyFileURL, false);
            if (options.isUpdate()) {
                File tmp = File.createTempFile("ivy", ".xml");
                tmp.deleteOnExit();
                String[] confs = replaceWildcards(options.getConfs(), md);
                Set<String> confsToRemove = new HashSet<>(Arrays.asList(md.getConfigurationsNames()));
                confsToRemove.removeAll(Arrays.asList(confs));
                try {
                    XmlModuleDescriptorUpdater.update(ivyFileURL, tmp, new UpdateOptions().setSettings(settings).setStatus(options.getStatus() == null ? md.getStatus() : options.getStatus()).setRevision(options.getPubrevision()).setBranch(options.getPubBranch()).setPubdate(options.getPubdate() == null ? new Date() : options.getPubdate()).setMerge(options.isMerge()).setMergedDescriptor(md).setConfsToExclude(confsToRemove.toArray(new String[confsToRemove.size()])));
                    ivyFile = tmp;
                    // we parse the new file to get updated module descriptor
                    md = XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, ivyFile.toURI().toURL(), false);
                    options.setSrcIvyPattern(ivyFile.getAbsolutePath());
                } catch (SAXException e) {
                    throw new IllegalStateException("bad ivy file for " + mrid + ": " + ivyFile + ": " + e);
                }
            } else if (!options.getPubrevision().equals(md.getModuleRevisionId().getRevision())) {
                throw new IllegalArgumentException("cannot publish " + ivyFile + " as " + options.getPubrevision() + ": bad revision found in ivy file (Revision: " + md.getModuleRevisionId().getRevision() + "). Use forcedeliver or update.");
            }
        } catch (ParseException e) {
            throw new IllegalStateException("bad ivy file for " + mrid + ": " + ivyFile + ": " + e);
        }
    } else {
        ResolutionCacheManager cacheManager = settings.getResolutionCacheManager();
        try {
            md = cacheManager.getResolvedModuleDescriptor(mrid);
        } catch (ParseException e) {
            throw new IllegalStateException("bad ivy file in cache for " + mrid + ": " + e);
        }
        md.setResolvedModuleRevisionId(pubmrid);
    }
    DependencyResolver resolver = settings.getResolver(resolverName);
    if (resolver == null) {
        throw new IllegalArgumentException("unknown resolver " + resolverName);
    }
    // collect all declared artifacts of this module
    Collection<Artifact> missing = publish(md, srcArtifactPattern, resolver, options);
    Message.verbose("\tpublish done (" + (System.currentTimeMillis() - start) + "ms)");
    return missing;
}
Also used : ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Date(java.util.Date) URL(java.net.URL) UpdateOptions(org.apache.ivy.plugins.parser.xml.UpdateOptions) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) MDArtifact(org.apache.ivy.core.module.descriptor.MDArtifact) SAXException(org.xml.sax.SAXException) DependencyResolver(org.apache.ivy.plugins.resolver.DependencyResolver) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ParseException(java.text.ParseException) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 49 with ModuleDescriptor

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

the class ResolveTest method testResolveConflict.

@Test
public void testResolveConflict() throws Exception {
    // mod4.1 v 4.1 depends on
    // - mod1.1 v 1.0 which depends on mod1.2 v 2.0
    // - mod3.1 v 1.1 which depends on mod1.2 v 2.1
    ResolveReport report = ivy.resolve(new File("test/repositories/2/mod4.1/ivy-4.1.xml"), getResolveOptions(new String[] { "*" }));
    assertNotNull(report);
    ModuleDescriptor md = report.getModuleDescriptor();
    assertNotNull(md);
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("org4", "mod4.1", "4.1");
    assertEquals(mrid, md.getModuleRevisionId());
    assertTrue(getResolvedIvyFileInCache(mrid).exists());
    // dependencies
    ConfigurationResolveReport crr = report.getConfigurationReport("default");
    assertNotNull(crr);
    assertEquals(0, crr.getDownloadReports(ModuleRevisionId.newInstance("org1", "mod1.2", "2.0")).length);
    assertEquals(1, crr.getDownloadReports(ModuleRevisionId.newInstance("org1", "mod1.2", "2.1")).length);
    File r = getConfigurationResolveReportInCache(ResolveOptions.getDefaultResolveId(md), "default");
    assertTrue(r.exists());
    final boolean[] found = new boolean[] { false };
    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
    saxParser.parse(r, new DefaultHandler() {

        @Override
        public void startElement(String uri, String localName, String qName, Attributes attributes) {
            if ("revision".equals(qName) && "2.0".equals(attributes.getValue("name"))) {
                found[0] = true;
            }
        }
    });
    // the report should contain the evicted revision
    assertTrue(found[0]);
    assertTrue(getIvyFileInCache(ModuleRevisionId.newInstance("org1", "mod1.1", "1.0")).exists());
    assertTrue(getArchiveFileInCache("org1", "mod1.1", "1.0", "mod1.1", "jar", "jar").exists());
    assertTrue(getIvyFileInCache(ModuleRevisionId.newInstance("org3", "mod3.1", "1.1")).exists());
    assertTrue(getArchiveFileInCache("org3", "mod3.1", "1.1", "mod3.1", "jar", "jar").exists());
    assertFalse(getArchiveFileInCache("org1", "mod1.2", "2.0", "mod1.2", "jar", "jar").exists());
    assertTrue(getIvyFileInCache(ModuleRevisionId.newInstance("org1", "mod1.2", "2.1")).exists());
    assertTrue(getArchiveFileInCache("org1", "mod1.2", "2.1", "mod1.2", "jar", "jar").exists());
}
Also used : ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) Attributes(org.xml.sax.Attributes) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) SAXParser(javax.xml.parsers.SAXParser) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) JarFile(java.util.jar.JarFile) File(java.io.File) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.Test)

Example 50 with ModuleDescriptor

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

the class ResolveTest method testIVY1300.

/**
 * Test case for IVY-1300.
 *
 * @throws Exception if something goes wrong
 * @see <a href="https://issues.apache.org/jira/browse/IVY-1300">IVY-1300</a>
 */
@Test
public void testIVY1300() throws Exception {
    ivy = Ivy.newInstance();
    ivy.configure(new File("test/repositories/IVY-1300/ivysettings.xml"));
    ResolveOptions opts = new ResolveOptions();
    opts.setConfs(new String[] { "*" });
    opts.setResolveId("resolveid");
    opts.setTransitive(true);
    ResolveReport report = ivy.resolve(new File("test/repositories/IVY-1300/assembly-ivy.xml"), opts);
    assertFalse(report.hasError());
    ModuleRevisionId modAExpectedRevId = ModuleRevisionId.newInstance("myorg", "modA", "trunk", "5");
    ModuleRevisionId modBExpectedRevId = ModuleRevisionId.newInstance("myorg", "modB", "releasebranch", "1");
    // check that the resolve report has the expected results, namely that
    // trunk/5 is considered later than branch/1 purely because 5>1. Of
    // course it is more likely that we would want to consider this a
    // 'bad comparison', but this Unit Test is not about that. It is about
    // inconsistency of results between the resolve report and the
    // delivered descriptor. In fact the delivered descriptor is out of
    // step, because retrieve and the report both agree that trunk/5 is
    // selected. Deliver begs to differ.
    Set<ModuleRevisionId> reportMrids = report.getConfigurationReport("default").getModuleRevisionIds();
    assertEquals(new HashSet<>(Arrays.asList(modAExpectedRevId, modBExpectedRevId)), reportMrids);
    DeliverOptions dopts = new DeliverOptions();
    dopts.setGenerateRevConstraint(true);
    dopts.setConfs(new String[] { "*" });
    dopts.setStatus("release");
    dopts.setPubdate(new Date());
    dopts.setResolveId("resolveid");
    String pubrev = "1";
    String deliveryPattern = "build/test/deliver/assembly-[revision].xml";
    ivy.deliver(pubrev, deliveryPattern, dopts);
    // now check that the resolve report has the same info as the delivered descriptor
    File deliveredIvyFile = new File("build/test/deliver/assembly-1.xml");
    assertTrue(deliveredIvyFile.exists());
    ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(ivy.getSettings(), deliveredIvyFile.toURI().toURL(), false);
    DependencyDescriptor[] dds = md.getDependencies();
    assertEquals(2, dds.length);
    assertEquals(ModuleRevisionId.newInstance("myorg", "modB", "releasebranch", "1"), dds[1].getDependencyRevisionId());
    assertEquals(ModuleRevisionId.newInstance("myorg", "modA", "trunk", "5"), dds[0].getDependencyRevisionId());
}
Also used : DeliverOptions(org.apache.ivy.core.deliver.DeliverOptions) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) DependencyDescriptor(org.apache.ivy.core.module.descriptor.DependencyDescriptor) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) JarFile(java.util.jar.JarFile) File(java.io.File) Date(java.util.Date) 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