use of org.apache.ivy.core.resolve.ResolvedModuleRevision in project ant-ivy by apache.
the class DefaultRepositoryCacheManager method doFindModuleInCache.
private ResolvedModuleRevision doFindModuleInCache(ModuleRevisionId mrid, CacheMetadataOptions options, String expectedResolver) {
if (!lockMetadataArtifact(mrid)) {
Message.error("impossible to acquire lock for " + mrid);
return null;
}
boolean unlock = true;
try {
if (settings.getVersionMatcher().isDynamic(mrid)) {
String resolvedRevision = getResolvedRevision(expectedResolver, mrid, options);
if (resolvedRevision == null) {
return null;
}
Message.verbose("found resolved revision in cache: " + mrid + " => " + resolvedRevision);
// we have found another module in the cache, make sure we unlock
// the original module
unlockMetadataArtifact(mrid);
mrid = ModuleRevisionId.newInstance(mrid, resolvedRevision);
// don't forget to request a lock on the new module!
if (!lockMetadataArtifact(mrid)) {
Message.error("impossible to acquire lock for " + mrid);
// we couldn't lock the new module, so no need to unlock it
unlock = false;
return null;
}
}
File ivyFile = getIvyFileInCache(mrid);
if (ivyFile.exists()) {
// found in cache !
try {
ModuleDescriptorParser parser = getModuleDescriptorParser(ivyFile);
ModuleDescriptor depMD = getMdFromCache(parser, options, ivyFile);
String resolverName = getSavedResolverName(depMD);
String artResolverName = getSavedArtResolverName(depMD);
DependencyResolver resolver = settings.getResolver(resolverName);
if (resolver == null) {
Message.debug("\tresolver not found: " + resolverName + " => trying to use the one configured for " + mrid);
resolver = settings.getResolver(depMD.getResolvedModuleRevisionId());
if (resolver != null) {
Message.debug("\tconfigured resolver found for " + depMD.getResolvedModuleRevisionId() + ": " + resolver.getName() + ": saving this data");
saveResolver(depMD, resolver.getName());
}
}
DependencyResolver artResolver = settings.getResolver(artResolverName);
if (artResolver == null) {
artResolver = resolver;
}
if (resolver != null) {
Message.debug("\tfound ivy file in cache for " + mrid + " (resolved by " + resolver.getName() + "): " + ivyFile);
if (expectedResolver == null || expectedResolver.equals(resolver.getName())) {
MetadataArtifactDownloadReport madr = new MetadataArtifactDownloadReport(depMD.getMetadataArtifact());
madr.setDownloadStatus(DownloadStatus.NO);
madr.setSearched(false);
madr.setLocalFile(ivyFile);
madr.setSize(ivyFile.length());
madr.setArtifactOrigin(getSavedArtifactOrigin(depMD.getMetadataArtifact()));
if (madr.getArtifactOrigin().isExists()) {
if (madr.getArtifactOrigin().isLocal() && madr.getArtifactOrigin().getArtifact().getUrl() != null) {
madr.setOriginalLocalFile(new File(madr.getArtifactOrigin().getArtifact().getUrl().toURI()));
} else {
// find locally cached file
madr.setOriginalLocalFile(getArchiveFileInCache(madr.getArtifactOrigin().getArtifact()));
}
}
return new ResolvedModuleRevision(resolver, artResolver, depMD, madr);
}
Message.debug("found module in cache but with a different resolver: " + "discarding: " + mrid + "; expected resolver=" + expectedResolver + "; resolver=" + resolver.getName());
} else {
Message.debug("\tresolver not found: " + resolverName + " => cannot use cached ivy file for " + mrid);
}
} catch (Exception e) {
// will try with resolver
Message.debug("\tproblem while parsing cached ivy file for: " + mrid, e);
}
} else {
Message.debug("\tno ivy file in cache for " + mrid + ": tried " + ivyFile);
}
} finally {
if (unlock) {
unlockMetadataArtifact(mrid);
}
}
return null;
}
use of org.apache.ivy.core.resolve.ResolvedModuleRevision in project ant-ivy by apache.
the class IvyInfo method doExecute.
public void doExecute() throws BuildException {
Ivy ivy = getIvyInstance();
IvySettings settings = ivy.getSettings();
try {
if (organisation != null || module != null || revision != null || branch != null) {
if (organisation == null) {
throw new BuildException("no organisation provided for ivy info task");
}
if (module == null) {
throw new BuildException("no module name provided for ivy info task");
}
if (revision == null) {
throw new BuildException("no revision provided for ivy info task");
}
if (branch == null) {
settings.getDefaultBranch(new ModuleId(organisation, module));
}
ResolvedModuleRevision rmr = ivy.findModule(ModuleRevisionId.newInstance(organisation, module, branch, revision));
if (rmr != null) {
ModuleDescriptor md = rmr.getDescriptor();
ModuleRevisionId mrid = rmr.getId();
setProperties(md, mrid);
}
} else {
if (file == null) {
file = getProject().resolveFile(getProperty(settings, "ivy.dep.file"));
}
ModuleDescriptor md = ModuleDescriptorParserRegistry.getInstance().parseDescriptor(settings, file.toURI().toURL(), doValidate(settings));
ModuleRevisionId mrid = md.getModuleRevisionId();
setProperties(md, mrid);
}
} catch (MalformedURLException e) {
throw new BuildException("unable to convert given ivy file to url: " + file + ": " + e, e);
} catch (ParseException e) {
log(e.getMessage(), Project.MSG_ERR);
throw new BuildException("syntax errors in ivy file: " + e, e);
} catch (Exception e) {
throw new BuildException("impossible to resolve dependencies: " + e, e);
}
}
use of org.apache.ivy.core.resolve.ResolvedModuleRevision in project ant-ivy by apache.
the class IvyArtifactReport method startModule.
private void startModule(TransformerHandler saxHandler, IvyNode dependency) throws SAXException {
AttributesImpl moduleAttrs = new AttributesImpl();
moduleAttrs.addAttribute(null, "organisation", "organisation", "CDATA", dependency.getModuleId().getOrganisation());
moduleAttrs.addAttribute(null, "name", "name", "CDATA", dependency.getModuleId().getName());
ResolvedModuleRevision moduleRevision = dependency.getModuleRevision();
moduleAttrs.addAttribute(null, "rev", "rev", "CDATA", moduleRevision.getId().getRevision());
moduleAttrs.addAttribute(null, "status", "status", "CDATA", moduleRevision.getDescriptor().getStatus());
saxHandler.startElement(null, "module", "module", moduleAttrs);
}
use of org.apache.ivy.core.resolve.ResolvedModuleRevision in project ant-ivy by apache.
the class PomModuleDescriptorParser method parseDescriptor.
public ModuleDescriptor parseDescriptor(ParserSettings ivySettings, URL descriptorURL, Resource res, boolean validate) throws ParseException, IOException {
PomModuleDescriptorBuilder mdBuilder = new PomModuleDescriptorBuilder(this, res, ivySettings);
try {
final IvyContext ivyContext = IvyContext.pushNewCopyContext();
HashSet<ModuleRevisionId> parents = ivyContext.get(PARENT_MAP_KEY);
if (parents == null) {
parents = new LinkedHashSet<>();
ivyContext.set(PARENT_MAP_KEY, parents);
}
PomReader domReader = new PomReader(descriptorURL, res);
domReader.setProperty("parent.version", domReader.getParentVersion());
domReader.setProperty("parent.groupId", domReader.getParentGroupId());
domReader.setProperty("project.parent.version", domReader.getParentVersion());
domReader.setProperty("project.parent.groupId", domReader.getParentGroupId());
Message.debug("parent.groupId: " + domReader.getParentGroupId());
Message.debug("parent.artifactId: " + domReader.getParentArtifactId());
Message.debug("parent.version: " + domReader.getParentVersion());
for (final Map.Entry<String, String> prop : domReader.getPomProperties().entrySet()) {
domReader.setProperty(prop.getKey(), prop.getValue());
mdBuilder.addProperty(prop.getKey(), prop.getValue());
}
final List<PomProfileElement> activeProfiles = new ArrayList<>();
// add profile specific properties
for (final PomProfileElement profile : domReader.getProfiles()) {
if (!profile.isActive()) {
continue;
}
// keep track of this active profile for later use
activeProfiles.add(profile);
final Map<String, String> profileProps = profile.getProfileProperties();
if (profileProps.isEmpty()) {
continue;
}
for (final Map.Entry<String, String> entry : profileProps.entrySet()) {
domReader.setProperty(entry.getKey(), entry.getValue());
mdBuilder.addProperty(entry.getKey(), entry.getValue());
}
}
ModuleDescriptor parentDescr = null;
if (domReader.hasParent()) {
// Is there any other parent properties?
ModuleRevisionId parentModRevID = ModuleRevisionId.newInstance(domReader.getParentGroupId(), domReader.getParentArtifactId(), domReader.getParentVersion());
// check for cycles
if (parents.contains(parentModRevID)) {
throw new CircularDependencyException(parents);
} else {
parents.add(parentModRevID);
}
ResolvedModuleRevision parentModule = parseOtherPom(ivySettings, parentModRevID);
if (parentModule == null) {
throw new IOException("Impossible to load parent for " + res.getName() + ". Parent=" + parentModRevID);
}
parentDescr = parentModule.getDescriptor();
if (parentDescr != null) {
for (Map.Entry<String, String> prop : extractPomProperties(parentDescr.getExtraInfos()).entrySet()) {
domReader.setProperty(prop.getKey(), prop.getValue());
}
}
}
String groupId = domReader.getGroupId();
String artifactId = domReader.getArtifactId();
String version = domReader.getVersion();
mdBuilder.setModuleRevId(groupId, artifactId, version);
mdBuilder.setHomePage(domReader.getHomePage());
mdBuilder.setDescription(domReader.getDescription());
// if this module doesn't have an explicit license, use the parent's license (if any)
final License[] licenses = domReader.getLicenses();
if (licenses != null && licenses.length > 0) {
mdBuilder.setLicenses(licenses);
} else if (parentDescr != null) {
mdBuilder.setLicenses(parentDescr.getLicenses());
}
ModuleRevisionId relocation = domReader.getRelocation();
if (relocation != null) {
if (groupId != null && artifactId != null && artifactId.equals(relocation.getName()) && groupId.equals(relocation.getOrganisation())) {
Message.error("Relocation to an other version number not supported in ivy : " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " relocated to " + relocation + ". Please update your dependency to directly use the right version.");
Message.warn("Resolution will only pick dependencies of the relocated element." + " Artifact and other metadata will be ignored.");
ResolvedModuleRevision relocatedModule = parseOtherPom(ivySettings, relocation);
if (relocatedModule == null) {
throw new ParseException("impossible to load module " + relocation + " to which " + mdBuilder.getModuleDescriptor().getModuleRevisionId() + " has been relocated", 0);
}
for (DependencyDescriptor dd : relocatedModule.getDescriptor().getDependencies()) {
mdBuilder.addDependency(dd);
}
} else {
Message.info(mdBuilder.getModuleDescriptor().getModuleRevisionId() + " is relocated to " + relocation + ". Please update your dependencies.");
Message.verbose("Relocated module will be considered as a dependency");
DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(mdBuilder.getModuleDescriptor(), relocation, true, false, true);
/* Map all public dependencies */
for (Configuration m2Conf : MAVEN2_CONFIGURATIONS) {
if (PUBLIC.equals(m2Conf.getVisibility())) {
dd.addDependencyConfiguration(m2Conf.getName(), m2Conf.getName());
}
}
mdBuilder.addDependency(dd);
}
} else {
domReader.setProperty("project.groupId", groupId);
domReader.setProperty("pom.groupId", groupId);
domReader.setProperty("groupId", groupId);
domReader.setProperty("project.artifactId", artifactId);
domReader.setProperty("pom.artifactId", artifactId);
domReader.setProperty("artifactId", artifactId);
domReader.setProperty("project.version", version);
domReader.setProperty("pom.version", version);
domReader.setProperty("version", version);
if (parentDescr != null) {
mdBuilder.addExtraInfos(parentDescr.getExtraInfos());
// add dependency management info from parent
for (PomDependencyMgt dep : getDependencyManagements(parentDescr)) {
if (dep instanceof PomDependencyMgtElement) {
dep = domReader.new PomDependencyMgtElement((PomDependencyMgtElement) dep);
}
mdBuilder.addDependencyMgt(dep);
}
// add plugins from parent
for (PomDependencyMgt pomDependencyMgt : getPlugins(parentDescr)) {
mdBuilder.addPlugin(pomDependencyMgt);
}
}
for (PomDependencyMgt dep : domReader.getDependencyMgt()) {
addTo(mdBuilder, dep, ivySettings);
}
for (PomDependencyData dep : domReader.getDependencies()) {
mdBuilder.addDependency(res, dep);
}
for (PomPluginElement plugin : domReader.getPlugins()) {
mdBuilder.addPlugin(plugin);
}
// consult active profiles:
for (final PomProfileElement activeProfile : activeProfiles) {
for (PomDependencyMgt dep : activeProfile.getDependencyMgt()) {
addTo(mdBuilder, dep, ivySettings);
}
for (PomDependencyData dep : activeProfile.getDependencies()) {
mdBuilder.addDependency(res, dep);
}
for (PomPluginElement plugin : activeProfile.getPlugins()) {
mdBuilder.addPlugin(plugin);
}
}
if (parentDescr != null) {
for (DependencyDescriptor descriptor : parentDescr.getDependencies()) {
if (descriptor instanceof PomDependencyDescriptor) {
PomDependencyData parentDep = ((PomDependencyDescriptor) descriptor).getPomDependencyData();
PomDependencyData dep = domReader.new PomDependencyData(parentDep);
mdBuilder.addDependency(res, dep);
} else {
mdBuilder.addDependency(descriptor);
}
}
}
mdBuilder.addMainArtifact(artifactId, domReader.getPackaging());
addSourcesAndJavadocArtifactsIfPresent(mdBuilder, ivySettings);
}
} catch (SAXException e) {
throw newParserException(e);
} finally {
IvyContext.popContext();
}
return mdBuilder.getModuleDescriptor();
}
use of org.apache.ivy.core.resolve.ResolvedModuleRevision in project ant-ivy by apache.
the class ChainResolverTest method testReturnFirst.
@Test
public void testReturnFirst() throws Exception {
ChainResolver chain = new ChainResolver();
chain.setName("chain");
chain.setSettings(settings);
chain.setReturnFirst(true);
MockResolver[] resolvers = new MockResolver[] { MockResolver.buildMockResolver(settings, "1", true, new GregorianCalendar(2005, 1, 20).getTime()), MockResolver.buildMockResolver(settings, "2", false, null), MockResolver.buildMockResolver(settings, "3", true, new GregorianCalendar(2005, 1, 25).getTime()), // younger -> should the one kept
MockResolver.buildMockResolver(settings, "4", false, null), MockResolver.buildMockResolver(settings, "5", true, new GregorianCalendar(2005, 1, 22).getTime()), MockResolver.buildMockResolver(settings, "6", true, new GregorianCalendar(2005, 1, 18).getTime()), MockResolver.buildMockResolver(settings, "7", false, null) };
for (MockResolver resolver : resolvers) {
chain.add(resolver);
}
assertResolversSizeAndNames(chain, resolvers.length);
DefaultDependencyDescriptor dd = new DefaultDependencyDescriptor(ModuleRevisionId.newInstance("org", "mod", "latest.integration"), false);
ResolvedModuleRevision rmr = chain.getDependency(dd, data);
assertNotNull(rmr);
assertEquals("1", rmr.getResolver().getName());
for (int i = 1; i < resolvers.length; i++) {
assertTrue(resolvers[i].askedDeps.isEmpty());
}
}
Aggregations