rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 27 May 2014 12:25:41 +0200
branchclosure
changeset 1604 7665471a56c1
parent 1513 ba912ef24b27
child 1762 293838e72201
permissions -rw-r--r--
The static calculator demo needs to reference just a single application .js file from its main HTML page. The rest is loaded based on classpath attribute.
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@109
    23
import java.io.FileWriter;
jaroslav@305
    24
import java.io.IOException;
jaroslav@109
    25
import java.net.MalformedURLException;
jaroslav@109
    26
import java.net.URL;
jaroslav@109
    27
import java.net.URLClassLoader;
jaroslav@109
    28
import java.util.ArrayList;
jaroslav@109
    29
import java.util.Collection;
jaroslav@109
    30
import java.util.List;
jaroslav@109
    31
import org.apache.maven.artifact.Artifact;
jaroslav@109
    32
import org.apache.maven.plugin.MojoExecutionException;
jaroslav@109
    33
import org.apache.maven.plugins.annotations.LifecyclePhase;
jaroslav@109
    34
import org.apache.maven.plugins.annotations.Mojo;
jaroslav@109
    35
import org.apache.maven.plugins.annotations.Parameter;
jaroslav@1245
    36
import org.apache.maven.plugins.annotations.ResolutionScope;
jaroslav@109
    37
import org.apache.maven.project.MavenProject;
jaroslav@305
    38
import org.apidesign.vm4brwsr.Bck2Brwsr;
lubomir@849
    39
import org.apidesign.vm4brwsr.ObfuscationLevel;
jaroslav@109
    40
jaroslav@109
    41
/** Compiles classes into JavaScript. */
jaroslav@1245
    42
@Mojo(name="j2js", 
jaroslav@1245
    43
    requiresDependencyResolution = ResolutionScope.COMPILE,
jaroslav@1245
    44
    defaultPhase=LifecyclePhase.PROCESS_CLASSES
jaroslav@1245
    45
)
jaroslav@740
    46
