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.
19 package org.apidesign.bck2brwsr.mojo;
21 import java.io.BufferedReader;
23 import java.io.FileWriter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.net.MalformedURLException;
29 import java.net.URLClassLoader;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.Enumeration;
33 import java.util.HashSet;
34 import java.util.List;
36 import java.util.jar.JarEntry;
37 import java.util.jar.JarFile;
38 import java.util.logging.Level;
39 import java.util.logging.Logger;
40 import org.apache.maven.artifact.Artifact;
41 import org.apache.maven.plugin.AbstractMojo;
42 import org.apache.maven.plugin.MojoExecutionException;
43 import org.apache.maven.plugin.MojoFailureException;
44 import org.apache.maven.plugins.annotations.LifecyclePhase;
45 import org.apache.maven.plugins.annotations.Mojo;
46 import org.apache.maven.plugins.annotations.Parameter;
47 import org.apache.maven.plugins.annotations.ResolutionScope;
48 import org.apache.maven.project.MavenProject;
49 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
50 import org.apidesign.vm4brwsr.Bck2Brwsr;
51 import org.apidesign.vm4brwsr.ObfuscationLevel;
55 * @author Jaroslav Tulach
59 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
60 defaultPhase = LifecyclePhase.PACKAGE
62 public class AheadOfTime extends AbstractMojo {
63 @Parameter(defaultValue = "${project}")
64 private MavenProject prj;
67 * Directory where to generate ahead-of-time JavaScript files for
70 @Parameter(defaultValue = "${project.build.directory}/lib")
73 /** Root JavaScript file to generate */
74 @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
78 * The obfuscation level for the generated JavaScript file.
82 @Parameter(defaultValue = "NONE")
83 private ObfuscationLevel obfuscation;
86 public void execute() throws MojoExecutionException, MojoFailureException {
87 URLClassLoader loader;
89 loader = buildClassLoader(null, prj.getArtifacts());
90 } catch (MalformedURLException ex) {
91 throw new MojoFailureException("Can't initialize classloader");
93 for (Artifact a : prj.getArtifacts()) {
94 if (a.getFile() == null) {
97 String n = a.getFile().getName();
98 if (!n.endsWith(".jar")) {
101 if ("provided".equals(a.getScope())) {
105 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
107 aotLibrary(a, js , loader);
108 } catch (IOException ex) {
109 throw new MojoFailureException("Can't compile" + a.getFile(), ex);
114 FileWriter w = new FileWriter(vm);
115 Bck2Brwsr.newCompiler().
116 obfuscation(obfuscation).
118 resources(new Bck2Brwsr.Resources() {
121 public InputStream get(String resource) throws IOException {
128 } catch (IOException ex) {
129 throw new MojoExecutionException("Can't compile", ex);
133 private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
134 FileWriter w = new FileWriter(js);
135 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile());
137 obfuscation(obfuscation).
142 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
143 List<URL> arr = new ArrayList<URL>();
145 arr.add(root.toURI().toURL());
147 for (Artifact a : deps) {
148 if (a.getFile() != null) {
149 arr.add(a.getFile().toURI().toURL());
152 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());