Package org.springframework.roo.project.maven

Examples of org.springframework.roo.project.maven.Pom


                "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) {
            // Can't filter the XPath on groupId, as it's optional in the POM
            // 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);
    }
View Full Code Here


                "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)) {
                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);
    }
View Full Code Here

            final Dependency dependency, final String containingPath,
            final String path) {
        Validate.isTrue(isProjectAvailable(moduleName),
                "Dependency modification prohibited at this time");
        Validate.notNull(dependency, "Dependency to remove required");
        final Pom pom = getPomFromModuleName(moduleName);
        Validate.notNull(pom,
                "The pom is not available, so dependency removal cannot be performed");
        if (!pom.isDependencyRegistered(dependency)) {
            return;
        }

        final Document document = XmlUtils.readXml(fileManager
                .getInputStream(pom.getPath()));
        final Element root = document.getDocumentElement();

        String descriptionOfChange = "";
        final Element dependenciesElement = XmlUtils.findFirstElement(
                containingPath, root);
        for (final Element candidate : XmlUtils.findElements(path, root)) {
            if (dependency.equals(new Dependency(candidate))) {
                dependenciesElement.removeChild(candidate);
                descriptionOfChange = highlight(REMOVED + " dependency") + " "
                        + dependency.getSimpleDescription();
                // Stay in the loop, just in case it was in the POM more than
                // once
            }
        }

        DomUtils.removeTextNodes(dependenciesElement);

        fileManager.createOrUpdateTextFileIfRequired(pom.getPath(),
                XmlUtils.nodeToString(document), descriptionOfChange, false);
    }
View Full Code Here

    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);
    }
View Full Code Here

    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);
    }
View Full Code Here

    private void removeRepository(final String moduleName,
            final Repository repository, final String path) {
        Validate.isTrue(isProjectAvailable(moduleName),
                "Repository modification prohibited at this time");
        Validate.notNull(repository, "Repository required");
        final Pom pom = getPomFromModuleName(moduleName);
        Validate.notNull(pom,
                "The pom is not available, so repository removal cannot be performed");
        if ("pluginRepository".equals(path)) {
            if (!pom.isPluginRepositoryRegistered(repository)) {
                return;
            }
        }
        else {
            if (!pom.isRepositoryRegistered(repository)) {
                return;
            }
        }

        final Document document = XmlUtils.readXml(fileManager
                .getInputStream(pom.getPath()));
        final Element root = document.getDocumentElement();

        String descriptionOfChange = "";
        for (final Element candidate : XmlUtils.findElements(path, root)) {
            if (repository.equals(new Repository(candidate))) {
                candidate.getParentNode().removeChild(candidate);
                descriptionOfChange = highlight(REMOVED + " repository") + " "
                        + repository.getUrl();
                // We stay in the loop just in case it was in the POM more than
                // once
            }
        }

        fileManager.createOrUpdateTextFileIfRequired(pom.getPath(),
                XmlUtils.nodeToString(document), descriptionOfChange, false);
    }
View Full Code Here

    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);
    }
View Full Code Here

            features.remove(feature.getName());
        }
    }

    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;
            }
        }
View Full Code Here

    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);
        }
    }
View Full Code Here

    }

    public void updateProjectType(final String moduleName,
            final ProjectType projectType) {
        Validate.notNull(projectType, "Project type required");
        final Pom pom = getPomFromModuleName(moduleName);
        Validate.notNull(pom,
                "The pom is not available, so the project type cannot be changed");

        final Document document = XmlUtils.readXml(fileManager
                .getInputStream(pom.getPath()));
        final Element packaging = DomUtils.createChildIfNotExists("packaging",
                document.getDocumentElement(), document);
        if (packaging.getTextContent().equals(projectType.getType())) {
            return;
        }

        packaging.setTextContent(projectType.getType());
        final String descriptionOfChange = highlight(UPDATED + " project type")
                + " to " + projectType.getType();

        fileManager.createOrUpdateTextFileIfRequired(pom.getPath(),
                XmlUtils.nodeToString(document), descriptionOfChange, false);
    }
View Full Code Here

TOP

Related Classes of org.springframework.roo.project.maven.Pom

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.