Search in sources :

Example 1 with UpdateOptions

use of org.apache.ivy.plugins.parser.xml.UpdateOptions 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 2 with UpdateOptions

use of org.apache.ivy.plugins.parser.xml.UpdateOptions 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)

Aggregations

File (java.io.File)2 HashSet (java.util.HashSet)2 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)2 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)2 UpdateOptions (org.apache.ivy.plugins.parser.xml.UpdateOptions)2 SAXException (org.xml.sax.SAXException)2 FileInputStream (java.io.FileInputStream)1 URL (java.net.URL)1 ParseException (java.text.ParseException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Properties (java.util.Properties)1 ResolutionCacheManager (org.apache.ivy.core.cache.ResolutionCacheManager)1 Artifact (org.apache.ivy.core.module.descriptor.Artifact)1 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)1 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)1 MDArtifact (org.apache.ivy.core.module.descriptor.MDArtifact)1 Resource (org.apache.ivy.plugins.repository.Resource)1 DependencyResolver (org.apache.ivy.plugins.resolver.DependencyResolver)1