Search in sources :

Example 1 with ArtifactRevisionId

use of org.apache.ivy.core.module.id.ArtifactRevisionId in project ant-ivy by apache.

the class DefaultRepositoryCacheManager method getSavedArtifactOrigin.

public ArtifactOrigin getSavedArtifactOrigin(Artifact artifact) {
    ModuleRevisionId mrid = artifact.getModuleRevisionId();
    if (!lockMetadataArtifact(mrid)) {
        Message.error("impossible to acquire lock for " + mrid);
        return ArtifactOrigin.unknown(artifact);
    }
    try {
        PropertiesFile cdf = getCachedDataFile(artifact.getModuleRevisionId());
        String location = cdf.getProperty(getLocationKey(artifact));
        String local = cdf.getProperty(getIsLocalKey(artifact));
        String lastChecked = cdf.getProperty(getLastCheckedKey(artifact));
        String exists = cdf.getProperty(getExistsKey(artifact));
        String original = cdf.getProperty(getOriginalKey(artifact));
        boolean isLocal = Boolean.valueOf(local);
        if (location == null) {
            // origin has not been specified, return null
            return ArtifactOrigin.unknown(artifact);
        }
        if (original != null) {
            // original artifact key artifact:[name]#[type]#[ext]#[hashcode]
            java.util.regex.Matcher m = ARTIFACT_KEY_PATTERN.matcher(original);
            if (m.matches()) {
                String origName = m.group(1);
                String origType = m.group(2);
                String origExt = m.group(3);
                ArtifactRevisionId originArtifactId = ArtifactRevisionId.newInstance(artifact.getModuleRevisionId(), origName, origType, origExt);
                // second check: verify the hashcode of the cached artifact
                if (m.group(4).equals("" + originArtifactId.hashCode())) {
                    try {
                        artifact = new DefaultArtifact(originArtifactId, artifact.getPublicationDate(), new URL(location), true);
                    } catch (MalformedURLException e) {
                        Message.debug(e);
                    }
                }
            }
        } else {
            // origin artifact for it
            if (!location.endsWith("." + artifact.getExt())) {
                // try to find other cached artifact info with same location. This must be the
                // origin. We must parse the key as we do not know for sure what the original
                // artifact is named.
                String ownLocationKey = getLocationKey(artifact);
                for (Map.Entry<Object, Object> entry : cdf.entrySet()) {
                    if (entry.getValue().equals(location) && !ownLocationKey.equals(entry.getKey())) {
                        // found a match, key is
                        // artifact:[name]#[type]#[ext]#[hashcode].location
                        java.util.regex.Matcher m = ARTIFACT_KEY_PATTERN.matcher((String) entry.getKey());
                        if (m.matches()) {
                            String origName = m.group(1);
                            String origType = m.group(2);
                            String origExt = m.group(3);
                            // first check: the type should end in .original
                            if (!origType.endsWith(".original")) {
                                continue;
                            }
                            ArtifactRevisionId originArtifactId = ArtifactRevisionId.newInstance(artifact.getModuleRevisionId(), origName, origType, origExt);
                            // second check: verify the hashcode of the cached artifact
                            if (m.group(4).equals("" + originArtifactId.hashCode())) {
                                try {
                                    artifact = new DefaultArtifact(originArtifactId, artifact.getPublicationDate(), new URL(location), true);
                                } catch (MalformedURLException e) {
                                    Message.debug(e);
                                }
                            }
                            break;
                        }
                    }
                }
            }
        }
        ArtifactOrigin origin = new ArtifactOrigin(artifact, isLocal, location);
        if (lastChecked != null) {
            origin.setLastChecked(Long.valueOf(lastChecked));
        }
        if (exists != null) {
            origin.setExist(Boolean.valueOf(exists));
        }
        return origin;
    } finally {
        unlockMetadataArtifact(mrid);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) URL(java.net.URL) PropertiesFile(org.apache.ivy.util.PropertiesFile) Map(java.util.Map) HashMap(java.util.HashMap) ArtifactRevisionId(org.apache.ivy.core.module.id.ArtifactRevisionId) DefaultArtifact(org.apache.ivy.core.module.descriptor.DefaultArtifact)

Example 2 with ArtifactRevisionId

use of org.apache.ivy.core.module.id.ArtifactRevisionId 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)

