Search in sources :

Example 76 with Artifact

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

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

the class DefaultRepositoryCacheManager method unpackArtifact.

private void unpackArtifact(Artifact artifact, ArtifactDownloadReport adr, CacheDownloadOptions options) {
    Artifact unpacked = packagingManager.getUnpackedArtifact(artifact);
    if (unpacked == null) {
        // nothing to unpack
        return;
    }
    File archiveFile = getArchiveFileInCache(unpacked, null, false);
    if (archiveFile.exists() && !options.isForce()) {
        adr.setUnpackedLocalFile(archiveFile);
        adr.setUnpackedArtifact(unpacked);
    } else {
        Message.info("\tUnpacking " + artifact.getId());
        try {
            final Artifact unpackedArtifact = packagingManager.unpackArtifact(artifact, adr.getLocalFile(), archiveFile);
            adr.setUnpackedLocalFile(archiveFile);
            adr.setUnpackedArtifact(unpackedArtifact);
        } catch (Exception e) {
            Message.debug(e);
            adr.setDownloadStatus(DownloadStatus.FAILED);
            adr.setDownloadDetails("The packed artifact " + artifact.getId() + " could not be unpacked (" + e.getMessage() + ")");
        }
    }
}
Also used : PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) ParseException(java.text.ParseException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 78 with Artifact

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

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

the class IvyPublish method doExecute.

@Override
public void doExecute() throws BuildException {
    Ivy ivy = getIvyInstance();
    IvySettings settings = ivy.getSettings();
    organisation = getProperty(organisation, settings, "ivy.organisation");
    module = getProperty(module, settings, "ivy.module");
    revision = getProperty(revision, settings, "ivy.revision");
    pubBranch = getProperty(pubBranch, settings, "ivy.deliver.branch");
    pubRevision = getProperty(pubRevision, settings, "ivy.deliver.revision");
    if (artifactspattern.isEmpty()) {
        String p = getProperty(null, settings, "ivy.publish.src.artifacts.pattern");
        if (p != null) {
            artifactspattern.add(p);
        }
    }
    if (srcivypattern == null) {
        srcivypattern = getArtifactspattern();
    }
    status = getProperty(status, settings, "ivy.status");
    if (organisation == null) {
        throw new BuildException("no organisation provided for ivy publish task: " + "It can either be set explicitly via the attribute 'organisation' " + "or via 'ivy.organisation' property or a prior call to <resolve/>");
    }
    if (module == null) {
        throw new BuildException("no module name provided for ivy publish task: " + "It can either be set explicitly via the attribute 'module' " + "or via 'ivy.module' property or a prior call to <resolve/>");
    }
    if (revision == null) {
        throw new BuildException("no module revision provided for ivy publish task: " + "It can either be set explicitly via the attribute 'revision' " + "or via 'ivy.revision' property or a prior call to <resolve/>");
    }
    if (artifactspattern.isEmpty()) {
        throw new BuildException("no artifacts pattern: either provide it through parameter or " + "through ivy.publish.src.artifacts.pattern property");
    }
    if (publishResolverName == null) {
        throw new BuildException("no publish deliver name: please provide it through parameter 'resolver'");
    }
    if ("working".equals(revision)) {
        revision = Ivy.getWorkingRevision();
    }
    Date pubdate = getPubDate(this.pubdate, new Date());
    if (pubRevision == null) {
        if (revision.startsWith("working@")) {
            pubRevision = DateUtil.format(pubdate);
        } else {
            pubRevision = revision;
        }
    }
    if (status == null) {
        throw new BuildException("no status provided: either provide it as parameter " + "or through the ivy.status.default property");
    }
    ModuleRevisionId mrid = ModuleRevisionId.newInstance(organisation, module, revision);
    try {
        File ivyFile = getProject().resolveFile(IvyPatternHelper.substitute(srcivypattern, organisation, module, pubRevision, "ivy", "ivy", "xml"));
        if (publishivy && (!ivyFile.exists() || forcedeliver)) {
            IvyDeliver deliver = new IvyDeliver();
            deliver.setSettingsRef(getSettingsRef());
            deliver.setTaskName(getTaskName());
            deliver.setProject(getProject());
            deliver.setDeliverpattern(getSrcivypattern());
            deliver.setDelivertarget(deliverTarget);
            deliver.setDeliveryList(deliveryList);
            deliver.setModule(getModule());
            deliver.setOrganisation(getOrganisation());
            deliver.setPubdate(DateUtil.format(pubdate));
            deliver.setPubrevision(getPubrevision());
            deliver.setPubbranch(getPubbranch());
            deliver.setRevision(getRevision());
            deliver.setStatus(getStatus());
            deliver.setValidate(doValidate(settings));
            deliver.setReplacedynamicrev(isReplacedynamicrev());
            deliver.setMerge(merge);
            deliver.setConf(conf);
            deliver.execute();
        }
        ivy.publish(mrid, artifactspattern, publishResolverName, new PublishOptions().setPubrevision(getPubrevision()).setPubbranch(getPubbranch()).setSrcIvyPattern(publishivy ? srcivypattern : null).setStatus(getStatus()).setPubdate(pubdate).setExtraArtifacts(artifacts.toArray(new Artifact[artifacts.size()])).setValidate(doValidate(settings)).setOverwrite(overwrite).setUpdate(update).setMerge(merge).setWarnOnMissing(warnonmissing).setHaltOnMissing(haltonmissing).setConfs(splitToArray(conf)));
    } catch (Exception e) {
        if (e instanceof BuildException) {
            throw (BuildException) e;
        }
        throw new BuildException("impossible to publish artifacts for " + mrid + ": " + e, e);
    }
}
Also used : IvySettings(org.apache.ivy.core.settings.IvySettings) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) BuildException(org.apache.tools.ant.BuildException) PublishOptions(org.apache.ivy.core.publish.PublishOptions) Ivy(org.apache.ivy.Ivy) File(java.io.File) Date(java.util.Date) Artifact(org.apache.ivy.core.module.descriptor.Artifact) BuildException(org.apache.tools.ant.BuildException)

