rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 06 May 2013 18:21:02 +0200
branchelements
changeset 1080 8af592baceb7
parent 861 f41a1fef7738
child 988 c2386b2f53d0
child 1245 5f8cc8ad490e
permissions -rw-r--r--
Merge with another head on elements
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@109
    36
import org.apache.maven.project.MavenProject;
jaroslav@305
    37
import org.apidesign.vm4brwsr.Bck2Brwsr;
lubomir@849
    38
import org.apidesign.vm4brwsr.ObfuscationLevel;
jaroslav@109
    39
jaroslav@109
    40
/** Compiles classes into JavaScript. */
jaroslav@109
    41
@Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
jaroslav@740
    42
public class Java2JavaScript extends AbstractMojo {
jaroslav@740
    43
    public Java2JavaScript() {
jaroslav@109
    44
    }
jaroslav@109
    45
    /** Root of the class files */
jaroslav@109
    46
    @Parameter(defaultValue="${project.build.directory}/classes")
jaroslav@109
    47
    private File classes;
jaroslav@847
    48
    /** JavaScript file to generate */
jaroslav@109
    49
    @Parameter
jaroslav@109
    50
    private File javascript;
lubomir@849
    51
jaroslav@847
    52
    /** Additional classes that should be pre-compiled into the javascript 
jaroslav@847
    53
     * file. By default compiles all classes found under <code>classes</code>
jaroslav@847
    54
     * directory and their transitive closure.
jaroslav@847
    55
     */
jaroslav@847
    56
    @Parameter
jaroslav@847
    57
    private List<String> compileclasses;
jaroslav@847
    58
    
jaroslav@109
    59
    @Parameter(defaultValue="${project}")
jaroslav@109
    60
    private MavenProject prj;
lubomir@849
    61
lubomir@860
    62
    /**
lubomir@860
    63
     * The obfuscation level for the generated JavaScript file.
lubomir@860
    64
     *
lubomir@860
    65
     * @since 0.5
lubomir@860
    66
     */
lubomir@849
    67
    @Parameter(defaultValue="NONE")
lubomir@849
    68
    private ObfuscationLevel obfuscation;
jaroslav@109
    69
jaroslav@109
    70
    @Override
jaroslav@109
    71
    public void execute() throws MojoExecutionException {
jaroslav@109
    72
        if (!classes.isDirectory()) {
jaroslav@109
    73
            throw new MojoExecutionException("Can't find " + classes);
jaroslav@109
    74
        }
jaroslav@109
    75
jaroslav@109
    76
        List<String> arr = new ArrayList<String>();
jaroslav@134
    77
        long newest = collectAllClasses("", classes, arr);
jaroslav@109
    78
        
jaroslav@847
    79
        if (compileclasses != null) {
jaroslav@847
    80
            arr.retainAll(compileclasses);
jaroslav@847
    81
            arr.addAll(compileclasses);
jaroslav@847
    82
        }
jaroslav@847
    83
        
jaroslav@134
    84
        if (javascript.lastModified() > newest) {
jaroslav@134
    85
            return;
jaroslav@134
    86
        }
jaroslav@109
    87
jaroslav@109
    88
        try {
jaroslav@109
    89
            URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
jaroslav@109
    90
            FileWriter w = new FileWriter(javascript);
jaroslav@874
    91
            Bck2Brwsr.newCompiler().
jaroslav@874
    92
                obfuscation(obfuscation).
jaroslav@874
    93
                resources(url).
jaroslav@874
    94
                addRootClasses(arr.toArray(new String[0])).
jaroslav@874
    95
                generate(w);
jaroslav@109
    96
            w.close();
jaroslav@305
    97
        } catch (IOException ex) {
jaroslav@109
    98
            throw new MojoExecutionException("Can't compile", ex);
jaroslav@109
    99
        }
jaroslav@109
   100
    }
jaroslav@109
   101
jaroslav@134
   102
    private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
jaroslav@109
   103
        File[] files = toCheck.listFiles();
jaroslav@109
   104
        if (files != null) {
jaroslav@134
   105
            long newest = 0L;
jaroslav@109
   106
            for (File f : files) {
jaroslav@134
   107
                long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
jaroslav@134
   108
                if (newest < lastModified) {
jaroslav@134
   109
                    newest = lastModified;
jaroslav@134
   110
                }
jaroslav@109
   111
            }
jaroslav@134
   112
            return newest;
jaroslav@109
   113
        } else if (toCheck.getName().endsWith(".class")) {
jaroslav@847
   114
            final String cls = prefix.substring(0, prefix.length() - 7);
jaroslav@847
   115
            arr.add(cls);
jaroslav@134
   116
            return toCheck.lastModified();
jaroslav@134
   117
        } else {
jaroslav@134
   118
            return 0L;
jaroslav@109
   119
        }
jaroslav@109
   120
    }
jaroslav@109
   121
jaroslav@109
   122
    private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
jaroslav@109
   123
        List<URL> arr = new ArrayList<URL>();
jaroslav@109
   124
        arr.add(root.toURI().toURL());
jaroslav@109
   125
        for (Artifact a : deps) {
jaroslav@847
   126
            if (a.getFile() != null) {
jaroslav@847
   127
                arr.add(a.getFile().toURI().toURL());
jaroslav@847
   128
            }
jaroslav@109
   129
        }
jaroslav@740
   130
        return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
jaroslav@109
   131
    }
jaroslav@109
   132
}