Search in sources :

Example 31 with Artifact

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

the class URLResolverTest method testFile.

@Test
public void testFile() throws Exception {
    URLResolver resolver = new URLResolver();
    resolver.setSettings(settings);
    String rootpath = new File("test/repositories/1").toURI().toURL().toExternalForm();
    resolver.addIvyPattern(rootpath + "/[organisation]/[module]/ivys/ivy-[revision].xml");
    resolver.addArtifactPattern(rootpath + "/[organisation]/[module]/[type]s/[artifact]-[revision].[type]");
    resolver.setName("test");
    assertEquals("test", resolver.getName());
    ModuleRevisionId mrid = ModuleRevisionId.newInstance("org1", "mod1.1", "1.0");
    ResolvedModuleRevision rmr = resolver.getDependency(new DefaultDependencyDescriptor(mrid, false), data);
    assertNotNull(rmr);
    assertEquals(mrid, rmr.getId());
    Date pubdate = new GregorianCalendar(2004, 10, 1, 11, 0, 0).getTime();
    assertEquals(pubdate, rmr.getPublicationDate());
    // test to ask to download
    DefaultArtifact artifact = new DefaultArtifact(mrid, pubdate, "mod1.1", "jar", "jar");
    DownloadReport report = resolver.download(new Artifact[] { artifact }, downloadOptions());
    assertNotNull(report);
    assertEquals(1, report.getArtifactsReports().length);
    ArtifactDownloadReport ar = report.getArtifactReport(artifact);
    assertNotNull(ar);
    assertEquals(artifact, ar.getArtifact());
    assertEquals(DownloadStatus.SUCCESSFUL, ar.getDownloadStatus());
    // test to ask to download again, should use cache
    report = resolver.download(new Artifact[] { artifact }, downloadOptions());
    assertNotNull(report);
    assertEquals(1, report.getArtifactsReports().length);
    ar = report.getArtifactReport(artifact);
    assertNotNull(ar);
    assertEquals(artifact, ar.getArtifact());
    assertEquals(DownloadStatus.NO, ar.getDownloadStatus());
}
Also used : DownloadReport(org.apache.ivy.core.report.DownloadReport) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) GregorianCalendar(java.util.GregorianCalendar) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ResolvedModuleRevision(org.apache.ivy.core.resolve.ResolvedModuleRevision) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) File(java.io.File) Date(java.util.Date) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) Test(org.junit.Test)

Example 32 with Artifact

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

the class ConfigurationResolveReport method addDependency.

public void addDependency(IvyNode node, DownloadReport report) {
    dependencies.put(node.getId(), node);
    dependencies.put(node.getResolvedId(), node);
    List<ArtifactDownloadReport> adrs = new ArrayList<>();
    for (Artifact artifact : node.getArtifacts(conf)) {
        ArtifactDownloadReport artifactReport = report.getArtifactReport(artifact);
        if (artifactReport != null) {
            adrs.add(artifactReport);
        } else {
            Message.debug("no report found for " + artifact);
        }
    }
    dependencyReports.put(node, adrs);
}
Also used : ArrayList(java.util.ArrayList) Artifact(org.apache.ivy.core.module.descriptor.Artifact)

Example 33 with Artifact

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

the class ResolveReport method toFixedModuleDescriptor.

