jaroslav@111: /** jaroslav@111: * Back 2 Browser Bytecode Translator jaroslav@111: * Copyright (C) 2012 Jaroslav Tulach jaroslav@111: * jaroslav@111: * This program is free software: you can redistribute it and/or modify jaroslav@111: * it under the terms of the GNU General Public License as published by jaroslav@111: * the Free Software Foundation, version 2 of the License. jaroslav@111: * jaroslav@111: * This program is distributed in the hope that it will be useful, jaroslav@111: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@111: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@111: * GNU General Public License for more details. jaroslav@111: * jaroslav@111: * You should have received a copy of the GNU General Public License jaroslav@111: * along with this program. Look for COPYING file in the top folder. jaroslav@111: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@111: */ jaroslav@109: package org.apidesign.bck2brwsr.mojo; jaroslav@109: jaroslav@109: import org.apache.maven.plugin.AbstractMojo; jaroslav@109: jaroslav@109: import java.io.File; jaroslav@109: import java.io.FileWriter; jaroslav@305: import java.io.IOException; jaroslav@109: import java.net.MalformedURLException; jaroslav@109: import java.net.URL; jaroslav@109: import java.net.URLClassLoader; jaroslav@109: import java.util.ArrayList; jaroslav@109: import java.util.Collection; jaroslav@109: import java.util.List; jaroslav@109: import org.apache.maven.artifact.Artifact; jaroslav@109: import org.apache.maven.plugin.MojoExecutionException; jaroslav@109: import org.apache.maven.plugins.annotations.LifecyclePhase; jaroslav@109: import org.apache.maven.plugins.annotations.Mojo; jaroslav@109: import org.apache.maven.plugins.annotations.Parameter; jaroslav@1245: import org.apache.maven.plugins.annotations.ResolutionScope; jaroslav@109: import org.apache.maven.project.MavenProject; jaroslav@305: import org.apidesign.vm4brwsr.Bck2Brwsr; lubomir@849: import org.apidesign.vm4brwsr.ObfuscationLevel; jaroslav@109: jaroslav@109: /** Compiles classes into JavaScript. */ jaroslav@1245: @Mojo(name="j2js", jaroslav@1245: requiresDependencyResolution = ResolutionScope.COMPILE, jaroslav@1245: defaultPhase=LifecyclePhase.PROCESS_CLASSES jaroslav@1245: ) jaroslav@740: public class Java2JavaScript extends AbstractMojo { jaroslav@740: public Java2JavaScript() { jaroslav@109: } jaroslav@109: /** Root of the class files */ jaroslav@109: @Parameter(defaultValue="${project.build.directory}/classes") jaroslav@109: private File classes; jaroslav@847: /** JavaScript file to generate */ jaroslav@1371: @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js") jaroslav@109: private File javascript; lubomir@849: jaroslav@847: /** Additional classes that should be pre-compiled into the javascript jaroslav@847: * file. By default compiles all classes found under classes jaroslav@847: * directory and their transitive closure. jaroslav@847: */ jaroslav@847: @Parameter jaroslav@847: private List compileclasses; jaroslav@847: jaroslav@109: @Parameter(defaultValue="${project}") jaroslav@109: private MavenProject prj; lubomir@849: lubomir@860: /** lubomir@860: * The obfuscation level for the generated JavaScript file. lubomir@860: * lubomir@860: * @since 0.5 lubomir@860: */ lubomir@849: @Parameter(defaultValue="NONE") lubomir@849: private ObfuscationLevel obfuscation; jaroslav@1365: jaroslav@1365: /** Should classes from rt.jar be included? */ jaroslav@1365: @Parameter(defaultValue = "false") jaroslav@1365: private boolean ignoreBootClassPath; jaroslav@109: lubomir@1104: /** jaroslav@1503: * Indicates whether to create an extension library jaroslav@1503: * module instead of a standalone JavaScript VM. lubomir@1104: * jaroslav@1503: * @since 0.9 lubomir@1104: */ lubomir@1104: @Parameter(defaultValue="false") jaroslav@1503: private boolean library; lubomir@1104: jaroslav@109: @Override jaroslav@109: public void execute() throws MojoExecutionException { jaroslav@109: if (!classes.isDirectory()) { jaroslav@109: throw new MojoExecutionException("Can't find " + classes); jaroslav@109: } jaroslav@1366: if (javascript == null) { jaroslav@1366: throw new MojoExecutionException("Need to define 'javascript' attribute with a path to file to generate"); jaroslav@1366: } jaroslav@109: jaroslav@109: List arr = new ArrayList(); jaroslav@134: long newest = collectAllClasses("", classes, arr); jaroslav@109: jaroslav@847: if (compileclasses != null) { jaroslav@847: arr.retainAll(compileclasses); jaroslav@847: arr.addAll(compileclasses); jaroslav@847: } jaroslav@847: jaroslav@134: if (javascript.lastModified() > newest) { jaroslav@134: return; jaroslav@134: } jaroslav@109: jaroslav@109: try { jaroslav@1245: URLClassLoader url = buildClassLoader(classes, prj.getArtifacts()); jaroslav@109: FileWriter w = new FileWriter(javascript); jaroslav@1604: Bck2Brwsr c = Bck2Brwsr.newCompiler(). jaroslav@874: obfuscation(obfuscation). jaroslav@1365: resources(url, ignoreBootClassPath). jaroslav@1604: addRootClasses(arr.toArray(new String[0])); jaroslav@1604: if (library) { jaroslav@1604: c = c.library(); jaroslav@1604: } jaroslav@1604: c.generate(w); jaroslav@109: w.close(); jaroslav@305: } catch (IOException ex) { jaroslav@109: throw new MojoExecutionException("Can't compile", ex); jaroslav@109: } jaroslav@109: } jaroslav@109: jaroslav@134: private static long collectAllClasses(String prefix, File toCheck, List arr) { jaroslav@109: File[] files = toCheck.listFiles(); jaroslav@109: if (files != null) { jaroslav@134: long newest = 0L; jaroslav@109: for (File f : files) { jaroslav@134: long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr); jaroslav@134: if (newest < lastModified) { jaroslav@134: newest = lastModified; jaroslav@134: } jaroslav@109: } jaroslav@134: return newest; jaroslav@109: } else if (toCheck.getName().endsWith(".class")) { jaroslav@847: final String cls = prefix.substring(0, prefix.length() - 7); lubomir@988: if (!cls.endsWith("package-info")) { lubomir@988: arr.add(cls); lubomir@988: } jaroslav@134: return toCheck.lastModified(); jaroslav@134: } else { jaroslav@134: return 0L; jaroslav@109: } jaroslav@109: } jaroslav@109: jaroslav@109: private static URLClassLoader buildClassLoader(File root, Collection deps) throws MalformedURLException { jaroslav@109: List arr = new ArrayList(); jaroslav@109: arr.add(root.toURI().toURL()); jaroslav@109: for (Artifact a : deps) { jaroslav@847: if (a.getFile() != null) { jaroslav@847: arr.add(a.getFile().toURI().toURL()); jaroslav@847: } jaroslav@109: } jaroslav@740: return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader()); jaroslav@109: } jaroslav@109: }