public class Java2JavaScript extends AbstractMojo {
jaroslav@740
    47
    public Java2JavaScript() {
jaroslav@109
    48
    }
jaroslav@109
    49
    /** Root of the class files */
jaroslav@109
    50
    @Parameter(defaultValue="${project.build.directory}/classes")
jaroslav@109
    51
    private File classes;
jaroslav@847
    52
    /** JavaScript file to generate */
jaroslav@1371
    53
    @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
jaroslav@109
    54
    private File javascript;
lubomir@849
    55
jaroslav@847
    56
    /** Additional classes that should be pre-compiled into the javascript 
jaroslav@847
    57
     * file. By default compiles all classes found under <code>classes</code>
jaroslav@847
    58
     * directory and their transitive closure.
jaroslav@847
    59
     */
jaroslav@847
    60
    @Parameter
jaroslav@847
    61
    private List<String> compileclasses;
jaroslav@847
    62
    
jaroslav@109
    63
    @Parameter(defaultValue="${project}")
jaroslav@109
    64
    private MavenProject prj;
lubomir@849
    65
lubomir@860
    66
    /**
lubomir@860
    67
     * The obfuscation level for the generated JavaScript file.
lubomir@860
    68
     *
lubomir@860
    69
     * @since 0.5
lubomir@860
    70
     */
lubomir@849
    71
    @Parameter(defaultValue="NONE")
lubomir@849
    72
    private ObfuscationLevel obfuscation;
jaroslav@1365
    73
    
jaroslav@1365
    74
    /** Should classes from rt.jar be included? */
jaroslav@1365
    75
    @Parameter(defaultValue = "false")
jaroslav@1365
    76
    private boolean ignoreBootClassPath;
jaroslav@109
    77
lubomir@1104
    78
    /**
jaroslav@1503
    79
     * Indicates whether to create an extension library 
jaroslav@1503
    80
     * module instead of a standalone JavaScript VM.
lubomir@1104
    81
     *
jaroslav@1503
    82
     * @since 0.9
lubomir@1104
    83
     */
lubomir@1104
    84
    @Parameter(defaultValue="false")
jaroslav@1503
    85
    private boolean library;
lubomir@1104
    86
jaroslav@109
    87
    @Override
jaroslav@109
    88
    public void execute() throws MojoExecutionException {
jaroslav@109
    89
        if (!classes.isDirectory()) {
jaroslav@109
    90
            throw new MojoExecutionException("Can't find " + classes);
jaroslav@109
    91
        }
jaroslav@1366
    92
        if (javascript == null) {
jaroslav@1366
    93
            throw new MojoExecutionException("Need to define 'javascript' attribute with a path to file to generate");
jaroslav@1366
    94
        }
jaroslav@109
    95
jaroslav@109
    96
        List<String> arr = new ArrayList<String>();
jaroslav@134
    97
        long newest = collectAllClasses("", classes, arr);
jaroslav@109
    98
        
jaroslav@847
    99
        if (compileclasses != null) {
jaroslav@847
   100
            arr.retainAll(compileclasses);
jaroslav@847
   101
            arr.addAll(compileclasses);
jaroslav@847
   102
        }
jaroslav@847
   103
        
jaroslav@134
   104
        if (javascript.lastModified() > newest) {
jaroslav@134
   105
            return;
jaroslav@134
   106
        }
jaroslav@109
   107
jaroslav@109
   108
        try {
jaroslav@1245
   109
            URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
jaroslav@109
   110
            FileWriter w = new FileWriter(javascript);
jaroslav@1604
   111
            Bck2Brwsr c = Bck2Brwsr.newCompiler().
jaroslav@874
   112
                obfuscation(obfuscation).
jaroslav@1365
   113
                resources(url, ignoreBootClassPath).
jaroslav@1604
   114
                addRootClasses(arr.toArray(new String[0]));
jaroslav@1604
   115
            if (library) {
jaroslav@1604
   116
                c = c.library();
jaroslav@1604
   117
            }
jaroslav@1604
   118
            c.generate(w);
jaroslav@109
   119
            w.close();
jaroslav@305
   120
        } catch (IOException ex) {
jaroslav@109
   121
            throw new MojoExecutionException("Can't compile", ex);
jaroslav@109
   122
        }
jaroslav@109
   123
    }
jaroslav@109
   124
jaroslav@134
   125
    private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
jaroslav@109
   126
        File[] files = toCheck.listFiles();
jaroslav@109
   127
        if (files != null) {
jaroslav@134
   128
            long newest = 0L;
jaroslav@109
   129
            for (File f : files) {
jaroslav@134
   130
                long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
jaroslav@134
   131
                if (newest < lastModified) {
jaroslav@134
   132
                    newest = lastModified;
jaroslav@134
   133
                }
jaroslav@109
   134
            }
jaroslav@134
   135
            return newest;
jaroslav@109
   136
        } else if (toCheck.getName().endsWith(".class")) {
jaroslav@847
   137
            final String cls = prefix.substring(0, prefix.length() - 7);
lubomir@988
   138
            if (!cls.endsWith("package-info")) {
lubomir@988
   139
                arr.add(cls);
lubomir@988
   140
            }
jaroslav@134
   141
            return toCheck.lastModified();
jaroslav@134
   142
        } else {
jaroslav@134
   143
            return 0L;
jaroslav@109
   144
        }
jaroslav@109
   145
    }
jaroslav@109
   146
jaroslav@109
   147
    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
jaroslav@109
   148
        List<URL> arr = new ArrayList<URL>();
jaroslav@109
   149
        arr.add(root.toURI().toURL());
jaroslav@109
   150
        for (Artifact a : deps) {
jaroslav@847
   151
            if (a.getFile() != null) {
jaroslav@847
   152
                arr.add(a.getFile().toURI().toURL());
jaroslav@847
   153
            }
jaroslav@109
   154
        }
jaroslav@740
   155
        return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
jaroslav@109
   156
    }
jaroslav@109
   157
}