Search in sources :

Example 11 with Artifact

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

the class DefaultRepositoryCacheManager method downloadRepositoryResource.

public ArtifactDownloadReport downloadRepositoryResource(final Resource resource, String name, String type, String extension, CacheResourceOptions options, Repository repository) {
    String hash = computeResourceNameHash(resource);
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("_repository_metadata_", hash, Ivy.getWorkingRevision());
    Artifact artifact = new DefaultArtifact(mrid, null, name, type, extension);
    final ArtifactDownloadReport adr = new ArtifactDownloadReport(artifact);
    boolean useOrigin = isUseOrigin();
    try {
        DownloadListener listener = options.getListener();
        if (listener != null) {
            listener.needArtifact(this, artifact);
        }
        ArtifactOrigin savedOrigin = getSavedArtifactOrigin(artifact);
        File archiveFile = getArchiveFileInCache(artifact, savedOrigin, useOrigin);
        ArtifactOrigin origin = new ArtifactOrigin(artifact, resource.isLocal(), resource.getName());
        if (!options.isForce() && // if the local file has been checked to be up to date enough recently, don't download
        checkCacheUptodate(archiveFile, resource, savedOrigin, origin, options.getTtl())) {
            if (archiveFile.exists()) {
                saveArtifactOrigin(artifact, origin);
                adr.setDownloadStatus(DownloadStatus.NO);
                adr.setSize(archiveFile.length());
                adr.setArtifactOrigin(savedOrigin);
                adr.setLocalFile(archiveFile);
            } else {
                // we trust the cache to says that the resource doesn't exist
                adr.setDownloadStatus(DownloadStatus.FAILED);
                adr.setDownloadDetails("Remote resource is known to not exist");
            }
        } else {
            long start = System.currentTimeMillis();
            origin.setLastChecked(start);
            try {
                ResolvedResource artifactRef = new ResolvedResource(resource, Ivy.getWorkingRevision());
                if (useOrigin && resource.isLocal()) {
                    saveArtifactOrigin(artifact, origin);
                    archiveFile = getArchiveFileInCache(artifact, origin);
                    adr.setDownloadStatus(DownloadStatus.NO);
                    adr.setSize(archiveFile.length());
                    adr.setArtifactOrigin(origin);
                    adr.setLocalFile(archiveFile);
                } else {
                    if (listener != null) {
                        listener.startArtifactDownload(this, artifactRef, artifact, origin);
                    }
                    // actual download
                    if (archiveFile.exists()) {
                        archiveFile.delete();
                    }
                    File part = new File(archiveFile.getAbsolutePath() + ".part");
                    repository.get(resource.getName(), part);
                    if (!part.renameTo(archiveFile)) {
                        throw new IOException("impossible to move part file to definitive one: " + part + " -> " + archiveFile);
                    }
                    adr.setSize(archiveFile.length());
                    saveArtifactOrigin(artifact, origin);
                    adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
                    adr.setDownloadStatus(DownloadStatus.SUCCESSFUL);
                    adr.setArtifactOrigin(origin);
                    adr.setLocalFile(archiveFile);
                }
            } catch (Exception ex) {
                Message.debug(ex);
                origin.setExist(false);
                saveArtifactOrigin(artifact, origin);
                adr.setDownloadStatus(DownloadStatus.FAILED);
                adr.setDownloadDetails(ex.getMessage());
                adr.setDownloadTimeMillis(System.currentTimeMillis() - start);
            }
        }
        if (listener != null) {
            listener.endArtifactDownload(this, artifact, adr, archiveFile);
        }
        return adr;
    } finally {
        unlockMetadataArtifact(mrid);
    }
}
Also used : ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) MetadataArtifactDownloadReport(org.apache.ivy.core.report.MetadataArtifactDownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) IOException(java.io.IOException) 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) ResolvedResource(org.apache.ivy.plugins.resolver.util.ResolvedResource) PropertiesFile(org.apache.ivy.util.PropertiesFile) File(java.io.File) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact)

Example 12 with Artifact

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

the class IvyInfo method setProperties.

