use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method addProperty.
public void addProperty(final String moduleName, final Property property) {
Validate.isTrue(isProjectAvailable(moduleName), "Property modification prohibited at this time");
Validate.notNull(property, "Property to add required");
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so property addition cannot be performed");
if (pom.isPropertyRegistered(property)) {
return;
}
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element root = document.getDocumentElement();
final String descriptionOfChange;
final Element existing = XmlUtils.findFirstElement("/project/properties/" + property.getName(), root);
if (existing == null) {
// No existing property of this name; add it
final Element properties = DomUtils.createChildIfNotExists("properties", document.getDocumentElement(), document);
properties.appendChild(XmlUtils.createTextElement(document, property.getName(), property.getValue()));
descriptionOfChange = highlight(ADDED + " property") + " '" + property.getName() + "' = '" + property.getValue() + "'";
} else {
// A property of this name exists; update it
existing.setTextContent(property.getValue());
descriptionOfChange = highlight(UPDATED + " property") + " '" + property.getName() + "' to '" + property.getValue() + "'";
}
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false);
}
use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method updateBuildPlugin.
public void updateBuildPlugin(final String moduleName, final Plugin plugin) {
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so plugins cannot be modified at this time");
Validate.notNull(plugin, "Plugin required");
for (final Plugin existingPlugin : pom.getBuildPlugins()) {
if (existingPlugin.equals(plugin)) {
// Already exists, so just quit
return;
}
}
// Delete any existing plugin with a different version
removeBuildPlugin(moduleName, plugin);
// Add the plugin
addBuildPlugin(moduleName, plugin);
}
use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method addElementToPluginExecution.
@Override
public void addElementToPluginExecution(final String moduleName, final Plugin plugin, String executionId, String parentElementName, String elementName, Map<String, String> elementValues, boolean addToPluginManagement) {
Validate.isTrue(isProjectAvailable(moduleName), "Package modification prohibited at this time");
Validate.notNull(executionId, "Execution id required");
Validate.notNull(plugin, "Plugin required");
Validate.notNull(parentElementName, "parentElementName required");
Validate.notNull(elementName, "elementName required");
Validate.notNull(elementValues, "elementValues required");
String descriptionOfChange;
final Pom parentPom = getPomFromModuleName("");
boolean isSamePom = false;
Pom pom = null;
if ("".equals(moduleName)) {
isSamePom = true;
pom = parentPom;
} else {
pom = getPomFromModuleName(moduleName);
}
Validate.notNull(parentPom, "The parentPom is not available, so plugin addition cannot be performed");
Validate.notNull(pom, "The pom is not available, so plugin addition cannot be performed");
final Document parentDocument = XmlUtils.readXml(fileManager.getInputStream(parentPom.getPath()));
Document document = null;
if (isSamePom) {
document = parentDocument;
} else {
document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
}
final Element parentRoot = parentDocument.getDocumentElement();
final Element root = document.getDocumentElement();
// Find plugin
Element pluginsElement = null;
if (addToPluginManagement) {
pluginsElement = DomUtils.createChildIfNotExists("build/pluginManagement/plugins", parentRoot, parentDocument);
} else {
pluginsElement = DomUtils.createChildIfNotExists("/project/build/plugins", root, document);
}
final List<Element> existingPluginElements = XmlUtils.findElements("plugin", pluginsElement);
for (final Element existingPluginElement : existingPluginElements) {
final Plugin existingPlugin = new Plugin(existingPluginElement);
if (existingPlugin.hasSameCoordinates(plugin)) {
for (final Execution execution : existingPlugin.getExecutions()) {
if (executionId.equals(execution.getId()) && execution.getConfiguration() != null) {
// Check if element is already added
final Element elementsElement = DomUtils.createChildIfNotExists(parentElementName, execution.getConfiguration().getConfiguration(), document);
final List<Element> existingElements = XmlUtils.findElements(elementName, elementsElement);
for (Element existingElement : existingElements) {
for (Entry<String, String> element : elementValues.entrySet()) {
if (elementValues.size() == 1 && element.getKey().equals(DEFAULT_VALUE_TEXT)) {
final String elem = DomUtils.getTextContent(existingElement, "");
if (elem.equals(element.getValue())) {
return;
}
} else {
Element childElement = DomUtils.getChildElementByTagName(existingElement, element.getKey());
if (childElement != null && DomUtils.getTextContent(childElement, "").equals(element.getValue())) {
return;
}
}
}
}
// No such package; add it
Element newParentElement = null;
for (Entry<String, String> element : elementValues.entrySet()) {
descriptionOfChange = highlight(ADDED + " " + elementName + "/" + element.getKey()) + " '" + element.getValue() + "'";
if (elementValues.size() == 1 && element.getKey().equals(DEFAULT_VALUE_TEXT)) {
if (!isSamePom && addToPluginManagement) {
elementsElement.appendChild(XmlUtils.createTextElement(parentDocument, elementName, element.getValue()));
fileManager.createOrUpdateTextFileIfRequired(parentPom.getPath(), XmlUtils.nodeToString(parentDocument), descriptionOfChange, false);
} else {
elementsElement.appendChild(XmlUtils.createTextElement(document, elementName, element.getValue()));
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false);
}
} else {
if (!isSamePom && addToPluginManagement) {
if (newParentElement == null) {
newParentElement = DomUtils.createChildIfNotExists(elementName, execution.getConfiguration().getConfiguration(), parentDocument);
elementsElement.appendChild(newParentElement);
}
newParentElement.appendChild(XmlUtils.createTextElement(parentDocument, element.getKey(), element.getValue()));
fileManager.createOrUpdateTextFileIfRequired(parentPom.getPath(), XmlUtils.nodeToString(parentDocument), descriptionOfChange, false);
} else {
if (newParentElement == null) {
newParentElement = DomUtils.createChildIfNotExists(elementName, execution.getConfiguration().getConfiguration(), document);
elementsElement.appendChild(newParentElement);
}
newParentElement.appendChild(XmlUtils.createTextElement(document, element.getKey(), element.getValue()));
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false);
}
}
}
}
}
}
}
}
use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method addResource.
public void addResource(final String moduleName, final Resource resource) {
Validate.isTrue(isProjectAvailable(moduleName), "Resource modification prohibited at this time");
Validate.notNull(resource, "Resource to add required");
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so resource addition cannot be performed");
if (pom.isResourceRegistered(resource)) {
return;
}
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element buildElement = XmlUtils.findFirstElement("/project/build", document.getDocumentElement());
final Element resourcesElement = DomUtils.createChildIfNotExists("resources", buildElement, document);
resourcesElement.appendChild(resource.getElement(document));
final String descriptionOfChange = highlight(ADDED + " resource") + " " + resource.getSimpleDescription();
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false);
}
use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method removeResource.
public void removeResource(final String moduleName, final Resource resource) {
Validate.isTrue(isProjectAvailable(moduleName), "Resource modification prohibited at this time");
Validate.notNull(resource, "Resource required");
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so resource removal cannot be performed");
if (!pom.isResourceRegistered(resource)) {
return;
}
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element root = document.getDocumentElement();
final Element resourcesElement = XmlUtils.findFirstElement("/project/build/resources", root);
if (resourcesElement == null) {
return;
}
String descriptionOfChange = "";
for (final Element candidate : XmlUtils.findElements("resource[directory = '" + resource.getDirectory() + "']", resourcesElement)) {
if (resource.equals(new Resource(candidate))) {
resourcesElement.removeChild(candidate);
descriptionOfChange = highlight(REMOVED + " resource") + " " + resource.getSimpleDescription();
// Stay in the loop just in case it was in the POM more than
// once
}
}
final List<Element> resourceElements = XmlUtils.findElements("resource", resourcesElement);
if (resourceElements.isEmpty()) {
resourcesElement.getParentNode().removeChild(resourcesElement);
}
DomUtils.removeTextNodes(root);
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), descriptionOfChange, false);
}
Aggregations