Search in sources :

Example 31 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class RetrieveEngine method retrieve.

public RetrieveReport retrieve(ModuleRevisionId mrid, RetrieveOptions options) throws IOException {
    RetrieveReport report = new RetrieveReport();
    ModuleId moduleId = mrid.getModuleId();
    if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
        Message.info(":: retrieving :: " + moduleId + (options.isSync() ? " [sync]" : ""));
    } else {
        Message.verbose(":: retrieving :: " + moduleId + (options.isSync() ? " [sync]" : ""));
    }
    Message.verbose("\tcheckUpToDate=" + settings.isCheckUpToDate());
    long start = System.currentTimeMillis();
    String destFilePattern = IvyPatternHelper.substituteVariables(options.getDestArtifactPattern(), settings.getVariables());
    String destIvyPattern = IvyPatternHelper.substituteVariables(options.getDestIvyPattern(), settings.getVariables());
    String[] confs = getConfs(mrid, options);
    if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
        Message.info("\tconfs: " + Arrays.asList(confs));
    } else {
        Message.verbose("\tconfs: " + Arrays.asList(confs));
    }
    if (this.eventManager != null) {
        this.eventManager.fireIvyEvent(new StartRetrieveEvent(mrid, confs, options));
    }
    try {
        Map<ArtifactDownloadReport, Set<String>> artifactsToCopy = determineArtifactsToCopy(mrid, destFilePattern, options);
        File fileRetrieveRoot = settings.resolveFile(IvyPatternHelper.getTokenRoot(destFilePattern));
        report.setRetrieveRoot(fileRetrieveRoot);
        File ivyRetrieveRoot = destIvyPattern == null ? null : settings.resolveFile(IvyPatternHelper.getTokenRoot(destIvyPattern));
        Collection<File> targetArtifactsStructure = new HashSet<>();
        // Set(File) set of all paths which should be present at then end of retrieve (useful
        // for sync)
        // same for ivy files
        Collection<File> targetIvysStructure = new HashSet<>();
        // do retrieve
        long totalCopiedSize = 0;
        for (Map.Entry<ArtifactDownloadReport, Set<String>> artifactAndPaths : artifactsToCopy.entrySet()) {
            ArtifactDownloadReport artifact = artifactAndPaths.getKey();
            File archive = artifact.getLocalFile();
            if (artifact.getUnpackedLocalFile() != null) {
                archive = artifact.getUnpackedLocalFile();
            }
            if (archive == null) {
                Message.verbose("\tno local file available for " + artifact + ": skipping");
                continue;
            }
            Message.verbose("\tretrieving " + archive);
            for (String path : artifactAndPaths.getValue()) {
                IvyContext.getContext().checkInterrupted();
                File destFile = settings.resolveFile(path);
                if (!settings.isCheckUpToDate() || !upToDate(archive, destFile, options)) {
                    Message.verbose("\t\tto " + destFile);
                    if (this.eventManager != null) {
                        this.eventManager.fireIvyEvent(new StartRetrieveArtifactEvent(artifact, destFile));
                    }
                    if (options.isMakeSymlinks()) {
                        boolean symlinkCreated;
                        try {
                            symlinkCreated = FileUtil.symlink(archive, destFile, true);
                        } catch (IOException ioe) {
                            symlinkCreated = false;
                            // warn about the inability to create a symlink
                            Message.warn("symlink creation failed at path " + destFile, ioe);
                        }
                        if (!symlinkCreated) {
                            // since symlink creation failed, let's attempt to an actual copy instead
                            Message.info("Attempting a copy operation (since symlink creation failed) at path " + destFile);
                            FileUtil.copy(archive, destFile, null, true);
                        }
                    } else {
                        FileUtil.copy(archive, destFile, null, true);
                    }
                    if (this.eventManager != null) {
                        this.eventManager.fireIvyEvent(new EndRetrieveArtifactEvent(artifact, destFile));
                    }
                    totalCopiedSize += FileUtil.getFileLength(destFile);
                    report.addCopiedFile(destFile, artifact);
                } else {
                    Message.verbose("\t\tto " + destFile + " [NOT REQUIRED]");
                    report.addUpToDateFile(destFile, artifact);
                }
                if ("ivy".equals(artifact.getType())) {
                    targetIvysStructure.addAll(FileUtil.getPathFiles(ivyRetrieveRoot, destFile));
                } else {
                    Collection<File> files = FileUtil.listAll(destFile, Collections.<String>emptyList());
                    for (File file : files) {
                        targetArtifactsStructure.addAll(FileUtil.getPathFiles(fileRetrieveRoot, file));
                    }
                }
            }
        }
        if (options.isSync()) {
            Message.verbose("\tsyncing...");
            String[] ignorableFilenames = settings.getIgnorableFilenames();
            Collection<String> ignoreList = Arrays.asList(ignorableFilenames);
            Collection<File> existingArtifacts = FileUtil.listAll(fileRetrieveRoot, ignoreList);
            Collection<File> existingIvys = (ivyRetrieveRoot == null) ? null : FileUtil.listAll(ivyRetrieveRoot, ignoreList);
            if (fileRetrieveRoot.equals(ivyRetrieveRoot)) {
                targetArtifactsStructure.addAll(targetIvysStructure);
                existingArtifacts.addAll(existingIvys);
                sync(targetArtifactsStructure, existingArtifacts);
            } else {
                sync(targetArtifactsStructure, existingArtifacts);
                if (existingIvys != null) {
                    sync(targetIvysStructure, existingIvys);
                }
            }
        }
        long elapsedTime = System.currentTimeMillis() - start;
        String msg = "\t" + report.getNbrArtifactsCopied() + " artifacts copied" + (settings.isCheckUpToDate() ? (", " + report.getNbrArtifactsUpToDate() + " already retrieved") : "") + " (" + (totalCopiedSize / KILO) + "kB/" + elapsedTime + "ms)";
        if (LogOptions.LOG_DEFAULT.equals(options.getLog())) {
            Message.info(msg);
        } else {
            Message.verbose(msg);
        }
        Message.verbose("\tretrieve done (" + (elapsedTime) + "ms)");
        if (this.eventManager != null) {
            this.eventManager.fireIvyEvent(new EndRetrieveEvent(mrid, confs, elapsedTime, report.getNbrArtifactsCopied(), report.getNbrArtifactsUpToDate(), totalCopiedSize, options));
        }
        return report;
    } catch (Exception ex) {
        throw new RuntimeException("problem during retrieve of " + moduleId + ": " + ex, ex);
    }
}
Also used : EndRetrieveEvent(org.apache.ivy.core.event.retrieve.EndRetrieveEvent) HashSet(java.util.HashSet) Set(java.util.Set) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) IOException(java.io.IOException) ParseException(java.text.ParseException) IOException(java.io.IOException) ModuleId(org.apache.ivy.core.module.id.ModuleId) StartRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.StartRetrieveArtifactEvent) StartRetrieveEvent(org.apache.ivy.core.event.retrieve.StartRetrieveEvent) EndRetrieveArtifactEvent(org.apache.ivy.core.event.retrieve.EndRetrieveArtifactEvent) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 32 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class RetrieveEngine method getConflictResolvingPolicy.