private void setProperties(ModuleDescriptor md, ModuleRevisionId mrid) {
    getProject().setProperty(property + ".organisation", mrid.getOrganisation());
    getProject().setProperty(property + ".module", mrid.getName());
    if (mrid.getBranch() != null) {
        getProject().setProperty(property + ".branch", mrid.getBranch());
    }
    getProject().setProperty(property + ".revision", mrid.getRevision());
    getProject().setProperty(property + ".status", md.getStatus());
    if (md.getPublicationDate() != null) {
        getProject().setProperty(property + ".publication", Long.toString(md.getPublicationDate().getTime()));
    }
    for (Map.Entry<String, String> entry : mrid.getExtraAttributes().entrySet()) {
        getProject().setProperty(property + ".extra." + entry.getKey(), entry.getValue());
    }
    getProject().setProperty(property + ".configurations", mergeConfs(md.getConfigurationsNames()));
    // store the public configurations in a separate property
    List<String> publicConfigsList = new ArrayList<>();
    for (Configuration config : md.getConfigurations()) {
        String name = config.getName();
        if (PUBLIC.equals(config.getVisibility())) {
            publicConfigsList.add(name);
        }
        if (config.getDescription() != null) {
            getProject().setProperty(property + ".configuration." + name + ".desc", config.getDescription());
        }
    }
    String[] publicConfigs = publicConfigsList.toArray(new String[publicConfigsList.size()]);
    getProject().setProperty(property + ".public.configurations", mergeConfs(publicConfigs));
    List<Artifact> artifacts = Arrays.asList(md.getAllArtifacts());
    for (Artifact artifact : artifacts) {
        int id = artifacts.indexOf(artifact) + 1;
        getProject().setProperty(property + ".artifact." + id + ".name", artifact.getName());
        getProject().setProperty(property + ".artifact." + id + ".type", artifact.getType());
        getProject().setProperty(property + ".artifact." + id + ".ext", artifact.getExt());
        getProject().setProperty(property + ".artifact." + id + ".conf", mergeConfs(artifact.getConfigurations()));
        for (Map.Entry<String, String> entry : artifact.getExtraAttributes().entrySet()) {
            getProject().setProperty(property + ".artifact." + id + ".extra." + entry.getKey(), entry.getValue());
        }
    }
}
Also used : Configuration(org.apache.ivy.core.module.descriptor.Configuration) ArrayList(java.util.ArrayList) Map(java.util.Map) Artifact(org.apache.ivy.core.module.descriptor.Artifact)

Example 13 with Artifact

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

the class IvyRetrieve method doExecute.

@SuppressWarnings("deprecation")
@Override
public void doExecute() throws BuildException {
    prepareAndCheck();
    if (!getAllowedLogOptions().contains(getLog())) {
        throw new BuildException("invalid option for 'log': " + getLog() + ". Available options are " + getAllowedLogOptions());
    }
    pattern = getProperty(pattern, getSettings(), "ivy.retrieve.pattern");
    try {
        final Filter<Artifact> artifactFilter = getArtifactFilter();
        final RetrieveOptions retrieveOptions = (RetrieveOptions) new RetrieveOptions().setLog(getLog());
        retrieveOptions.setConfs(splitToArray(getConf())).setDestArtifactPattern(pattern).setDestIvyPattern(ivypattern).setArtifactFilter(artifactFilter).setSync(sync).setOverwriteMode(getOverwriteMode()).setUseOrigin(isUseOrigin()).setMakeSymlinks(symlink).setResolveId(getResolveId()).setMapper(mapper == null ? null : new MapperAdapter(mapper));
        // only set this if the user has explicitly enabled this deprecated option
        if (symlinkmass) {
            retrieveOptions.setMakeSymlinksInMass(symlinkmass);
        }
        final RetrieveReport report = getIvyInstance().retrieve(getResolvedMrid(), retrieveOptions);
        int targetsCopied = report.getNbrArtifactsCopied();
        boolean haveTargetsBeenCopied = targetsCopied > 0;
        getProject().setProperty("ivy.nb.targets.copied", String.valueOf(targetsCopied));
        getProject().setProperty("ivy.targets.copied", String.valueOf(haveTargetsBeenCopied));
        if (getPathId() != null) {
            Path path = new Path(getProject());
            getProject().addReference(getPathId(), path);
            for (File file : report.getRetrievedFiles()) {
                path.createPathElement().setLocation(file);
            }
        }
        if (getSetId() != null) {
            FileSet fileset = new FileSet();
            fileset.setProject(getProject());
            getProject().addReference(getSetId(), fileset);
            fileset.setDir(report.getRetrieveRoot());
            for (File file : report.getRetrievedFiles()) {
                PatternSet.NameEntry ne = fileset.createInclude();
                ne.setName(getPath(report.getRetrieveRoot(), file));
            }
        }
    } catch (Exception ex) {
        throw new BuildException("impossible to ivy retrieve: " + ex, ex);
    }
}
Also used : Path(org.apache.tools.ant.types.Path) FileSet(org.apache.tools.ant.types.FileSet) RetrieveOptions(org.apache.ivy.core.retrieve.RetrieveOptions) Artifact(org.apache.ivy.core.module.descriptor.Artifact) BuildException(org.apache.tools.ant.BuildException) RetrieveReport(org.apache.ivy.core.retrieve.RetrieveReport) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) PatternSet(org.apache.tools.ant.types.PatternSet)

