Search in sources :

Example 1 with XmlReportParser

use of org.apache.ivy.plugins.report.XmlReportParser in project ant-ivy by apache.

the class IvyReport method getOutputPattern.

private String getOutputPattern(String conf, String ext) {
    if (mRevId == null) {
        ResolutionCacheManager cacheMgr = getIvyInstance().getResolutionCacheManager();
        XmlReportParser parser = new XmlReportParser();
        File reportFile = cacheMgr.getConfigurationResolveReportInCache(resolveId, conf);
        try {
            parser.parse(reportFile);
        } catch (ParseException e) {
            throw new BuildException("Error occurred while parsing reportfile '" + reportFile.getAbsolutePath() + "'", e);
        }
        // get the resolve module
        mRevId = parser.getResolvedModule();
    }
    return IvyPatternHelper.substitute(outputpattern, mRevId.getOrganisation(), mRevId.getName(), mRevId.getRevision(), "", "", ext, conf, mRevId.getQualifiedExtraAttributes(), null);
}
Also used : XmlReportParser(org.apache.ivy.plugins.report.XmlReportParser) ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) ParseException(java.text.ParseException) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 2 with XmlReportParser

use of org.apache.ivy.plugins.report.XmlReportParser in project ant-ivy by apache.

the class ConfigurationResolveReport method checkIfChanged.

/**
 * Check if the set of dependencies has changed since the previous execution of a resolution.
 * <p>
 * This function use the report file found in the cache. So the function must be called before
 * the new report is serialized there.
 * </p>
 * <p>
 * This function also use the internal dependencies that must already be filled. This function
 * might be 'heavy' because it may have to parse the previous report.
 * </p>
 */
public void checkIfChanged() {
    ResolutionCacheManager cache = resolveEngine.getSettings().getResolutionCacheManager();
    String resolveId = options.getResolveId();
    File previousReportFile = cache.getConfigurationResolveReportInCache(resolveId, conf);
    if (previousReportFile.exists()) {
        try {
            XmlReportParser parser = new XmlReportParser();
            parser.parse(previousReportFile);
            Set<ModuleRevisionId> previousDepSet = new HashSet<>(Arrays.asList(parser.getDependencyRevisionIds()));
            hasChanged = !previousDepSet.equals(getModuleRevisionIds());
        } catch (Exception e) {
            Message.warn("Error while parsing configuration resolve report " + previousReportFile.getAbsolutePath(), e);
            hasChanged = Boolean.TRUE;
        }
    } else {
        hasChanged = Boolean.TRUE;
    }
}
Also used : XmlReportParser(org.apache.ivy.plugins.report.XmlReportParser) ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with XmlReportParser

use of org.apache.ivy.plugins.report.XmlReportParser 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 4 with XmlReportParser

use of org.apache.ivy.plugins.report.XmlReportParser in project Saturn by vipshop.

the class IvyGetArtifact method getCachePath.

private Set<URL> getCachePath(ModuleDescriptor md, String[] confs) throws ParseException, IOException {
    Set<URL> fs = new HashSet<URL>();
    StringBuffer buf = new StringBuffer();
    Collection<ArtifactDownloadReport> all = new LinkedHashSet<ArtifactDownloadReport>();
    ResolutionCacheManager cacheMgr = ivy.getResolutionCacheManager();
    XmlReportParser parser = new XmlReportParser();
    for (int i = 0; i < confs.length; i++) {
        String resolveId = ResolveOptions.getDefaultResolveId(md);
        File report = cacheMgr.getConfigurationResolveReportInCache(resolveId, confs[i]);
        parser.parse(report);
        all.addAll(Arrays.asList(parser.getArtifactReports()));
    }
    for (ArtifactDownloadReport artifact : all) {
        if (artifact.getLocalFile() != null) {
            buf.append(artifact.getLocalFile().getCanonicalPath());
            buf.append(File.pathSeparator);
        }
    }
    String[] fs_str = buf.toString().split(File.pathSeparator);
    for (String str : fs_str) {
        File file = new File(str);
        if (file.exists()) {
            fs.add(file.toURI().toURL());
        }
    }
    return fs;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ResolutionCacheManager(org.apache.ivy.core.cache.ResolutionCacheManager) ArtifactDownloadReport(org.apache.ivy.core.report.ArtifactDownloadReport) URL(java.net.URL) XmlReportParser(org.apache.ivy.plugins.report.XmlReportParser) File(java.io.File) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with XmlReportParser

use of org.apache.ivy.plugins.report.XmlReportParser in project ant-ivy by apache.

the class DeliverEngine method deliver.

/**
 * Delivers a resolved ivy file based upon last resolve call status. If resolve report file
 * cannot be found in cache, then it throws an IllegalStateException (maybe resolve has not been
 * called before ?).
 *
 * @param revision
 *            the revision to which the module should be delivered
 * @param destIvyPattern
 *            the pattern to which the delivered ivy file should be written
 * @param options
 *            the options with which deliver should be done
 * @throws IOException if something goes wrong
 * @throws ParseException if something goes wrong
 */
public void deliver(String revision, String destIvyPattern, DeliverOptions options) throws IOException, ParseException {
    String resolveId = options.getResolveId();
    if (resolveId == null) {
        throw new IllegalArgumentException("A resolveId must be specified for delivering.");
    }
    File[] files = getCache().getConfigurationResolveReportsInCache(resolveId);
    if (files.length == 0) {
        throw new IllegalStateException("No previous resolve found for id '" + resolveId + "' Please resolve dependencies before delivering.");
    }
    XmlReportParser parser = new XmlReportParser();
    parser.parse(files[0]);
    ModuleRevisionId mrid = parser.getResolvedModule();
    deliver(mrid, revision, destIvyPattern, options);
}
Also used : XmlReportParser(org.apache.ivy.plugins.report.XmlReportParser) ModuleRevisionId(org.apache.ivy.core.module.id.ModuleRevisionId) File(java.io.File)

Aggregations

File (java.io.File)9 XmlReportParser (org.apache.ivy.plugins.report.XmlReportParser)9 ResolutionCacheManager (org.apache.ivy.core.cache.ResolutionCacheManager)8 LinkedHashSet (java.util.LinkedHashSet)5 ArtifactDownloadReport (org.apache.ivy.core.report.ArtifactDownloadReport)5 ModuleRevisionId (org.apache.ivy.core.module.id.ModuleRevisionId)4 HashSet (java.util.HashSet)3 BuildException (org.apache.tools.ant.BuildException)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 Artifact (org.apache.ivy.core.module.descriptor.Artifact)2 ResolveProcessException (org.apache.ivy.core.resolve.ResolveProcessException)2 PropertiesFile (org.apache.ivy.util.PropertiesFile)2 ParseException (org.apache.ivy.util.cli.ParseException)2 FileOutputStream (java.io.FileOutputStream)1 PrintWriter (java.io.PrintWriter)1 Method (java.lang.reflect.Method)1