/**
 * The returned comparator should consider greater the artifact which gains the conflict battle.
 * This is used only during retrieve... prefer resolve conflict manager to resolve conflicts.
 *
 * @return Comparator&lt;ArtifactDownloadReport&gt;
 */
private Comparator<ArtifactDownloadReport> getConflictResolvingPolicy() {
    return new Comparator<ArtifactDownloadReport>() {

        // younger conflict resolving policy
        public int compare(ArtifactDownloadReport o1, ArtifactDownloadReport o2) {
            Artifact a1 = o1.getArtifact();
            Artifact a2 = o2.getArtifact();
            if (a1.getPublicationDate().after(a2.getPublicationDate())) {
                // a1 is after a2 <=> a1 is younger than a2 <=> a1 wins the conflict battle
                return +1;
            } else if (a1.getPublicationDate().before(a2.getPublicationDate())) {
                // a1 is before a2 <=> a2 is younger than a1 <=> a2 wins the conflict battle
                return -1;
            } else {
                return 0;
            }
        }
    };
}
Also used : ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) Artifact(org.apache.ivy.core.module.descriptor.Artifact) Comparator(java.util.Comparator)

Example 33 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class ResolveTest method testResolveWithConflictManagerDefinedInModule.

/**
 * Test case for IVY-465.
 *
 * @throws Exception if something goes wrong
 * @see <a href="https://issues.apache.org/jira/browse/IVY-465">IVY-465</a>
 */
@Test
public void testResolveWithConflictManagerDefinedInModule() throws Exception {
    // #M1;1.0 -> #M2;1.0
    // #M2;1.0 -> {org1#mod1.2;1.1 org1#mod1.2;2.0} with
    // <conflict org="org1" module="mod1.2" rev="1.1,2.0" />
    ResolveReport report = ivy.resolve(new File("test/repositories/1/IVY-465/M1/ivys/ivy-1.0.xml"), getResolveOptions(new String[] { "*" }));
    assertFalse(report.hasError());
    ArtifactDownloadReport[] adrs = report.getConfigurationReport("default").getDownloadedArtifactsReports();
    assertEquals(2, adrs.length);
    assertEquals("1.1", adrs[0].getArtifact().getId().getRevision());
    assertEquals("2.0", adrs[1].getArtifact().getId().getRevision());
}
Also used : ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) JarFile(java.util.jar.JarFile) File(java.io.File) Test(org.junit.Test)

Example 34 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class ResolveTest method testResolveWithRetainingArtifactNameAndExtraAttributes.