Example 3 with ArtifactRevisionId

use of org.apache.ivy.core.module.id.ArtifactRevisionId in project ant-ivy by apache.

the class ResolveTest method testMultiClassifierMavenDepResolution.

/**
 * Tests that if a pom.xml has multiple dependencies for the same module, but for different Maven classifiers,
 * then the resolution of such dependencies, preserves those multiple (Ivy) artifacts against the dependency
 *
 * @throws Exception if something goes wrong
 * @see <a href="https://issues.apache.org/jira/browse/IVY-1576">IVY-1576</a> for more details
 */
@Test
public void testMultiClassifierMavenDepResolution() throws Exception {
    final File pomFile = new File("test/repositories/m2/org/apache/test-classifier/1.0/test-classifier-1.0.pom");
    final ResolveReport resolveReport = ivy.resolve(pomFile.toURI().toURL(), new ResolveOptions().setConfs(new String[] { "*" }));
    assertFalse(resolveReport.hasError());
    final ModuleRevisionId dependencyId = ModuleRevisionId.newInstance("org.apache", "test-classified", "1.0");
    final ArtifactDownloadReport[] adrs = resolveReport.getArtifactsReports(dependencyId);
    assertNotNull("Artifact download reports is null for dependency " + dependencyId, adrs);
    assertEquals("Unexpected number of artifact download reports for dependency " + dependencyId, 3, adrs.length);
    final Set<String> classifiers = new HashSet<>();
    Collections.addAll(classifiers, "other", "asl", "unix");
    for (final ArtifactDownloadReport adr : adrs) {
        final Artifact artifact = adr.getArtifact();
        assertNotNull("Artifact is null for dependency " + dependencyId, artifact);
        final ArtifactRevisionId artifactId = artifact.getId();
        assertEquals("Unexpected artifact " + artifactId.getName(), "test-classified", artifactId.getName());
        final String classifier = artifact.getExtraAttribute("m:classifier");
        assertNotNull("No classifier extra attribute on artifact for dependency " + dependencyId, classifier);
        assertTrue("Unexpected classifier " + classifier + " on artifact " + artifact, classifiers.remove(classifier));
        // just make sure it's the correct jar file
        assertJarContains(adr.getLocalFile(), classifier + ".txt");
    }
    assertTrue("Missing artifact(s) with classifiers " + classifiers, classifiers.isEmpty());
}
Also used : ConfigurationResolveReport(org.apache.ivy.core.report.ConfigurationResolveReport) ResolveReport(org.apache.ivy.core.report.ResolveReport) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) 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) ArtifactRevisionId(org.apache.ivy.core.module.id.ArtifactRevisionId) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ArtifactRevisionId (org.apache.ivy.core.module.id.ArtifactRevisionId)3 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)3 File (java.io.File)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Artifact (org.apache.ivy.core.module.descriptor.Artifact)2 DefaultArtifact (org.apache.ivy.core.module.descriptor.DefaultArtifact)2 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 JarFile (java.util.jar.JarFile)1 ResolutionCacheManager (org.apache.ivy.core.cache.ResolutionCacheManager)1 ModuleId (org.apache.ivy.core.module.id.ModuleId)1 PackagingManager (org.apache.ivy.core.pack.PackagingManager)1 ConfigurationResolveReport (org.apache.ivy.core.report.ConfigurationResolveReport)1 ResolveReport (org.apache.ivy.core.report.ResolveReport)1 XmlReportParser (org.apache.ivy.plugins.report.XmlReportParser)1