public ModuleDescriptor toFixedModuleDescriptor(IvySettings settings, List<ModuleId> midToKeep) {
    DefaultModuleDescriptor fixedmd = new DefaultModuleDescriptor(md.getModuleRevisionId(), md.getStatus(), new Date());
    // copy namespaces
    for (Map.Entry<String, String> ns : md.getExtraAttributesNamespaces().entrySet()) {
        fixedmd.addExtraAttributeNamespace(ns.getKey(), ns.getValue());
    }
    // copy info
    fixedmd.setDescription(md.getDescription());
    fixedmd.setHomePage(md.getHomePage());
    fixedmd.getExtraInfos().addAll(md.getExtraInfos());
    // copy configurations
    List<String> resolvedConfs = Arrays.asList(getConfigurations());
    for (String conf : resolvedConfs) {
        fixedmd.addConfiguration(new Configuration(conf));
    }
    // copy artifacts
    for (String conf : resolvedConfs) {
        for (Artifact a : md.getArtifacts(conf)) {
            fixedmd.addArtifact(conf, a);
        }
    }
    // add resolved dependencies
    for (IvyNode dep : dependencies) {
        ModuleRevisionId depMrid;
        boolean force;
        if (midToKeep != null && midToKeep.contains(dep.getModuleId())) {
            depMrid = dep.getId();
            force = false;
        } else {
            depMrid = dep.getResolvedId();
            force = true;
        }
        DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(fixedmd, depMrid, force, false, false);
        boolean evicted = true;
        for (String rootConf : dep.getRootModuleConfigurations()) {
            if (dep.isEvicted(rootConf)) {
                continue;
            }
            evicted = false;
            for (String targetConf : dep.getConfigurations(rootConf)) {
                dd.addDependencyConfiguration(rootConf, targetConf);
            }
        }
        if (!evicted) {
            fixedmd.addDependency(dd);
        }
    }
    return fixedmd;
}
Also used : Configuration(org.apache.ivy.core.module.descriptor.Configuration) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) DefaultDependencyDescriptor(org.apache.ivy.core.module.descriptor.DefaultDependencyDescriptor) DefaultModuleDescriptor(org.apache.ivy.core.module.descriptor.DefaultModuleDescriptor) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Date(java.util.Date) Artifact(org.apache.ivy.core.module.descriptor.Artifact) IvyNode(org.apache.ivy.core.resolve.IvyNode)

Example 34 with Artifact

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

the class IvyNode method getArtifacts.

/**
 * Returns the artifacts of this dependency required in the configurations themselves required
 * in the given root module configuration
 *
 * @param rootModuleConf String
 * @return array of {@link Artifact}s
 */
public Artifact[] getArtifacts(String rootModuleConf) {
    // first we look for the dependency configurations required
    // in the given root module configuration
    String[] confs = getConfigurations(rootModuleConf);
    if (confs == null || confs.length == 0) {
        // no configuration required => no artifact required
        return new Artifact[0];
    }
    if (md == null) {
        throw new IllegalStateException("impossible to get artifacts when data has not been loaded. IvyNode = " + this);
    }
    // the set we fill before returning
    Set<Artifact> artifacts = new HashSet<>();
    // we check if we have dependencyArtifacts includes description for this rootModuleConf
    Set<DependencyArtifactDescriptor> dependencyArtifacts = usage.getDependencyArtifactsSet(rootModuleConf);
    if (md.isDefault() && dependencyArtifacts != null && !dependencyArtifacts.isEmpty()) {
        addArtifactsFromOwnUsage(artifacts, dependencyArtifacts);
        addArtifactsFromMergedUsage(rootModuleConf, artifacts);
    } else {
        Set<IncludeRule> includes = new LinkedHashSet<>();
        addAllIfNotNull(includes, usage.getDependencyIncludesSet(rootModuleConf));
        for (IvyNodeUsage usage : mergedUsages.values()) {
            addAllIfNotNull(includes, usage.getDependencyIncludesSet(rootModuleConf));
        }
        if ((dependencyArtifacts == null || dependencyArtifacts.isEmpty()) && includes.isEmpty()) {
            // no artifacts / includes: we get all artifacts as defined by the descriptor
            for (String conf : confs) {
                artifacts.addAll(Arrays.asList(md.getArtifacts(conf)));
            }
        } else {
            // we have to get only artifacts listed as "includes";
            // first we get all artifacts as defined by the module descriptor
            // and classify them by artifact id
            Map<ArtifactId, Artifact> allArtifacts = new HashMap<>();
            for (String conf : confs) {
                for (Artifact art : md.getArtifacts(conf)) {
                    allArtifacts.put(art.getId().getArtifactId(), art);
                }
            }
            // now we add caller defined ones
            if (dependencyArtifacts != null) {
                addArtifactsFromOwnUsage(artifacts, dependencyArtifacts);
            }
            addArtifactsFromMergedUsage(rootModuleConf, artifacts);
            // and now we filter according to include rules
            Iterator<IncludeRule> it = includes.iterator();
            while (it.hasNext()) {
                IncludeRule dad = it.next();
                Collection<Artifact> arts = findArtifactsMatching(dad, allArtifacts);
                if (arts.isEmpty()) {
                    Message.error("a required artifact is not listed by module descriptor: " + dad.getId());
                    // we remove it from required list to prevent message to be displayed more
                    // than once
                    it.remove();
                } else {
                    Message.debug(this + " in " + rootModuleConf + ": including " + arts);
                    artifacts.addAll(arts);
                }
            }
        }
    }
    // now exclude artifacts that aren't accepted by any caller
    Iterator<Artifact> iter = artifacts.iterator();
    while (iter.hasNext()) {
        Artifact artifact = iter.next();
        boolean excluded = callers.doesCallersExclude(rootModuleConf, artifact);
        if (excluded) {
            Message.debug(this + " in " + rootModuleConf + ": excluding " + artifact);
            iter.remove();
        }
    }
    return artifacts.toArray(new Artifact[artifacts.size()]);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ArtifactId(org.apache.ivy.core.module.id.ArtifactId) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IncludeRule(org.apache.ivy.core.module.descriptor.IncludeRule) Artifact(org.apache.ivy.core.module.descriptor.Artifact) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact) MDArtifact(org.apache.ivy.core.module.descriptor.MDArtifact) DependencyArtifactDescriptor(org.apache.ivy.core.module.descriptor.DependencyArtifactDescriptor) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 35 with Artifact

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

