use of org.apache.ivy.core.module.descriptor.DependencyDescriptor in project ant-ivy by apache.
the class BasicResolver method findResource.
/**
* When the resolver has many choices, this function helps choosing one
*
* @param rress
* the list of resolved resource which the resolver found to fit the requirement
* @param rmdparser
* the parser of module descriptor
* @param mrid
* the module being resolved
* @param date
* the current date
* @return the selected resource
*/
public ResolvedResource findResource(ResolvedResource[] rress, ResourceMDParser rmdparser, ModuleRevisionId mrid, Date date) {
String name = getName();
VersionMatcher versionMatcher = getSettings().getVersionMatcher();
ResolvedResource found = null;
List<ArtifactInfo> sorted = getLatestStrategy().sort(rress);
List<String> rejected = new ArrayList<>();
List<ModuleRevisionId> foundBlacklisted = new ArrayList<>();
IvyContext context = IvyContext.getContext();
ListIterator<ArtifactInfo> iter = sorted.listIterator(sorted.size());
while (iter.hasPrevious()) {
ResolvedResource rres = (ResolvedResource) iter.previous();
// name, blacklisting and first level version matching
if (filterNames(new ArrayList<>(Collections.singleton(rres.getRevision()))).isEmpty()) {
Message.debug("\t" + name + ": filtered by name: " + rres);
continue;
}
ModuleRevisionId foundMrid = ModuleRevisionId.newInstance(mrid, rres.getRevision());
ResolveData data = context.getResolveData();
if (data != null && data.getReport() != null && data.isBlacklisted(data.getReport().getConfiguration(), foundMrid)) {
Message.debug("\t" + name + ": blacklisted: " + rres);
rejected.add(rres.getRevision() + " (blacklisted)");
foundBlacklisted.add(foundMrid);
continue;
}
if (!versionMatcher.accept(mrid, foundMrid)) {
Message.debug("\t" + name + ": rejected by version matcher: " + rres);
rejected.add(rres.getRevision());
continue;
}
if (rres.getResource() != null && !rres.getResource().exists()) {
Message.debug("\t" + name + ": unreachable: " + rres + "; res=" + rres.getResource());
rejected.add(rres.getRevision() + " (unreachable)");
continue;
}
if (date != null && rres.getLastModified() > date.getTime()) {
Message.verbose("\t" + name + ": too young: " + rres);
rejected.add(rres.getRevision() + " (" + rres.getLastModified() + ")");
continue;
}
if (versionMatcher.needModuleDescriptor(mrid, foundMrid)) {
ResolvedResource r = rmdparser.parse(rres.getResource(), rres.getRevision());
if (r == null) {
Message.debug("\t" + name + ": impossible to get module descriptor resource: " + rres);
rejected.add(rres.getRevision() + " (no or bad MD)");
continue;
}
ModuleDescriptor md = ((MDResolvedResource) r).getResolvedModuleRevision().getDescriptor();
if (md.isDefault()) {
Message.debug("\t" + name + ": default md rejected by version matcher" + "requiring module descriptor: " + rres);
rejected.add(rres.getRevision() + " (MD)");
continue;
}
if (!versionMatcher.accept(mrid, md)) {
Message.debug("\t" + name + ": md rejected by version matcher: " + rres);
rejected.add(rres.getRevision() + " (MD)");
continue;
}
found = r;
} else {
found = rres;
}
if (found != null) {
break;
}
}
if (found == null && !rejected.isEmpty()) {
logAttempt(rejected.toString());
}
if (found == null && !foundBlacklisted.isEmpty()) {
// all acceptable versions have been blacklisted, this means that an unsolvable conflict
// has been found
DependencyDescriptor dd = context.getDependencyDescriptor();
IvyNode parentNode = context.getResolveData().getNode(dd.getParentRevisionId());
ConflictManager cm = parentNode.getConflictManager(mrid.getModuleId());
cm.handleAllBlacklistedRevisions(dd, foundBlacklisted);
}
return found;
}
use of org.apache.ivy.core.module.descriptor.DependencyDescriptor in project ant-ivy by apache.
the class BasicResolver method getDependency.
public ResolvedModuleRevision getDependency(DependencyDescriptor dd, ResolveData data) throws ParseException {
IvyContext context = IvyContext.pushNewCopyContext();
try {
ResolvedModuleRevision mr = data.getCurrentResolvedModuleRevision();
if (mr != null && shouldReturnResolvedModule(dd, mr)) {
return mr;
}
if (isForce()) {
dd = dd.clone(ModuleRevisionId.newInstance(dd.getDependencyRevisionId(), "latest.integration"));
}
DependencyDescriptor systemDd = dd;
DependencyDescriptor nsDd = fromSystem(dd);
context.setDependencyDescriptor(systemDd);
context.setResolveData(data);
clearIvyAttempts();
clearArtifactAttempts();
ModuleRevisionId systemMrid = systemDd.getDependencyRevisionId();
ModuleRevisionId nsMrid = nsDd.getDependencyRevisionId();
checkRevision(systemMrid);
boolean isDynamic = getAndCheckIsDynamic(systemMrid);
// we first search for the dependency in cache
ResolvedModuleRevision rmr = findModuleInCache(systemDd, data);
if (rmr != null) {
if (rmr.getDescriptor().isDefault() && rmr.getResolver() != this) {
Message.verbose("\t" + getName() + ": found revision in cache: " + systemMrid + " (resolved by " + rmr.getResolver().getName() + "): but it's a default one, maybe we can find a better one");
} else if (isForce() && rmr.getResolver() != this) {
Message.verbose("\t" + getName() + ": found revision in cache: " + systemMrid + " (resolved by " + rmr.getResolver().getName() + "): but we are in force mode, let's try to find one ourselves");
} else {
Message.verbose("\t" + getName() + ": revision in cache: " + systemMrid);
return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
}
}
if (data.getOptions().isUseCacheOnly()) {
throw new UnresolvedDependencyException("\t" + getName() + " (useCacheOnly) : no ivy file found for " + systemMrid, false);
}
checkInterrupted();
ResolvedResource ivyRef = findIvyFileRef(nsDd, data);
checkInterrupted();
// get module descriptor
ModuleDescriptor nsMd;
ModuleDescriptor systemMd = null;
if (ivyRef == null) {
if (!isAllownomd()) {
throw new UnresolvedDependencyException("\t" + getName() + ": no ivy file found for " + systemMrid, false);
}
nsMd = DefaultModuleDescriptor.newDefaultInstance(nsMrid, nsDd.getAllDependencyArtifacts());
ResolvedResource artifactRef = findFirstArtifactRef(nsMd, nsDd, data);
checkInterrupted();
if (artifactRef == null) {
throw new UnresolvedDependencyException("\t" + getName() + ": no ivy file nor artifact found for " + systemMrid, false);
}
long lastModified = artifactRef.getLastModified();
if (lastModified != 0 && nsMd instanceof DefaultModuleDescriptor) {
((DefaultModuleDescriptor) nsMd).setLastModified(lastModified);
}
Message.verbose("\t" + getName() + ": no ivy file found for " + systemMrid + ": using default data");
if (isDynamic) {
nsMd.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(nsMrid, artifactRef.getRevision()));
}
systemMd = toSystem(nsMd);
MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(systemMd.getMetadataArtifact());
madr.setDownloadStatus(DownloadStatus.NO);
madr.setSearched(true);
rmr = new ResolvedModuleRevision(this, this, systemMd, madr, isForce());
getRepositoryCacheManager().cacheModuleDescriptor(this, artifactRef, toSystem(dd), systemMd.getAllArtifacts()[0], null, getCacheOptions(data));
} else {
if (ivyRef instanceof MDResolvedResource) {
rmr = ((MDResolvedResource) ivyRef).getResolvedModuleRevision();
}
if (rmr == null) {
rmr = parse(ivyRef, systemDd, data);
if (rmr == null) {
throw new UnresolvedDependencyException();
}
}
if (!rmr.getReport().isDownloaded() && rmr.getReport().getLocalFile() != null) {
return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
}
nsMd = rmr.getDescriptor();
// check descriptor data is in sync with resource revision and names
systemMd = toSystem(nsMd);
if (isCheckconsistency()) {
checkDescriptorConsistency(systemMrid, systemMd, ivyRef);
checkDescriptorConsistency(nsMrid, nsMd, ivyRef);
} else {
if (systemMd instanceof DefaultModuleDescriptor) {
DefaultModuleDescriptor defaultMd = (DefaultModuleDescriptor) systemMd;
ModuleRevisionId revision = getRevision(ivyRef, systemMrid, systemMd);
defaultMd.setModuleRevisionId(revision);
defaultMd.setResolvedModuleRevisionId(revision);
} else {
Message.warn("consistency disabled with instance of non DefaultModuleDescriptor... module info can't be updated, so consistency check will be done");
checkDescriptorConsistency(nsMrid, nsMd, ivyRef);
checkDescriptorConsistency(systemMrid, systemMd, ivyRef);
}
}
rmr = new ResolvedModuleRevision(this, this, systemMd, toSystem(rmr.getReport()), isForce());
}
resolveAndCheckRevision(systemMd, systemMrid, ivyRef, isDynamic);
resolveAndCheckPublicationDate(systemDd, systemMd, systemMrid, data);
checkNotConvertedExclusionRule(systemMd, ivyRef, data);
if (ivyRef == null || ivyRef.getResource() != null) {
cacheModuleDescriptor(systemMd, systemMrid, ivyRef, rmr);
}
return checkLatest(systemDd, checkForcedResolvedModuleRevision(rmr), data);
} catch (UnresolvedDependencyException ex) {
if (!ex.getMessage().isEmpty()) {
if (ex.isError()) {
Message.error(ex.getMessage());
} else {
Message.verbose(ex.getMessage());
}
}
return data.getCurrentResolvedModuleRevision();
} finally {
IvyContext.popContext();
}
}
use of org.apache.ivy.core.module.descriptor.DependencyDescriptor in project ant-ivy by apache.
the class IvyDependencyUpdateChecker method doExecute.
public void doExecute() throws BuildException {
prepareAndCheck();
ModuleDescriptor originalModuleDescriptor = getResolvedReport().getModuleDescriptor();
// clone module descriptor
DefaultModuleDescriptor latestModuleDescriptor = new DefaultModuleDescriptor(originalModuleDescriptor.getModuleRevisionId(), originalModuleDescriptor.getStatus(), originalModuleDescriptor.getPublicationDate());
// copy configurations
for (Configuration configuration : originalModuleDescriptor.getConfigurations()) {
latestModuleDescriptor.addConfiguration(configuration);
}
// clone dependency and add new one with the requested revisionToCheck
for (DependencyDescriptor dependencyDescriptor : originalModuleDescriptor.getDependencies()) {
ModuleRevisionId upToDateMrid = ModuleRevisionId.newInstance(dependencyDescriptor.getDependencyRevisionId(), revisionToCheck);
latestModuleDescriptor.addDependency(dependencyDescriptor.clone(upToDateMrid));
}
// resolve
ResolveOptions resolveOptions = new ResolveOptions();
resolveOptions.setDownload(isDownload());
resolveOptions.setLog(getLog());
resolveOptions.setConfs(splitToArray(getConf()));
resolveOptions.setCheckIfChanged(checkIfChanged);
ResolveReport latestReport;
try {
latestReport = getIvyInstance().getResolveEngine().resolve(latestModuleDescriptor, resolveOptions);
displayDependencyUpdates(getResolvedReport(), latestReport);
if (showTransitive) {
displayNewDependencyOnLatest(getResolvedReport(), latestReport);
displayMissingDependencyOnLatest(getResolvedReport(), latestReport);
}
} catch (ParseException | IOException e) {
throw new BuildException("impossible to resolve dependencies:\n\t" + e, e);
}
}
use of org.apache.ivy.core.module.descriptor.DependencyDescriptor 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 mrid
* the module revision id of the module to deliver
* @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(ModuleRevisionId mrid, String revision, String destIvyPattern, DeliverOptions options) throws IOException, ParseException {
Message.info(":: delivering :: " + mrid + " :: " + revision + " :: " + options.getStatus() + " :: " + options.getPubdate());
Message.verbose("\toptions = " + options);
long start = System.currentTimeMillis();
destIvyPattern = settings.substitute(destIvyPattern);
// 1) find the resolved module descriptor in cache
ModuleDescriptor md = getCache().getResolvedModuleDescriptor(mrid);
md.setResolvedModuleRevisionId(ModuleRevisionId.newInstance(md.getModuleRevisionId(), options.getPubBranch() == null ? mrid.getBranch() : options.getPubBranch(), revision));
md.setResolvedPublicationDate(options.getPubdate());
// 2) parse resolvedRevisions From properties file
// Map (ModuleId -> String revision)
Map<ModuleRevisionId, String> resolvedRevisions = new HashMap<>();
// Map (ModuleId -> String branch)
Map<ModuleRevisionId, String> resolvedBranches = new HashMap<>();
// Map (ModuleId -> String status)
Map<ModuleRevisionId, String> dependenciesStatus = new HashMap<>();
File ivyProperties = getCache().getResolvedIvyPropertiesInCache(mrid);
if (!ivyProperties.exists()) {
throw new IllegalStateException("ivy properties not found in cache for " + mrid + "; please resolve dependencies before delivering!");
}
Properties props = new Properties();
FileInputStream in = new FileInputStream(ivyProperties);
props.load(in);
in.close();
for (Object o : props.keySet()) {
String depMridStr = (String) o;
String[] parts = props.getProperty(depMridStr).split(" ");
ModuleRevisionId decodedMrid = ModuleRevisionId.decode(depMridStr);
if (options.isResolveDynamicRevisions()) {
resolvedRevisions.put(decodedMrid, parts[0]);
if (parts.length >= 4) {
if (parts[3] != null && !"null".equals(parts[3])) {
resolvedBranches.put(decodedMrid, parts[3]);
}
}
}
dependenciesStatus.put(decodedMrid, parts[1]);
if (options.isReplaceForcedRevisions()) {
if (parts.length <= 2) {
// so it is possible that this part doesn't exist.
throw new IllegalStateException("ivy properties file generated by an older" + " version of Ivy which doesn't support replacing forced revisions!");
}
resolvedRevisions.put(decodedMrid, parts[2]);
}
}
// 3) use pdrResolver to resolve dependencies info
Map<ModuleRevisionId, String> resolvedDependencies = new HashMap<>();
// Map (ModuleRevisionId -> String revision)
for (DependencyDescriptor dependency : md.getDependencies()) {
String rev = resolvedRevisions.get(dependency.getDependencyRevisionId());
if (rev == null) {
rev = dependency.getDependencyRevisionId().getRevision();
}
String bra = resolvedBranches.get(dependency.getDependencyRevisionId());
if (bra == null || "null".equals(bra)) {
bra = dependency.getDependencyRevisionId().getBranch();
}
String depStatus = dependenciesStatus.get(dependency.getDependencyRevisionId());
ModuleRevisionId mrid2 = (bra == null) ? ModuleRevisionId.newInstance(dependency.getDependencyRevisionId(), rev) : ModuleRevisionId.newInstance(dependency.getDependencyRevisionId(), bra, rev);
resolvedDependencies.put(dependency.getDependencyRevisionId(), options.getPdrResolver().resolve(md, options.getStatus(), mrid2, depStatus));
}
// 4) copy the source resolved ivy to the destination specified,
// updating status, revision and dependency revisions obtained by
// PublishingDependencyRevisionResolver
File publishedIvy = settings.resolveFile(IvyPatternHelper.substitute(destIvyPattern, md.getResolvedModuleRevisionId()));
Message.info("\tdelivering ivy file to " + publishedIvy);
String[] confs = ConfigurationUtils.replaceWildcards(options.getConfs(), md);
Set<String> confsToRemove = new HashSet<>(Arrays.asList(md.getConfigurationsNames()));
confsToRemove.removeAll(Arrays.asList(confs));
try {
UpdateOptions opts = new UpdateOptions().setSettings(settings).setResolvedRevisions(resolvedDependencies).setStatus(options.getStatus()).setRevision(revision).setBranch(options.getPubBranch()).setPubdate(options.getPubdate()).setGenerateRevConstraint(options.isGenerateRevConstraint()).setMerge(options.isMerge()).setMergedDescriptor(md).setConfsToExclude(confsToRemove.toArray(new String[confsToRemove.size()]));
if (!resolvedBranches.isEmpty()) {
opts = opts.setResolvedBranches(resolvedBranches);
}
Resource res = md.getResource();
XmlModuleDescriptorUpdater.update(res.openStream(), res, publishedIvy, opts);
} catch (SAXException ex) {
throw new RuntimeException("bad ivy file in cache for " + mrid, ex);
}
Message.verbose("\tdeliver done (" + (System.currentTimeMillis() - start) + "ms)");
}
use of org.apache.ivy.core.module.descriptor.DependencyDescriptor in project ant-ivy by apache.
the class IvyDeliverTest method testWithBranch.
/**
* Test case for IVY-404.
*
* @throws Exception if something goes wrong
* @see <a href="https://issues.apache.org/jira/browse/IVY-404">IVY-404</a>
*/
@Test
public void testWithBranch() throws Exception {
project.setProperty("ivy.dep.file", "test/java/org/apache/ivy/ant/ivy-latest-branch.xml");
IvyResolve res = new IvyResolve();
res.setProject(project);
res.execute();
deliver.setPubrevision("1.2");
deliver.setDeliverpattern("build/test/deliver/ivy-[revision].xml");
deliver.execute();
// should have done the ivy delivering
File deliveredIvyFile = new File("build/test/deliver/ivy-1.2.xml");
assertTrue(deliveredIvyFile.exists());
ModuleDescriptor md = XmlModuleDescriptorParser.getInstance().parseDescriptor(new IvySettings(), deliveredIvyFile.toURI().toURL(), true);
assertEquals(ModuleRevisionId.newInstance("apache", "resolve-latest", "1.2"), md.getModuleRevisionId());
DependencyDescriptor[] dds = md.getDependencies();
assertEquals(1, dds.length);
assertEquals(ModuleRevisionId.newInstance("org1", "mod1.2", "TRUNK", "2.2"), dds[0].getDependencyRevisionId());
}
Aggregations