use of org.apache.ivy.plugins.parser.m2.PomReader.PomProfileElement 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();
}
Aggregations