Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package io.fabric8.openshift.agent;
 
 
 import java.io.File;
 import java.util.List;

Updates the openshift profile of a pom.xml to add the given set of deployments into the build so that they get copied to the relevant deployDir and webAppDir as part of the build
 
 public class OpenShiftPomDeployer {
     private static final transient Logger LOG = LoggerFactory.getLogger(OpenShiftPomDeployer.class);
     private static final String INDENT = "  ";
 
     private final Git git;
     private final File baseDir;
     private final String deployDir;
     private final String webAppDir;
     private final String buildWarName = "build";
     private XPathBuilder xpathBuilder = new XPathBuilder();
 
     public OpenShiftPomDeployer(Git gitFile baseDirString deployDirString webAppDir) {
         this. = git;
         this. = baseDir;
         this. = deployDir;
         this. = webAppDir;
     }
 
         File pom = new File("pom.xml");
         Files.assertFileExists(pom);
 
         Document doc = XmlUtils.parseDoc(pom);
         Objects.notNull(doc"xml document");
 
         Element project = doc.getDocumentElement();
         Objects.notNull(doc"project element");
 
         Element dependencies = getOrCreateChild(project"dependencies", 1, true);
         Element repositories = getOrCreateChild(project"repositories", 1, true);
 
         Element openshiftPlugins = getOrCreateOpenShiftProfilePlugins(project);
 
 
         updateWarPlugin(openshiftPlugins);
         updateCleanPlugin(openshiftPlugins);
         updateDependencyPlugin(openshiftPluginsdependenciesartifacts);
         updateRepositories(repositoriesrepos);
 
         DomHelper.save(docpom);
         .add().addFilepattern("pom.xml").call();
     }

    