Example 80 with Artifact

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

the class PomModuleDescriptorParserTest method testParentGroupId.

@Test
public void testParentGroupId() throws Exception {
    ModuleDescriptor md = PomModuleDescriptorParser.getInstance().parseDescriptor(settings, getClass().getResource("test-parent.groupid.pom"), false);
    assertNotNull(md);
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("org.apache", "test", "1.0");
    assertEquals(mrid, md.getModuleRevisionId());
    Artifact[] artifact = md.getArtifacts("master");
    assertEquals(1, artifact.length);
    assertEquals(mrid, artifact[0].getModuleRevisionId());
    assertEquals("test", artifact[0].getName());
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Artifact(org.apache.ivy.core.module.descriptor.Artifact) XmlModuleDescriptorParserTest(org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest) Test(org.junit.Test)

Aggregations

Artifact (org.apache.ivy.core.module.descriptor.Artifact)106 Test (org.junit.Test)68 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)59 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)55 File (java.io.File)53 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)31 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)30 ModuleDescriptor (org.apache.ivy.core.module.descriptor.ModuleDescriptor)28 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)25 Date (java.util.Date)22 DownloadReport (org.apache.ivy.core.report.DownloadReport)20 DefaultModuleDescriptor (org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor)15 DependencyDescriptor (org.apache.ivy.core.module.descriptor.DependencyDescriptor)13 Configuration (org.apache.ivy.core.module.descriptor.Configuration)10 IOException (java.io.IOException)9 PublishArtifact (org.gradle.api.artifacts.PublishArtifact)9 ParseException (java.text.ParseException)8 XmlModuleDescriptorParserTest (org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParserTest)8 ResolveReport (org.apache.ivy.core.report.ResolveReport)7 DownloadOptions (org.apache.ivy.core.resolve.DownloadOptions)7