use of org.apache.ivy.core.cache.ResolutionCacheManager 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;
}
use of org.apache.ivy.core.cache.ResolutionCacheManager in project ant-ivy by apache.
the class PublishEngine method publish.
/**
* Publishes a module to the repository. The publish can update the ivy file to publish if
* update is set to true. In this case it will use the given pubrevision, pubdate and status. If
* pubdate is null it will default to the current date. If status is null it will default to the
* current ivy file status (which itself defaults to integration if none is found). If update is
* false, then if the revision is not the same in the ivy file than the one expected (given as
* parameter), this method will fail with an IllegalArgumentException. pubdate and status are
* not used if update is false. extra artifacts can be used to publish more artifacts than
* actually declared in the ivy file. This can be useful to publish additional metadata or
* reports. The extra artifacts array can be null (= no extra artifacts), and if non null only
* the name, type, ext url and extra attributes of the artifacts are really used. Other methods
* can return null safely.
*
* @param mrid ModuleRevisionId
* @param srcArtifactPattern a Collection of String
* @param resolverName String
* @param options PublishOptions
* @return Collection<Artifact>
* @throws IOException if something goes wrong
*/
public Collection<Artifact> publish(ModuleRevisionId mrid, Collection<String> srcArtifactPattern, String resolverName, PublishOptions options) throws IOException {
Message.info(":: publishing :: " + mrid.getModuleId());
Message.verbose("\tvalidate = " + options.isValidate());
long start = System.currentTimeMillis();
options.setSrcIvyPattern(settings.substitute(options.getSrcIvyPattern()));
if (options.getPubBranch() == null) {
options.setPubbranch(mrid.getBranch());
}
if (options.getPubrevision() == null) {
options.setPubrevision(mrid.getRevision());
}
ModuleRevisionId pubmrid = ModuleRevisionId.newInstance(mrid, options.getPubBranch(), options.getPubrevision());
// let's find the resolved module descriptor
ModuleDescriptor md = null;
if (options.getSrcIvyPattern() != null) {
File ivyFile = settings.resolveFile(IvyPatternHelper.substitute(options.getSrcIvyPattern(), DefaultArtifact.newIvyArtifact(pubmrid, new Date())));
if (!ivyFile.exists()) {
throw new IllegalArgumentException("ivy file to publish not found for " + mrid + ": call deliver before (" + ivyFile + ")");
}
URL ivyFileURL = ivyFile.toURI().toURL();
try {
md = XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, ivyFileURL, false);
if (options.isUpdate()) {
File tmp = File.createTempFile("ivy", ".xml");
tmp.deleteOnExit();
String[] confs = replaceWildcards(options.getConfs(), md);
Set<String> confsToRemove = new HashSet<>(Arrays.asList(md.getConfigurationsNames()));
confsToRemove.removeAll(Arrays.asList(confs));
try {
XmlModuleDescriptorUpdater.update(ivyFileURL, tmp, new UpdateOptions().setSettings(settings).setStatus(options.getStatus() == null ? md.getStatus() : options.getStatus()).setRevision(options.getPubrevision()).setBranch(options.getPubBranch()).setPubdate(options.getPubdate() == null ? new Date() : options.getPubdate()).setMerge(options.isMerge()).setMergedDescriptor(md).setConfsToExclude(confsToRemove.toArray(new String[confsToRemove.size()])));
ivyFile = tmp;
// we parse the new file to get updated module descriptor
md = XmlModuleDescriptorParser.getInstance().parseDescriptor(settings, ivyFile.toURI().toURL(), false);
options.setSrcIvyPattern(ivyFile.getAbsolutePath());
} catch (SAXException e) {
throw new IllegalStateException("bad ivy file for " + mrid + ": " + ivyFile + ": " + e);
}
} else if (!options.getPubrevision().equals(md.getModuleRevisionId().getRevision())) {
throw new IllegalArgumentException("cannot publish " + ivyFile + " as " + options.getPubrevision() + ": bad revision found in ivy file (Revision: " + md.getModuleRevisionId().getRevision() + "). Use forcedeliver or update.");
}
} catch (ParseException e) {
throw new IllegalStateException("bad ivy file for " + mrid + ": " + ivyFile + ": " + e);
}
} else {
ResolutionCacheManager cacheManager = settings.getResolutionCacheManager();
try {
md = cacheManager.getResolvedModuleDescriptor(mrid);
} catch (ParseException e) {
throw new IllegalStateException("bad ivy file in cache for " + mrid + ": " + e);
}
md.setResolvedModuleRevisionId(pubmrid);
}
DependencyResolver resolver = settings.getResolver(resolverName);
if (resolver == null) {
throw new IllegalArgumentException("unknown resolver " + resolverName);
}
// collect all declared artifacts of this module
Collection<Artifact> missing = publish(md, srcArtifactPattern, resolver, options);
Message.verbose("\tpublish done (" + (System.currentTimeMillis() - start) + "ms)");
return missing;
}
use of org.apache.ivy.core.cache.ResolutionCacheManager 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;
}
use of org.apache.ivy.core.cache.ResolutionCacheManager in project ant-ivy by apache.
the class ResolveEngine method resolve.
/**
* Resolve dependencies of a module described by a module descriptor.
* @param md ModuleDescriptor
* @param options ResolveOptions
* @return ResolveReport
* @throws ParseException if something goes wrong
* @throws IOException if something goes wrong
*/
public ResolveReport resolve(ModuleDescriptor md, ResolveOptions options) throws ParseException, IOException {
DependencyResolver oldDictator = getDictatorResolver();
IvyContext context = IvyContext.getContext();
try {
String[] confs = options.getConfs(md);
options.setConfs(confs);
if (options.getResolveId() == null) {
options.setResolveId(ResolveOptions.getDefaultResolveId(md));
}
eventManager.fireIvyEvent(new StartResolveEvent(md, confs));
long start = System.currentTimeMillis();
if (ResolveOptions.LOG_DEFAULT.equals(options.getLog())) {
Message.info(":: resolving dependencies :: " + md.getResolvedModuleRevisionId() + (options.isTransitive() ? "" : " [not transitive]"));
Message.info("\tconfs: " + Arrays.asList(confs));
} else {
Message.verbose(":: resolving dependencies :: " + md.getResolvedModuleRevisionId() + (options.isTransitive() ? "" : " [not transitive]"));
Message.verbose("\tconfs: " + Arrays.asList(confs));
}
Message.verbose("\tvalidate = " + options.isValidate());
Message.verbose("\trefresh = " + options.isRefresh());
ResolveReport report = new ResolveReport(md, options.getResolveId());
ResolveData data = new ResolveData(this, options);
context.setResolveData(data);
// resolve dependencies
IvyNode[] dependencies = getDependencies(md, options, report);
report.setDependencies(Arrays.asList(dependencies), options.getArtifactFilter());
if (options.getCheckIfChanged()) {
report.checkIfChanged();
}
// produce resolved ivy file and ivy properties in cache
ResolutionCacheManager cacheManager = settings.getResolutionCacheManager();
cacheManager.saveResolvedModuleDescriptor(md);
// we store the resolved dependencies revisions and statuses per asked dependency
// revision id, for direct dependencies only.
// this is used by the deliver task to resolve dynamic revisions to static ones
File ivyPropertiesInCache = cacheManager.getResolvedIvyPropertiesInCache(md.getResolvedModuleRevisionId());
Properties props = new Properties();
if (dependencies.length > 0) {
Map<ModuleId, ModuleRevisionId> forcedRevisions = new HashMap<>();
for (IvyNode dependency : dependencies) {
if (dependency.getModuleRevision() != null && dependency.getModuleRevision().isForce()) {
forcedRevisions.put(dependency.getModuleId(), dependency.getResolvedId());
}
}
IvyNode root = dependencies[0].getRoot();
Map<ModuleId, IvyNode> topLevelDeps = new HashMap<>();
for (IvyNode dependency : dependencies) {
if (!dependency.hasProblem()) {
DependencyDescriptor dd = dependency.getDependencyDescriptor(root);
if (dd != null) {
ModuleId orgMod = dependency.getModuleId();
topLevelDeps.put(orgMod, dependency);
}
}
}
for (IvyNode dependency : dependencies) {
if (!dependency.hasProblem() && !dependency.isCompletelyEvicted()) {
DependencyDescriptor dd = dependency.getDependencyDescriptor(root);
if (dd == null) {
ModuleId mid = dependency.getModuleId();
IvyNode tlDep = topLevelDeps.get(mid);
if (tlDep != null) {
dd = tlDep.getDependencyDescriptor(root);
}
}
if (dd != null) {
ModuleRevisionId depResolvedId = dependency.getResolvedId();
ModuleDescriptor depDescriptor = dependency.getDescriptor();
ModuleRevisionId depRevisionId = dd.getDependencyRevisionId();
ModuleRevisionId forcedRevisionId = forcedRevisions.get(dependency.getModuleId());
if (dependency.getModuleRevision() != null && dependency.getModuleRevision().isForce() && !depResolvedId.equals(depRevisionId) && !settings.getVersionMatcher().isDynamic(depRevisionId)) {
// if we were forced to this revision and we
// are not a dynamic revision, reset to the
// asked revision
depResolvedId = depRevisionId;
depDescriptor = null;
}
if (depResolvedId == null) {
throw new NullPointerException("getResolvedId() is null for " + dependency.toString());
}
if (depRevisionId == null) {
throw new NullPointerException("getDependencyRevisionId() " + "is null for " + dd.toString());
}
String rev = depResolvedId.getRevision();
String forcedRev = forcedRevisionId == null ? rev : forcedRevisionId.getRevision();
// The evicted modules have no description, so we can't put the status
String status = depDescriptor == null ? "?" : depDescriptor.getStatus();
Message.debug("storing dependency " + depResolvedId + " in props");
props.put(depRevisionId.encodeToString(), rev + " " + status + " " + forcedRev + " " + depResolvedId.getBranch());
}
}
}
}
FileOutputStream out = new FileOutputStream(ivyPropertiesInCache);
props.store(out, md.getResolvedModuleRevisionId() + " resolved revisions");
out.close();
Message.verbose("\tresolved ivy file produced in cache");
report.setResolveTime(System.currentTimeMillis() - start);
if (options.isDownload()) {
Message.verbose(":: downloading artifacts ::");
DownloadOptions downloadOptions = new DownloadOptions();
downloadOptions.setLog(options.getLog());
downloadArtifacts(report, options.getArtifactFilter(), downloadOptions);
}
if (options.isOutputReport()) {
outputReport(report, cacheManager, options);
}
Message.verbose("\tresolve done (" + report.getResolveTime() + "ms resolve - " + report.getDownloadTime() + "ms download)");
Message.sumupProblems();
eventManager.fireIvyEvent(new EndResolveEvent(md, confs, report));
return report;
} catch (RuntimeException ex) {
Message.debug(ex);
Message.error(ex.getMessage());
Message.sumupProblems();
throw ex;
} finally {
context.setResolveData(null);
setDictatorResolver(oldDictator);
}
}
use of org.apache.ivy.core.cache.ResolutionCacheManager in project ant-ivy by apache.
the class IvyArtifactProperty method doExecute.
public void doExecute() throws BuildException {
prepareAndCheck();
try {
ResolutionCacheManager cacheMgr = getIvyInstance().getResolutionCacheManager();
String resolveId = getResolveId();
if (resolveId == null) {
resolveId = ResolveOptions.getDefaultResolveId(getResolvedModuleId());
}
XmlReportParser parser = new XmlReportParser();
for (String conf : splitToArray(getConf())) {
File report = cacheMgr.getConfigurationResolveReportInCache(resolveId, conf);
parser.parse(report);
for (Artifact artifact : parser.getArtifacts()) {
String name = IvyPatternHelper.substitute(getSettings().substitute(getName()), artifact, conf);
String value = IvyPatternHelper.substitute(getSettings().substitute(getValue()), artifact, conf);
setProperty(name, value);
}
}
} catch (Exception ex) {
throw new BuildException("impossible to add artifact properties: " + ex, ex);
}
}
Aggregations