the class RetrieveEngine method determineArtifactsToCopy.

public Map<ArtifactDownloadReport, Set<String>> determineArtifactsToCopy(ModuleRevisionId mrid, String destFilePattern, RetrieveOptions options) throws ParseException, IOException {
    ModuleId moduleId = mrid.getModuleId();
    if (options.getResolveId() == null) {
        options.setResolveId(ResolveOptions.getDefaultResolveId(moduleId));
    }
    ResolutionCacheManager cacheManager = getCache();
    String[] confs = getConfs(mrid, options);
    String destIvyPattern = IvyPatternHelper.substituteVariables(options.getDestIvyPattern(), settings.getVariables());
    // find what we must retrieve where
    // ArtifactDownloadReport source -> Set (String copyDestAbsolutePath)
    final Map<ArtifactDownloadReport, Set<String>> artifactsToCopy = new HashMap<>();
    // String copyDestAbsolutePath -> Set (ArtifactRevisionId source)
    final Map<String, Set<ArtifactRevisionId>> conflictsMap = new HashMap<>();
    // String copyDestAbsolutePath -> Set (ArtifactDownloadReport source)
    final Map<String, Set<ArtifactDownloadReport>> conflictsReportsMap = new HashMap<>();
    // String copyDestAbsolutePath -> Set (String conf)
    final Map<String, Set<String>> conflictsConfMap = new HashMap<>();
    XmlReportParser parser = new XmlReportParser();
    for (final String conf : confs) {
        File report = cacheManager.getConfigurationResolveReportInCache(options.getResolveId(), conf);
        parser.parse(report);
        Collection<ArtifactDownloadReport> artifacts = new ArrayList<>(Arrays.asList(parser.getArtifactReports()));
        if (destIvyPattern != null) {
            for (ModuleRevisionId rmrid : parser.getRealDependencyRevisionIds()) {
                artifacts.add(parser.getMetadataArtifactReport(rmrid));
            }
        }
        final PackagingManager packagingManager = new PackagingManager();
        packagingManager.setSettings(IvyContext.getContext().getSettings());
        for (final ArtifactDownloadReport adr : artifacts) {
            final Artifact artifact = adr.getArtifact();
            final String ext;
            if (adr.getUnpackedLocalFile() == null) {
                ext = artifact.getExt();
            } else {
                final Artifact unpackedArtifact;
                // check if the download report is aware of the unpacked artifact
                if (adr.getUnpackedArtifact() != null) {
                    unpackedArtifact = adr.getUnpackedArtifact();
                } else {
                    // use the packaging manager to get hold of the unpacked artifact
                    unpackedArtifact = packagingManager.getUnpackedArtifact(artifact);
                }
                if (unpackedArtifact == null) {
                    throw new RuntimeException("Could not determine unpacked artifact for " + artifact + " while determining artifacts to copy for module " + mrid);
                }
                ext = unpackedArtifact.getExt();
            }
            String destPattern = "ivy".equals(adr.getType()) ? destIvyPattern : destFilePattern;
            if (!"ivy".equals(adr.getType()) && !options.getArtifactFilter().accept(adr.getArtifact())) {
                // skip this artifact, the filter didn't accept it!
                continue;
            }
            ModuleRevisionId aMrid = artifact.getModuleRevisionId();
            String destFileName = IvyPatternHelper.substitute(destPattern, aMrid.getOrganisation(), aMrid.getName(), aMrid.getBranch(), aMrid.getRevision(), artifact.getName(), artifact.getType(), ext, conf, adr.getArtifactOrigin(), aMrid.getQualifiedExtraAttributes(), artifact.getQualifiedExtraAttributes());
            Set<String> dest = artifactsToCopy.get(adr);
            if (dest == null) {
                dest = new HashSet<>();
                artifactsToCopy.put(adr, dest);
            }
            String copyDest = settings.resolveFile(destFileName).getAbsolutePath();
            String[] destinations = new String[] { copyDest };
            if (options.getMapper() != null) {
                destinations = options.getMapper().mapFileName(copyDest);
            }
            for (String destination : destinations) {
                dest.add(destination);
                Set<ArtifactRevisionId> conflicts = conflictsMap.get(destination);
                Set<ArtifactDownloadReport> conflictsReports = conflictsReportsMap.get(destination);
                Set<String> conflictsConf = conflictsConfMap.get(destination);
                if (conflicts == null) {
                    conflicts = new HashSet<>();
                    conflictsMap.put(destination, conflicts);
                }
                if (conflictsReports == null) {
                    conflictsReports = new HashSet<>();
                    conflictsReportsMap.put(destination, conflictsReports);
                }
                if (conflictsConf == null) {
                    conflictsConf = new HashSet<>();
                    conflictsConfMap.put(destination, conflictsConf);
                }
                if (conflicts.add(artifact.getId())) {
                    conflictsReports.add(adr);
                    conflictsConf.add(conf);
                }
            }
        }
    }
    // resolve conflicts if any
    for (Map.Entry<String, Set<ArtifactRevisionId>> entry : conflictsMap.entrySet()) {
        String copyDest = entry.getKey();
        Set<ArtifactRevisionId> artifacts = entry.getValue();
        Set<String> conflictsConfs = conflictsConfMap.get(copyDest);
        if (artifacts.size() > 1) {
            List<ArtifactDownloadReport> artifactsList = new ArrayList<>(conflictsReportsMap.get(copyDest));
            // conflicts battle is resolved by a sort using a conflict resolving policy
            // comparator which consider as greater a winning artifact
            Collections.sort(artifactsList, getConflictResolvingPolicy());
            // after the sort, the winning artifact is the greatest one, i.e. the last one
            // we fail if different artifacts of the same module are mapped to the same file
            ArtifactDownloadReport winner = artifactsList.get(artifactsList.size() - 1);
            ModuleRevisionId winnerMD = winner.getArtifact().getModuleRevisionId();
            for (int i = artifactsList.size() - 2; i >= 0; i--) {
                ArtifactDownloadReport current = artifactsList.get(i);
                if (winnerMD.equals(current.getArtifact().getModuleRevisionId())) {
                    throw new RuntimeException("Multiple artifacts of the module " + winnerMD + " are retrieved to the same file! Update the retrieve pattern " + " to fix this error.");
                }
            }
            Message.info("\tconflict on " + copyDest + " in " + conflictsConfs + ": " + winnerMD.getRevision() + " won");
            // and going backward to the least artifact
            for (int i = artifactsList.size() - 2; i >= 0; i--) {
                ArtifactDownloadReport looser = artifactsList.get(i);
                Message.verbose("\t\tremoving conflict looser artifact: " + looser.getArtifact());
                // for each loser, we remove the pair (loser - copyDest) in the artifactsToCopy
                // map
                Set<String> dest = artifactsToCopy.get(looser);
                dest.remove(copyDest);
                if (dest.isEmpty()) {
                    artifactsToCopy.remove(looser);
                }
            }
        }
    }
    return artifactsToCopy;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) ArrayList(java.util.ArrayList) ModuleId(org.apache.ivy.core.module.id.ModuleId) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) Artifact(org.apache.ivy.core.module.descriptor.Artifact) XmlReportParser(org.apache.ivy.plugins.report.XmlReportParser) PackagingManager(org.apache.ivy.core.pack.PackagingManager) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) ArtifactRevisionId(org.apache.ivy.core.module.id.ArtifactRevisionId)

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