@Test
public void testResolveWithRetainingArtifactNameAndExtraAttributes() throws Exception {
    ((DefaultRepositoryCacheManager) ivy.getSettings().getDefaultRepositoryCacheManager()).setArtifactPattern(ivy.substitute("[module]/[originalname].[ext]"));
    ResolveReport report = ivy.resolve(new File("test/repositories/2/mod15.4/ivy-1.1.xml"), getResolveOptions(new String[] { "default" }).setValidate(false));
    assertNotNull(report);
    Map<String, String> extra = new HashMap<>();
    extra.put("extra", "foo");
    ArtifactDownloadReport[] dReports = report.getConfigurationReport("default").getDownloadReports(ModuleRevisionId.newInstance("org15", "mod15.3", "1.1", extra));
    assertNotNull(dReports);
    assertEquals("number of downloaded artifacts not correct", 1, dReports.length);
    Artifact artifact = dReports[0].getArtifact();
    assertNotNull(artifact);
    String cachePath = getArchivePathInCache(artifact);
    assertTrue("artifact name has not been retained: " + cachePath, cachePath.endsWith("library.jar"));
    dReports = report.getConfigurationReport("default").getDownloadReports(ModuleRevisionId.newInstance("org14", "mod14.1", "1.1"));
    assertNotNull(dReports);
    assertEquals("number of downloaded artifacts not correct", 1, dReports.length);
    artifact = dReports[0].getArtifact();
    assertNotNull(artifact);
    cachePath = getArchivePathInCache(artifact);
    assertTrue("artifact name has not been retained: " + cachePath, cachePath.endsWith("mod14.1-1.1.jar"));
}
Also used : ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) HashMap(java.util.HashMap) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) DefaultRepositoryCacheManager(org.apache.ivy.core.cache.DefaultRepositoryCacheManager) JarFile(java.util.jar.JarFile) File(java.io.File) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Example 35 with ArtifactDownloadReport

use of org.apache.ivy.core.report.ArtifactDownloadReport in project ant-ivy by apache.

the class ResolveTest method testResolveWithRetainingArtifactName.

@Test
public void testResolveWithRetainingArtifactName() throws Exception {
    ((DefaultRepositoryCacheManager) ivy.getSettings().getDefaultRepositoryCacheManager()).setArtifactPattern(ivy.substitute("[module]/[originalname].[ext]"));
    ResolveReport report = ivy.resolve(new File("test/repositories/2/mod15.2/ivy-1.1.xml"), getResolveOptions(new String[] { "default" }));
    assertNotNull(report);
    ArtifactDownloadReport[] dReports = report.getConfigurationReport("default").getDownloadReports(ModuleRevisionId.newInstance("org15", "mod15.1", "1.1"));
    assertNotNull(dReports);
    assertEquals("number of downloaded artifacts not correct", 1, dReports.length);
    Artifact artifact = dReports[0].getArtifact();
    assertNotNull(artifact);
    String cachePath = getArchivePathInCache(artifact);
    assertTrue("artifact name has not been retained: " + cachePath, cachePath.endsWith("library.jar"));
    dReports = report.getConfigurationReport("default").getDownloadReports(ModuleRevisionId.newInstance("org14", "mod14.1", "1.1"));
    assertNotNull(dReports);
    assertEquals("number of downloaded artifacts not correct", 1, dReports.length);
    artifact = dReports[0].getArtifact();
    assertNotNull(artifact);
    cachePath = getArchivePathInCache(artifact);
    assertTrue("artifact name has not been retained: " + cachePath, cachePath.endsWith("mod14.1-1.1.jar"));
}
Also used : ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) DefaultRepositoryCacheManager(org.apache.ivy.core.cache.DefaultRepositoryCacheManager) JarFile(java.util.jar.JarFile) File(java.io.File) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Aggregations

ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)64 File (java.io.File)33 Artifact (org.apache.ivy.core.module.descriptor.Artifact)29 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)26 Test (org.junit.Test)25 DownloadReport (org.apache.ivy.core.report.DownloadReport)23 DefaultDependencyDescriptor (org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor)18 ResolvedModuleRevision (org.apache.ivy.core.resolve.ResolvedModuleRevision)18 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)17 ResolveReport (org.apache.ivy.core.report.ResolveReport)15 ConfigurationResolveReport (org.apache.ivy.core.report.ConfigurationResolveReport)11 IOException (java.io.IOException)10 DownloadOptions (org.apache.ivy.core.resolve.DownloadOptions)10 URL (java.net.URL)8 JarFile (java.util.jar.JarFile)8 Date (java.util.Date)7 MetadataArtifactDownloadReport (org.apache.ivy.core.report.MetadataArtifactDownloadReport)7 URLResource (org.apache.ivy.plugins.repository.url.URLResource)7 ArtifactOrigin (org.apache.ivy.core.cache.ArtifactOrigin)6 MalformedURLException (java.net.MalformedURLException)5