Example 14 with Artifact

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

the class TestHelper method fillRepository.

/**
 * Fills a repository with a set of module, using empty files for published artifacts.
 *
 * @param resolver
 *            the resolver to use to publish the modules
 * @param mds
 *            the descriptors of the modules to put in the repository
 * @throws IOException
 *             if an IO problem occurs while filling the repository
 */
public static void fillRepository(DependencyResolver resolver, Collection<ModuleDescriptor> mds) throws IOException {
    File tmp = File.createTempFile("ivy", "tmp");
    try {
        for (ModuleDescriptor md : mds) {
            boolean overwrite = false;
            resolver.beginPublishTransaction(md.getModuleRevisionId(), overwrite);
            boolean published = false;
            try {
                XmlModuleDescriptorWriter.write(md, tmp);
                resolver.publish(md.getMetadataArtifact(), tmp, overwrite);
                tmp.delete();
                tmp.createNewFile();
                for (Artifact artifact : md.getAllArtifacts()) {
                    resolver.publish(artifact, tmp, overwrite);
                }
                resolver.commitPublishTransaction();
                published = true;
            } finally {
                if (!published) {
                    resolver.abortPublishTransaction();
                }
            }
        }
    } finally {
        tmp.delete();
    }
}
Also used : DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) ModuleDescriptor(org.apache.ivy.core.module.descriptor.ModuleDescriptor) File(java.io.File) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact)

Example 15 with Artifact

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

the class PomModuleDescriptorWriter method setModuleVariables.

private static void setModuleVariables(ModuleDescriptor md, IvyVariableContainer variables, PomWriterOptions options) {
    ModuleRevisionId mrid = md.getModuleRevisionId();
    variables.setVariable("ivy.pom.groupId", mrid.getOrganisation(), true);
    String artifactId = options.getArtifactName();
    if (artifactId == null) {
        artifactId = mrid.getName();
    }
    String packaging = options.getArtifactPackaging();
    if (packaging == null) {
        // find artifact to determine the packaging
        Artifact artifact = findArtifact(md, artifactId);
        if (artifact == null) {
            // no suitable artifact found, default to 'pom'
            packaging = "pom";
        } else {
            packaging = artifact.getType();
        }
    }
    variables.setVariable("ivy.pom.artifactId", artifactId, true);
    variables.setVariable("ivy.pom.packaging", packaging, true);
    if (mrid.getRevision() != null) {
        variables.setVariable("ivy.pom.version", mrid.getRevision(), true);
    }
    if (options.getDescription() != null) {
        variables.setVariable("ivy.pom.description", options.getDescription(), true);
    } else if (!isNullOrEmpty(md.getDescription())) {
        variables.setVariable("ivy.pom.description", md.getDescription(), true);
    }
    if (md.getHomePage() != null) {
        variables.setVariable("ivy.pom.url", md.getHomePage(), true);
    }
}
Also used : ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Artifact(org.apache.ivy.core.module.descriptor.Artifact)

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