Search in sources :

Example 11 with Pom

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);
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Pom(org.springframework.roo.project.maven.Pom)

Example 12 with Pom

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);
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Pom(org.springframework.roo.project.maven.Pom)

Example 13 with Pom

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);
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Pom(org.springframework.roo.project.maven.Pom)

Example 14 with Pom

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);
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Pom(org.springframework.roo.project.maven.Pom)

Example 15 with Pom

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);
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Pom(org.springframework.roo.project.maven.Pom)

Aggregations

Pom (org.springframework.roo.project.maven.Pom)86 Element (org.w3c.dom.Element)19 JavaType (org.springframework.roo.model.JavaType)16 Document (org.w3c.dom.Document)16 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)10 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)9 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)9 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)9 Dependency (org.springframework.roo.project.Dependency)9 RooJavaType (org.springframework.roo.model.RooJavaType)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 ServiceReference (org.osgi.framework.ServiceReference)5 ControllerMVCResponseService (org.springframework.roo.addon.web.mvc.controller.addon.responses.ControllerMVCResponseService)5 JavaPackage (org.springframework.roo.model.JavaPackage)5 List (java.util.List)4 Plugin (org.springframework.roo.project.Plugin)4 Completion (org.springframework.roo.shell.Completion)4 HashMap (java.util.HashMap)3 DataOnDemandCreatorProvider (org.springframework.roo.addon.test.providers.DataOnDemandCreatorProvider)3