Package de.timefinder.core.util

Source Code of de.timefinder.core.util.Packager

/*
* This file is part of the TimeFinder project.
*  Visit http://www.timefinder.de for more information.
*  Copyright (c) 2009 the original author or authors.
*
*  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 de.timefinder.core.util;

import de.timefinder.data.util.Helper;
import javolution.util.FastMap;
import javolution.util.FastSet;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.io.FileInputStream;
import java.util.Collection;
import java.util.Set;

/**
* Utility class to prevent us from strange bugs of the maven-assembly-plugin
* like this:
* http://forum.springsource.org/showthread.php?t=48787&page=2
* Instead of creating one fat jar simply copy all jars to a folder.
* <p/>
* But after this I found: http://maven.apache.org/plugins/maven-dependency-plugin/plugin-info.html
* See run.sh of the detailed usage.
*
* @author Peter Karich
* @deprecated you can simply use the zip from mvn install webstart:jnlp
*/
public class Packager {

    private FastMap<String, String> properties = new FastMap<String, String>();
    private String mavenRepoPath = "C:\\Users\\btn417\\.m2\\repository";

    public static void main(String[] args) throws Exception {
        new Packager().start();
    }

    public void start() throws Exception {
        properties.put("javafx_home", "C:\\Program Files\\NetBeans 6.5\\javafx2\\javafx-sdk");

        Set<File> files = new FastSet<File>();
        files.add(new File(mavenRepoPath + "\\de\\timefinder\\timefinder-core\\2009-v1\\timefinder-core-2009-v1.jar"));

        Document doc = doc = Helper.parse(new FileInputStream("../pom.xml"));
        fillProperties(doc);
        fillDependencies(files, doc);

        doc = Helper.parse(new FileInputStream("pom.xml"));
        fillProperties(doc);
        fillDependencies(files, doc);

        String destinationDir = ".\\target\\all";
        new File(destinationDir).mkdir();

        // print classpath
        StringBuilder clpa = new StringBuilder();
        for (File f : files) {
            if (!f.exists()) {
                System.out.println("File does not exist:" + f);
            } else {
                clpa.append(File.pathSeparatorChar);
                clpa.append(f.getName());
            }
        }
        System.out.println(clpa.toString());

        // create copy script
        for (File f : files) {
            if (!f.exists()) {
                System.out.println("File does not exist:" + f);
            } else {
                System.out.println("cp " + f + " " + destinationDir);
            }
        }

        // create sign
        for (File f : files) {
            if (f.exists()) {
                System.out.println("jarsigner -keystore timefinderstore -verbose -keypass KEYPW -storepass STOREPW " + f + " timefinder");
            }
        }

        // jnlp copy script
        for (File f : files) {
            if (f.exists()) {
                System.out.println("<jar href=\"" + f + "\" />");
            }
        }
    }

    public void fillDependencies(Collection<File> files, Document doc) {
        Element rootElement = Helper.getFirstElement(doc.getElementsByTagName("dependencies"));
        NodeList nodeList = rootElement.getElementsByTagName("dependency");
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Element element = (Element) n;
            boolean include = true;
            StringBuilder path = new StringBuilder(mavenRepoPath);
            StringBuilder jarName = new StringBuilder();

            String groupId = Helper.getFirstElement(element.getElementsByTagName("groupId")).getTextContent();
            for (String str : groupId.split("\\.")) {
                path.append('/');
                path.append(str);
            }
            String artifactId = Helper.getFirstElement(element.getElementsByTagName("artifactId")).getTextContent();
            path.append('/');
            path.append(artifactId);
            jarName.append(artifactId);
            jarName.append('-');

            // if version is not explicitely set => NPE!
            Element versionElement = Helper.getFirstElement(element.getElementsByTagName("version"));
            String versionString = replaceProperty(versionElement.getTextContent());

            path.append('/');
            path.append(versionString);

            jarName.append(versionString);
            jarName.append(".jar");

            Element scopeElement = Helper.getFirstElement(element.getElementsByTagName("scope"));
            if (scopeElement != null) {
                if (!"compile".equals(scopeElement.getTextContent()) && !"system".equals(scopeElement.getTextContent())) {
                    include = false;
                }

                if ("system".equals(scopeElement.getTextContent())) {
                    Element sysPath = Helper.getFirstElement(element.getElementsByTagName("systemPath"));
                    if (sysPath != null) {
                        path.setLength(0);
                        jarName.setLength(0);
                        path.append(replaceProperty(sysPath.getTextContent()));
                    }
                }
            }

            if (include) {
                files.add(new File(path.toString() + '/' + jarName.toString()));
            }
        }
    }

    public void fillProperties(Document doc) {
        Element rootElement = Helper.getFirstElement(doc.getElementsByTagName("properties"));
        if (rootElement == null) {
            return;
        }

        NodeList nodeList = rootElement.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node n = nodeList.item(i);
            if (n.getNodeType() != Node.ELEMENT_NODE) {
                continue;
            }
            Element element = (Element) n;
            properties.put(element.getNodeName(), element.getTextContent());
        }
    }

    public String replaceProperty(String str) {
        int index = 0;
        StringBuilder result = new StringBuilder();
        while (index < str.length()) {
            int start = str.indexOf("${", index);
            if (start < 0) {
                break;
            }
            start += index;

            result.append(str.substring(index, start));
            int end = str.indexOf("}", start) + start;
            result.append(properties.get(str.substring(start + 2, end)));
            index = end + 1;
        }
        result.append(str.substring(index));
        return result.toString();
    }
}
TOP

Related Classes of de.timefinder.core.util.Packager

TOP
Copyright © 2018 www.massapi.com. 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.