2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.mojo;
20 import org.apache.maven.plugin.AbstractMojo;
23 import java.io.FileWriter;
24 import java.lang.reflect.Method;
25 import java.net.MalformedURLException;
27 import java.net.URLClassLoader;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.List;
31 import org.apache.maven.artifact.Artifact;
32 import org.apache.maven.plugin.MojoExecutionException;
33 import org.apache.maven.plugins.annotations.LifecyclePhase;
34 import org.apache.maven.plugins.annotations.Mojo;
35 import org.apache.maven.plugins.annotations.Parameter;
36 import org.apache.maven.project.MavenProject;
38 /** Compiles classes into JavaScript. */
39 @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
40 public class Bck2BrswrMojo extends AbstractMojo {
41 public Bck2BrswrMojo() {
43 /** Root of the class files */
44 @Parameter(defaultValue="${project.build.directory}/classes")
46 /** File to generate. Defaults bootjava.js in the first non-empty
47 package under the classes directory */
49 private File javascript;
51 @Parameter(defaultValue="${project}")
52 private MavenProject prj;
57 public void execute() throws MojoExecutionException {
58 if (!classes.isDirectory()) {
59 throw new MojoExecutionException("Can't find " + classes);
62 if (javascript == null) {
63 javascript = new File(findNonEmptyFolder(classes), "bootjava.js");
66 List<String> arr = new ArrayList<String>();
67 long newest = collectAllClasses("", classes, arr);
69 if (javascript.lastModified() > newest) {
74 URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
76 Class<?> c = Class.forName("org.apidesign.vm4brwsr.GenJS");
77 Method m = c.getDeclaredMethod("compile", ClassLoader.class, Appendable.class, String[].class);
78 m.setAccessible(true);
79 FileWriter w = new FileWriter(javascript);
80 m.invoke(null, url, w, arr.toArray(new String[0]));
82 } catch (Exception ex) {
83 throw new MojoExecutionException("Can't compile", ex);
87 private static File findNonEmptyFolder(File dir) throws MojoExecutionException {
88 if (!dir.isDirectory()) {
89 throw new MojoExecutionException("Not a directory " + dir);
91 File[] arr = dir.listFiles();
92 if (arr.length == 1 && arr[0].isDirectory()) {
93 return findNonEmptyFolder(arr[0]);
98 private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
99 File[] files = toCheck.listFiles();
102 for (File f : files) {
103 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
104 if (newest < lastModified) {
105 newest = lastModified;
109 } else if (toCheck.getName().endsWith(".class")) {
110 arr.add(prefix.substring(0, prefix.length() - 7));
111 return toCheck.lastModified();
117 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
118 List<URL> arr = new ArrayList<URL>();
119 arr.add(root.toURI().toURL());
120 for (Artifact a : deps) {
121 arr.add(a.getFile().toURI().toURL());
123 return new URLClassLoader(arr.toArray(new URL[0]), Bck2BrswrMojo.class.getClassLoader());