rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 09 Jan 2015 20:46:35 +0100
changeset 1762 293838e72201
parent 1604 7665471a56c1
child 1763 647282885e6f
permissions -rw-r--r--
Always write down the JavaScript file in UTF-8 encoding
jaroslav@111
     1
/**
jaroslav@111
     2
 * Back 2 Browser Bytecode Translator
jaroslav@111
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@111
     4
 *
jaroslav@111
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@111
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@111
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@111
     8
 *
jaroslav@111
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@111
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@111
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@111
    12
 * GNU General Public License for more details.
jaroslav@111
    13
 *
jaroslav@111
    14
 * You should have received a copy of the GNU General Public License
jaroslav@111
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@111
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@111
    17
 */
jaroslav@109
    18
package org.apidesign.bck2brwsr.mojo;
jaroslav@109
    19
jaroslav@109
    20
import org.apache.maven.plugin.AbstractMojo;
jaroslav@109
    21
jaroslav@109
    22
import java.io.File;
jaroslav@1762
    23
import java.io.FileOutputStream;
jaroslav@305
    24
import java.io.IOException;
jaroslav@1762
    25
import java.io.OutputStreamWriter;
jaroslav@1762
    26
import java.io.Writer;
jaroslav@109
    27
import java.net.MalformedURLException;
jaroslav@109
    28
import java.net.URL;
jaroslav@109
    29
import java.net.URLClassLoader;
jaroslav@109
    30
import java.util.ArrayList;
jaroslav@109
    31
import java.util.Collection;
jaroslav@109
    32
import java.util.List;
jaroslav@109
    33
import org.apache.maven.artifact.Artifact;
jaroslav@109
    34
import org.apache.maven.plugin.MojoExecutionException;
jaroslav@109
    35
import org.apache.maven.plugins.annotations.LifecyclePhase;
jaroslav@109
    36
import org.apache.maven.plugins.annotations.Mojo;
jaroslav@109
    37
import org.apache.maven.plugins.annotations.Parameter;
jaroslav@1245
    38
import org.apache.maven.plugins.annotations.ResolutionScope;
jaroslav@109
    39
import org.apache.maven.project.MavenProject;
jaroslav@305
    40
import org.apidesign.vm4brwsr.Bck2Brwsr;
lubomir@849
    41
import org.apidesign.vm4brwsr.ObfuscationLevel;
jaroslav@109
    42
jaroslav@109
    43
/** Compiles classes into JavaScript. */
jaroslav@1245
    44
@Mojo(name="j2js", 
jaroslav@1245
    45
    requiresDependencyResolution = ResolutionScope.COMPILE,
jaroslav@1245
    46
    defaultPhase=LifecyclePhase.PROCESS_CLASSES
jaroslav@1245
    47
)
jaroslav@740
    48
