Search in sources :

Example 1 with MojoLogChute

use of sh.tak.appbundler.logging.MojoLogChute in project appbundle-maven-plugin by federkasten.

the class CreateApplicationBundleMojo method writeInfoPlist.

/**
 * Writes an Info.plist file describing this bundle.
 *
 * @param infoPlist The file to write Info.plist contents to
 * @param files A list of file names of the jar files to add in $JAVAROOT
 * @throws MojoExecutionException
 */
private void writeInfoPlist(File infoPlist, List<String> files) throws MojoExecutionException {
    Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new MojoLogChute(this));
    Velocity.setProperty("file.resource.loader.path", TARGET_CLASS_ROOT);
    try {
        Velocity.init();
    } catch (Exception ex) {
        throw new MojoExecutionException("Exception occured in initializing velocity", ex);
    }
    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("mainClass", mainClass);
    velocityContext.put("cfBundleExecutable", javaLauncherName);
    velocityContext.put("bundleName", cleanBundleName(bundleName));
    velocityContext.put("workingDirectory", workingDirectory);
    if (embeddJre && jrePath != null) {
        velocityContext.put("jrePath", "JRE");
        velocityContext.put("jreFullPath", "");
    } else if (embeddJre && jreFullPath != null) {
        velocityContext.put("jrePath", "");
        velocityContext.put("jreFullPath", jreFullPath);
    } else {
        velocityContext.put("jrePath", "");
        velocityContext.put("jreFullPath", "");
    }
    if (iconFile == null) {
        velocityContext.put("iconFile", "GenericJavaApp.icns");
    } else {
        File f = searchFile(iconFile, project.getBasedir());
        velocityContext.put("iconFile", (f != null && f.exists() && f.isFile()) ? f.getName() : "GenericJavaApp.icns");
    }
    velocityContext.put("version", version);
    velocityContext.put("jvmVersion", jvmVersion);
    StringBuilder options = new StringBuilder();
    options.append("<array>").append("\n      ");
    for (String jvmOption : defaultJvmOptions) {
        options.append("      ").append("<string>").append(jvmOption).append("</string>").append("\n");
    }
    options.append("      ").append("<string>").append("-Xdock:name=" + bundleName).append("</string>").append("\n");
    if (jvmOptions != null) {
        for (String jvmOption : jvmOptions) {
            options.append("      ").append("<string>").append(jvmOption).append("</string>").append("\n");
        }
    }
    options.append("    ").append("</array>");
    velocityContext.put("jvmOptions", options);
    StringBuilder jarFiles = new StringBuilder();
    jarFiles.append("<array>").append("\n");
    for (String file : files) {
        jarFiles.append("      ").append("<string>").append(file).append("</string>").append("\n");
    }
    if (additionalClasspath != null) {
        for (String pathElement : additionalClasspath) {
            jarFiles.append("      ").append("<string>").append(pathElement).append("</string>");
        }
    }
    jarFiles.append("    ").append("</array>");
    velocityContext.put("classpath", jarFiles.toString());
    try {
        File sourceInfoPlist = new File(TARGET_CLASS_ROOT, dictionaryFile);
        if (sourceInfoPlist.exists() && sourceInfoPlist.isFile()) {
            String encoding = detectEncoding(sourceInfoPlist);
            getLog().debug("Detected encoding " + encoding + " for dictionary file " + dictionaryFile);
            Writer writer = new OutputStreamWriter(new FileOutputStream(infoPlist), encoding);
            Template template = Velocity.getTemplate(dictionaryFile, encoding);
            template.merge(velocityContext, writer);
            writer.close();
        } else {
            Writer writer = new OutputStreamWriter(new FileOutputStream(infoPlist), "UTF-8");
            velocity.getEngine().mergeTemplate(dictionaryFile, "UTF-8", velocityContext, writer);
            writer.close();
        }
    } catch (IOException ex) {
        throw new MojoExecutionException("Could not write Info.plist to file " + infoPlist, ex);
    } catch (ParseErrorException ex) {
        throw new MojoExecutionException("Error parsing " + dictionaryFile, ex);
    } catch (ResourceNotFoundException ex) {
        throw new MojoExecutionException("Could not find resource for template " + dictionaryFile, ex);
    } catch (MethodInvocationException ex) {
        throw new MojoExecutionException("MethodInvocationException occured merging Info.plist template " + dictionaryFile, ex);
    } catch (Exception ex) {
        throw new MojoExecutionException("Exception occured merging Info.plist template " + dictionaryFile, ex);
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityContext(org.apache.velocity.VelocityContext) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) CommandLineException(org.codehaus.plexus.util.cli.CommandLineException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ParseErrorException(org.apache.velocity.exception.ParseErrorException) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileNotFoundException(java.io.FileNotFoundException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) Template(org.apache.velocity.Template) FileOutputStream(java.io.FileOutputStream) MojoLogChute(sh.tak.appbundler.logging.MojoLogChute) OutputStreamWriter(java.io.OutputStreamWriter) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) File(java.io.File) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Aggregations

File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)1 ParseErrorException (org.apache.velocity.exception.ParseErrorException)1 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)1 CommandLineException (org.codehaus.plexus.util.cli.CommandLineException)1 MojoLogChute (sh.tak.appbundler.logging.MojoLogChute)1