We usually need to either comment out the WAR plugin or at least update its destination file name
 
     protected void updateWarPlugin(Element pluginsthrows XPathExpressionException {
         Element plugin = getPlugin(plugins"maven-war-plugin");
        if (plugin != null) {
            Element warName = xpath("configuration/warName").element(plugin);
            if (warName != null) {
                String textContent = warName.getTextContent();
                if (Objects.equal("ROOT"textContent)) {
                    // lets overwrite the local build from being the root web app
                    warName.setTextContent();
                }
            }
        }
    }

    
Lets make sure we add the extra clean executions so that we remove any jars added in the build to the deployDir or webAppDir
    protected void updateCleanPlugin(Element pluginsthrows XPathExpressionException {
        Element plugin = getOrCreatePlugin(plugins"maven-clean-plugin""2.5");
        Element executions = getOrCreateChild(plugin"executions", 6);
        String executionId = "fuse-fabric-clean";
        Element execution = xpath("execution[id = '" + executionId + "']").element(executions);
        if (execution == null) {
            execution = getOrCreateChild(executions"execution", 7);
            createAndAppendChild(execution"id", 8, executionId);
            createAndAppendChild(execution"phase", 9, "initialise");
            Element goals = createAndAppendChild(execution"goals", 9);
            createAndAppendChild(goals"goal", 10, "clean");
        }
        Element configuration = recreateChild(execution"configuration", 9);
        Element filesets = createAndAppendChild(configuration"filesets", 10);
        if (Strings.isNotBlank()) {
            Element fileset = createAndAppendChild(filesets"fileset", 11);
            createAndAppendChild(fileset"directory", 12, "${basedir}/" + );
            Element includes = createAndAppendChild(fileset"includes", 12);
            createAndAppendChild(includes"include", 13, "**/*.war");
        }
        if (Strings.isNotBlank()) {
            Element fileset = createAndAppendChild(filesets"fileset", 11);
            createAndAppendChild(fileset"directory", 12, "${basedir}/" + );
            Element includes = createAndAppendChild(fileset"includes", 12);
            createAndAppendChild(includes"include", 13, "**/*.jar");
        }
    }


    
Lets add/update the maven dependency plugin configuration to copy deployments to the deployDir or the webAppDir
    protected void updateDependencyPlugin(Element pluginsElement dependenciesCollection<Parserartifactsthrows XPathExpressionException {
        Element plugin = getOrCreatePlugin(plugins"maven-dependency-plugin""2.8");
        Element executions = getOrCreateChild(plugin"executions", 6);
        List<ParserwarArtifacts = new ArrayList<Parser>();
        List<ParserjarArtifacts = new ArrayList<Parser>();
        for (Parser artifact : artifacts) {
            String type = artifact.getType();
            if (Objects.equal("war"type)) {
                warArtifacts.add(artifact);
            } else {
                jarArtifacts.add(artifact);
            }
        }
        if (Strings.isNotBlank() && !warArtifacts.isEmpty()) {
            recreateDependencyExecution(executionsdependencies"fuse-fabric-deploy-webapps"warArtifactstrue);
        }
        if (Strings.isNotBlank() && !jarArtifacts.isEmpty()) {
            recreateDependencyExecution(executionsdependencies"fuse-fabric-deploy-shared"jarArtifactsfalse);
        }
    }


    
Ensure that the given maven repositories are added to the pom.xml
    protected void updateRepositories(Element repositoriesList<MavenRepositoryURLreposthrows XPathExpressionException {
        for (MavenRepositoryURL repo : repos) {
            String url = repo.getURL().toString();
            String id = repo.getId();
            if (Strings.isNotBlank(url)) {
                Element repository = recreateChild(repositories"repository[url='" + url + "']""repository", 2);
                if (Strings.isNotBlank(id)) {
                    createAndAppendChild(repository"id", 3, id);
                }
                createAndAppendChild(repository"url", 3, url);
                addRepositoryFlag(repository"releases"repo.isReleasesEnabled());
                addRepositoryFlag(repository"snapshots"repo.isSnapshotsEnabled());
            }
        }
    }
    protected void addRepositoryFlag(Element repositoryString flagElementNameboolean flag) {
        Element flagElement = createAndAppendChild(repositoryflagElementName, 3);
        createAndAppendChild(flagElement"enabled", 4, flag ? "true" : "false");
    }
            Element profile = xpath("profiles/profile[id = 'openshift']").element(project);
            if (profile == null) {
                Element profiles = getOrCreateChild(project"profiles", 1, true);
                profile = createAndAppendChild(profiles"profile", 2);
                createAndAppendChild(profile"id", 3, "openshift");
            }
            Element build = getOrCreateChild(profile"build", 3);
            Element plugins = getOrCreateChild(build"plugins", 4);
            return plugins;
        }
    protected Element recreateDependencyExecution(Element executionsElement dependenciesString executionIdString outputDirList<Parserlistboolean isWarthrows XPathExpressionException {
        // lets make sure the output dir is trimmed of "/"
        while (outputDir.startsWith("/")) {
            outputDir = outputDir.substring(1);
        }
        Element execution = recreateChild(executions"execution[id = '" + executionId + "']""execution", 7);
        createAndAppendChild(execution"id", 8, executionId);
        createAndAppendChild(execution"phase", 8, "package");
        Element goals = createAndAppendChild(execution"goals", 8);
        createAndAppendChild(goals"goal", 9, "copy");
        Element configuration = createAndAppendChild(execution"configuration", 9);
        Element artifactItems = createAndAppendChild(configuration"artifactItems", 10);
        for (Parser parser : list) {
            Element artifactItem = createAndAppendChild(artifactItems"artifactItem", 11);
            addMavenCoordinates(artifactItemparser, 12);
            addOrUpdateDependency(dependenciesparser);
            createAndAppendChild(artifactItem"overWrite", 12, "true");
            createAndAppendChild(artifactItem"outputDirectory", 12, "${basedir}/" + outputDir);
            // TODO use ROOT if this is the configured web app!
            if (isWar) {
                createAndAppendChild(artifactItem"destFileName", 12, parser.getArtifact() + ".war");
            }
        }
        createAndAppendChild(configuration"outputDirectory", 10, "${basedir}/" + outputDir);
        createAndAppendChild(configuration"overWriteReleases", 10, "true");
        createAndAppendChild(configuration"overWriteSnapshots", 10, "true");
        return configuration;
    }
    protected void addOrUpdateDependency(Element dependenciesParser parserthrows XPathExpressionException {
        String group = groupId(parser);
        String artifact = parser.getArtifact();
        String xpath = "dependency[groupId = '" + group + "' and artifactId = '" + artifact + "'";
        String type = parser.getType();
        if (Strings.isNotBlank(type) && !Objects.equal("jar"type)) {
            xpath += " and type='" + type + "'";
        }
        String classifier = parser.getClassifier();
        if (Strings.isNotBlank(classifier)) {
            xpath += " and classifier='" + classifier + "'";
        }
        xpath += "]";
        String scope = "provided";
        Element dependency = xpath(xpath).element(dependencies);
        if (dependency != null) {
            // lets preserve the scope or not add it if there is no scope
            // on the previously found dependency
            scope = xpath("scope").elementTextContent(dependency);
            detachElement(dependency);
        }
        dependency =  createAndAppendChild(dependencies"dependency", 2);
        addMavenCoordinates(dependencyparser, 3);
        if (Strings.isNotBlank(scope)) {
            createAndAppendChild(dependency"scope", 3, scope);
        }
    }
    protected void addMavenCoordinates(Element ownerParser parserint indent) {
        String group = groupId(parser);
        createAndAppendChild(owner"groupId"indentgroup);
        createAndAppendChild(owner"artifactId"indentparser.getArtifact());
        createAndAppendChild(owner"version"indentparser.getVersion());
        String type = parser.getType();
        if (type != null && !Objects.equal("jar"type)) {
            createAndAppendChild(owner"type"indenttype);
        }
        String classifier = parser.getClassifier();
        if (Strings.isNotBlank(classifier)) {
            createAndAppendChild(owner"classifier"indentclassifier);
        }
    }
    public static String groupId(Parser parser) {
        String group = parser.getGroup();
        int idx = group.indexOf(':');
        if (idx > 0) {
            group = group.substring(idx + 1);
        }
        return group;
    }
    protected Element getOrCreatePlugin(Element pluginsString artifactIdString versionthrows XPathExpressionException {
        Element plugin = getPlugin(pluginsartifactId);
        if (plugin == null) {
            plugin = createAndAppendChild(plugins"plugin", 5);
            createAndAppendChild(plugin"artifactId", 6, artifactId);
            createAndAppendChild(plugin"version", 6, version);
        }
        return plugin;
    }
    protected Element getPlugin(Element pluginsString artifactIdthrows XPathExpressionException {
        return xpath("plugin[artifactId = '" + artifactId + "']").element(plugins);
    }

    
Removes the child matching the given name and recreates it and adds it
    private Element recreateChild(Element ownerString elementNameint indentthrows XPathExpressionException {
        return recreateChild(ownerelementNameelementNameindent);
    }

    
Removes the child matching the given name and recreates it and adds it
    private Element recreateChild(Element ownerString xpathString elementNameint indentthrows XPathExpressionException {
        Element answer = xpath(xpath).element(owner);
        if (answer != null) {
            detachElement(answer);
        }
        return createAndAppendChild(ownerelementNameindent);
    }
    protected void detachElement(Element answer) {
        DomHelper.removePreviousSiblingText(answer);
        DomHelper.removeNextSiblingText(answer);
        DomHelper.detach(answer);
    }

    
Gets the first child with the given element name or adds a new one if its missing
    protected Element getOrCreateChild(Element ownerString elementNameint indentboolean forceWhitespacethrows XPathExpressionException {
        Element answer = xpath(elementName).element(owner);
        if (answer == null) {
            answer = createAndAppendChild(ownerelementNameindentforceWhitespace);
        }
        return answer;
    }
    protected Element getOrCreateChild(Element ownerString elementNameint indentthrows XPathExpressionException {
        return getOrCreateChild(ownerelementNameindentfalse);
    }
    public static Element createAndAppendChild(Element ownerString elementNameint indentString text) {
        Element answer = createAndAppendChild(ownerelementNameindent);
        if (Strings.isNotBlank(text)) {
            appendText(answertext);
        }
        return answer;
    }
    public static Element createAndAppendChild(Element ownerString elementNameint indent) {
        return createAndAppendChild(ownerelementNameindentfalse);
    }
    public static Element createAndAppendChild(Element ownerString elementNameint indentboolean forceWhitespace) {
        Document ownerDocument = owner.getOwnerDocument();
        StringBuilder buffer = new StringBuilder("\n");
        for (int i = 0; i < indenti++) {
            buffer.append();
        }
        String whitespace = buffer.toString();
        // lets only add whitespace if we don't already have whitespace or we want to force it
        if (forceWhitespace || !isLastNodeTextWithNewline(owner)) {
            appendText(ownerwhitespace);
        }
        Element newElement = ownerDocument.createElement(elementName);
        owner.appendChild(newElement);
        appendText(ownerwhitespace);
        return newElement;
    }
    private static boolean isLastNodeTextWithNewline(Element owner) {
        Node lastChild = owner.getLastChild();
        if (lastChild instanceof Text) {
            Text textNode = (TextlastChild;
            String wholeText = textNode.getWholeText();
            return wholeText.contains("\n");
        }
        return false;
    }
    public static Text appendText(Element ownerString text) {
        Document ownerDocument = owner.getOwnerDocument();
        Text textNode = ownerDocument.createTextNode(text);
        owner.appendChild(textNode);
        return textNode;
    }
    protected XPathFacade xpath(String expressionthrows XPathExpressionException {
        return .xpath(expression);
    }
New to GrepCode? Check out our FAQ X