use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method removeDependencies.
public void removeDependencies(final String moduleName, final Collection<? extends Dependency> dependenciesToRemove) {
Validate.isTrue(isProjectAvailable(moduleName), "Dependency modification prohibited at this time");
Validate.notNull(dependenciesToRemove, "Dependencies required");
if (CollectionUtils.isEmpty(dependenciesToRemove)) {
return;
}
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so dependency removal cannot be performed");
if (!pom.isAnyDependenciesRegistered(dependenciesToRemove)) {
return;
}
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element root = document.getDocumentElement();
final Element dependenciesElement = XmlUtils.findFirstElement("/project/dependencies", root);
if (dependenciesElement == null) {
return;
}
final List<Element> existingDependencyElements = XmlUtils.findElements("dependency", dependenciesElement);
final List<String> removedDependencies = new ArrayList<String>();
for (final Dependency dependencyToRemove : dependenciesToRemove) {
if (pom.isDependencyRegistered(dependencyToRemove, false)) {
for (final Iterator<Element> iter = existingDependencyElements.iterator(); iter.hasNext(); ) {
final Element candidate = iter.next();
final Dependency candidateDependency = new Dependency(candidate);
if (candidateDependency.equals(dependencyToRemove)) {
// It's the same dependency; remove it
dependenciesElement.removeChild(candidate);
// Ensure we don't try to remove it again for another
// Dependency
iter.remove();
removedDependencies.add(candidateDependency.getSimpleDescription());
}
// Keep looping in case it's in the POM more than once
}
}
}
if (removedDependencies.isEmpty()) {
return;
}
DomUtils.removeTextNodes(dependenciesElement);
final String message = getDescriptionOfChange(REMOVED, removedDependencies, "dependency", "dependencies");
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), message, false);
}
use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method removeFilter.
public void removeFilter(final String moduleName, final Filter filter) {
Validate.isTrue(isProjectAvailable(moduleName), "Filter modification prohibited at this time");
Validate.notNull(filter, "Filter required");
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so filter removal cannot be performed");
if (filter == null || !pom.isFilterRegistered(filter)) {
return;
}
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element root = document.getDocumentElement();
final Element filtersElement = XmlUtils.findFirstElement("/project/build/filters", root);
if (filtersElement == null) {
return;
}
String descriptionOfChange = "";
for (final Element candidate : XmlUtils.findElements("filter", filtersElement)) {
if (filter.equals(new Filter(candidate))) {
filtersElement.removeChild(candidate);
descriptionOfChange = highlight(REMOVED + " filter") + " '" + filter.getValue() + "'";
// We will not break the loop (even though we could
// theoretically), just in case it was in the POM more than once
}
}
final List<Element> filterElements = XmlUtils.findElements("filter", filtersElement);
if (filterElements.isEmpty()) {
filtersElement.getParentNode().removeChild(filtersElement);
}
DomUtils.removeTextNodes(root);
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 removeProperty.
public void removeProperty(final String moduleName, final Property property) {
Validate.isTrue(isProjectAvailable(moduleName), "Property modification prohibited at this time");
Validate.notNull(property, "Property to remove required");
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so property removal cannot be performed");
if (!pom.isPropertyRegistered(property)) {
return;
}
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element root = document.getDocumentElement();
final Element propertiesElement = XmlUtils.findFirstElement("/project/properties", root);
String descriptionOfChange = "";
for (final Element candidate : XmlUtils.findElements("/project/properties/*", document.getDocumentElement())) {
if (property.equals(new Property(candidate))) {
propertiesElement.removeChild(candidate);
descriptionOfChange = highlight(REMOVED + " property") + " " + property.getName();
// Stay in the loop just in case it was in the POM more than
// once
}
}
DomUtils.removeTextNodes(propertiesElement);
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 removeBuildPlugins.
private void removeBuildPlugins(final String moduleName, final Collection<? extends Plugin> plugins, final boolean writeImmediately) {
Validate.isTrue(isProjectAvailable(moduleName), "Plugin modification prohibited at this time");
Validate.notNull(plugins, "Plugins required");
if (CollectionUtils.isEmpty(plugins)) {
return;
}
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so plugin removal cannot be performed");
if (!pom.isAnyPluginsRegistered(plugins)) {
return;
}
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element root = document.getDocumentElement();
final Element pluginsElement = XmlUtils.findFirstElement("/project/build/plugins", root);
if (pluginsElement == null) {
return;
}
final List<String> removedPlugins = new ArrayList<String>();
for (final Plugin plugin : plugins) {
// for Apache-owned plugins
for (final Element candidate : XmlUtils.findElements("plugin[artifactId = '" + plugin.getArtifactId() + "' and version = '" + plugin.getVersion() + "']", pluginsElement)) {
final Plugin candidatePlugin = new Plugin(candidate);
if (candidatePlugin.getGroupId().equals(plugin.getGroupId())) {
// This element has the same groupId, artifactId, and
// version as the plugin to be removed; remove it
pluginsElement.removeChild(candidate);
removedPlugins.add(candidatePlugin.getSimpleDescription());
// Keep looping in case this plugin is in the POM more than
// once (unlikely)
}
}
}
if (removedPlugins.isEmpty()) {
return;
}
DomUtils.removeTextNodes(pluginsElement);
final String message = getDescriptionOfChange(REMOVED, removedPlugins, "plugin", "plugins");
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), message, writeImmediately);
}
use of org.springframework.roo.project.maven.Pom in project spring-roo by spring-projects.
the class AbstractProjectOperations method addRepositories.
private void addRepositories(final String moduleName, final Collection<? extends Repository> repositories, final String containingPath, final String path) {
Validate.isTrue(isProjectAvailable(moduleName), "Repository modification prohibited at this time");
Validate.notNull(repositories, "Repositories required");
if (CollectionUtils.isEmpty(repositories)) {
return;
}
final Pom pom = getPomFromModuleName(moduleName);
Validate.notNull(pom, "The pom is not available, so repository addition cannot be performed");
final Document document = XmlUtils.readXml(fileManager.getInputStream(pom.getPath()));
final Element repositoriesElement = DomUtils.createChildIfNotExists(containingPath, document.getDocumentElement(), document);
if ("pluginRepository".equals(path)) {
if (pom.isAllPluginRepositoriesRegistered(repositories)) {
return;
}
} else if (pom.isAllRepositoriesRegistered(repositories)) {
return;
}
final List<Repository> existingRepositories = new ArrayList<Repository>();
for (Element exisitingRepElement : XmlUtils.findElements(path, repositoriesElement)) {
existingRepositories.add(new Repository(exisitingRepElement));
}
final List<String> addedRepositories = new ArrayList<String>();
for (final Repository repository : repositories) {
if ("pluginRepository".equals(path)) {
if (pom.isPluginRepositoryRegistered(repository)) {
continue;
} else if (existingRepositories.contains(repository)) {
continue;
}
} else {
if (pom.isRepositoryRegistered(repository)) {
continue;
} else if (existingRepositories.contains(repository)) {
continue;
}
}
if (repository != null) {
repositoriesElement.appendChild(repository.getElement(document, path));
addedRepositories.add(repository.getUrl());
}
}
final String message = getDescriptionOfChange(ADDED, addedRepositories, path, containingPath);
fileManager.createOrUpdateTextFileIfRequired(pom.getPath(), XmlUtils.nodeToString(document), message, false);
}
Aggregations