public void updateDependencyScope(final String moduleName,
final Dependency dependency, final DependencyScope dependencyScope) {
Validate.isTrue(isProjectAvailable(moduleName),
"Dependency modification prohibited at this time");
Validate.notNull(dependency, "Dependency to update required");
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom,
"The pom is not available, so updating a dependency cannot be performed");
if (!pom.isDependencyRegistered(dependency)) {
return;
}
final Document document = XmlUtils.readXml(fileManager
.getInputStream(pom.getPath()));
final Element root = document.getDocumentElement();
final Element dependencyElement = XmlUtils.findFirstElement(
"/project/dependencies/dependency[groupId = '"
+ dependency.getGroupId() + "' and artifactId = '"
+ dependency.getArtifactId() + "' and version = '"
+ dependency.getVersion() + "']", root);
if (dependencyElement == null) {
return;
}
final Element scopeElement = XmlUtils.findFirstElement("scope",
dependencyElement);
final String descriptionOfChange;
if (scopeElement == null) {
if (dependencyScope != null) {
dependencyElement.appendChild(new XmlElementBuilder("scope",
document).setText(dependencyScope.name().toLowerCase())
.build());
descriptionOfChange = highlight(ADDED + " scope") + " "
+ dependencyScope.name().toLowerCase()
+ " to dependency " + dependency.getSimpleDescription();
}
else {
descriptionOfChange = null;
}
}
else {
if (dependencyScope != null) {
scopeElement.setTextContent(dependencyScope.name()
.toLowerCase());
descriptionOfChange = highlight(CHANGED + " scope") + " to "
+ dependencyScope.name().toLowerCase()
+ " in dependency " + dependency.getSimpleDescription();
}
else {
dependencyElement.removeChild(scopeElement);
descriptionOfChange = highlight(REMOVED + " scope")
+ " from dependency "
+ dependency.getSimpleDescription();
}
}
if (descriptionOfChange != null) {
fileManager
.createOrUpdateTextFileIfRequired(pom.getPath(),
XmlUtils.nodeToString(document),
descriptionOfChange, false);
}
}