public class Java2JavaScript extends AbstractMojo {
jaroslav@740
    49
    public Java2JavaScript() {
jaroslav@109
    50
    }
jaroslav@109
    51
    /** Root of the class files */
jaroslav@109
    52
    @Parameter(defaultValue="${project.build.directory}/classes")
jaroslav@109
    53
    private File classes;
jaroslav@847
    54
    /** JavaScript file to generate */
jaroslav@1371
    55
    @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
jaroslav@109
    56
    private File javascript;
lubomir@849
    57
jaroslav@847
    58
    /** Additional classes that should be pre-compiled into the javascript 
jaroslav@847
    59
     * file. By default compiles all classes found under <code>classes</code>
jaroslav@847
    60
     * directory and their transitive closure.
jaroslav@847
    61
     */
jaroslav@847
    62
    @Parameter
jaroslav@847
    63
    private List<String> compileclasses;
jaroslav@847
    64
    
jaroslav@109
    65
    @Parameter(defaultValue="${project}")
jaroslav@109
    66
    private MavenProject prj;
lubomir@849
    67
lubomir@860
    68
    /**
lubomir@860
    69
     * The obfuscation level for the generated JavaScript file.
lubomir@860
    70
     *
lubomir@860
    71
     * @since 0.5
lubomir@860
    72
     */
lubomir@849
    73
    @Parameter(defaultValue="NONE")
lubomir@849
    74
    private ObfuscationLevel obfuscation;
jaroslav@1365
    75
    
jaroslav@1365
    76
    /** Should classes from rt.jar be included? */
jaroslav@1365
    77
    @Parameter(defaultValue = "false")
jaroslav@1365
    78
    private boolean ignoreBootClassPath;
jaroslav@109
    79
lubomir@1104
    80
    /**
jaroslav@1503
    81
     * Indicates whether to create an extension library 
jaroslav@1503
    82
     * module instead of a standalone JavaScript VM.
lubomir@1104
    83
     *
jaroslav@1503
    84
     * @since 0.9
lubomir@1104
    85
     */
lubomir@1104
    86
    @Parameter(defaultValue="false")
jaroslav@1503
    87
    private boolean library;
lubomir@1104
    88
jaroslav@109
    89
    @Override
jaroslav@109
    90
    public void execute() throws MojoExecutionException {
jaroslav@109
    91
        if (!classes.isDirectory()) {
jaroslav@109
    92
            throw new MojoExecutionException("Can't find " + classes);
jaroslav@109
    93
        }
jaroslav@1366
    94
        if (javascript == null) {
jaroslav@1366
    95
            throw new MojoExecutionException("Need to define 'javascript' attribute with a path to file to generate");
jaroslav@1366
    96
        }
jaroslav@109
    97
jaroslav@109
    98
        List<String> arr = new ArrayList<String>();
jaroslav@134
    99
        long newest = collectAllClasses("", classes, arr);
jaroslav@109
   100
        
jaroslav@847
   101
        if (compileclasses != null) {
jaroslav@847
   102
            arr.retainAll(compileclasses);
jaroslav@847
   103
            arr.addAll(compileclasses);
jaroslav@847
   104
        }
jaroslav@847
   105
        
jaroslav@134
   106
        if (javascript.lastModified() > newest) {
jaroslav@134
   107
            return;
jaroslav@134
   108
        }
jaroslav@109
   109
jaroslav@109
   110
        try {
jaroslav@1245
   111
            URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
jaroslav@1762
   112
            Writer w = new OutputStreamWriter(new FileOutputStream(javascript), "UTF-8");
jaroslav@1604
   113
            Bck2Brwsr c = Bck2Brwsr.newCompiler().
jaroslav@874
   114
                obfuscation(obfuscation).
jaroslav@1365
   115
                resources(url, ignoreBootClassPath).
jaroslav@1604
   116
                addRootClasses(arr.toArray(new String[0]));
jaroslav@1604
   117
            if (library) {
jaroslav@1604
   118
                c = c.library();
jaroslav@1604
   119
            }
jaroslav@1604
   120
            c.generate(w);
jaroslav@109
   121
            w.close();
jaroslav@305
   122
        } catch (IOException ex) {
jaroslav@109
   123
            throw new MojoExecutionException("Can't compile", ex);
jaroslav@109
   124
        }
jaroslav@109
   125
    }
jaroslav@109
   126
jaroslav@134
   127
    private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
jaroslav@109
   128
        File[] files = toCheck.listFiles();
jaroslav@109
   129
        if (files != null) {
jaroslav@134
   130
            long newest = 0L;
jaroslav@109
   131
            for (File f : files) {
jaroslav@134
   132
                long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
jaroslav@134
   133
                if (newest < lastModified) {
jaroslav@134
   134
                    newest = lastModified;
jaroslav@134
   135
                }
jaroslav@109
   136
            }
jaroslav@134
   137
            return newest;
jaroslav@109
   138
        } else if (toCheck.getName().endsWith(".class")) {
jaroslav@847
   139
            final String cls = prefix.substring(0, prefix.length() - 7);
lubomir@988
   140
            if (!cls.endsWith("package-info")) {
lubomir@988
   141
                arr.add(cls);
lubomir@988
   142
            }
jaroslav@134
   143
            return toCheck.lastModified();
jaroslav@134
   144
        } else {
jaroslav@134
   145
            return 0L;
jaroslav@109
   146
        }
jaroslav@109
   147
    }
jaroslav@109
   148
jaroslav@109
   149
    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
jaroslav@109
   150
        List<URL> arr = new ArrayList<URL>();
jaroslav@109
   151
        arr.add(root.toURI().toURL());
jaroslav@109
   152
        for (Artifact a : deps) {
jaroslav@847
   153
            if (a.getFile() != null) {
jaroslav@847
   154
                arr.add(a.getFile().toURI().toURL());
jaroslav@847
   155
            }
jaroslav@109
   156
        }
jaroslav@740
   157
        return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
jaroslav@109
   158
    }
jaroslav@109
   159
}