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;
22 import java.io.FileWriter;
23 import java.io.IOException;
24 import java.io.InputStream;
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.AbstractMojo;
33 import org.apache.maven.plugin.MojoExecutionException;
34 import org.apache.maven.plugin.MojoFailureException;
35 import org.apache.maven.plugins.annotations.LifecyclePhase;
36 import org.apache.maven.plugins.annotations.Mojo;
37 import org.apache.maven.plugins.annotations.Parameter;
38 import org.apache.maven.plugins.annotations.ResolutionScope;
39 import org.apache.maven.project.MavenProject;
40 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
41 import org.apidesign.vm4brwsr.Bck2Brwsr;
42 import org.apidesign.vm4brwsr.ObfuscationLevel;
46 * @author Jaroslav Tulach
50 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
51 defaultPhase = LifecyclePhase.PACKAGE
53 public class AheadOfTime extends AbstractMojo {
54 @Parameter(defaultValue = "${project}")
55 private MavenProject prj;
57 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
60 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.js")
61 private File mainJavaScript;
64 private String[] exports;
67 * Directory where to generate ahead-of-time JavaScript files for
70 @Parameter(defaultValue = "lib")
71 private String classPathPrefix;
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(mainJar, 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())) {
104 File aot = new File(prj.getBuild().getDirectory(), classPathPrefix);
106 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
108 aotLibrary(a, js , loader);
109 } catch (IOException ex) {
110 throw new MojoFailureException("Can't compile" + a.getFile(), ex);
115 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar);
116 if (exports != null) {
117 for (String e : exports) {
118 c = c.addExported(e.replace('.', '/'));
121 FileWriter w = new FileWriter(mainJavaScript);
123 obfuscation(obfuscation).
127 } catch (IOException ex) {
128 throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
132 FileWriter w = new FileWriter(vm);
133 Bck2Brwsr.newCompiler().
134 obfuscation(obfuscation).
136 resources(new Bck2Brwsr.Resources() {
139 public InputStream get(String resource) throws IOException {
146 } catch (IOException ex) {
147 throw new MojoExecutionException("Can't compile", ex);
151 private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
152 FileWriter w = new FileWriter(js);
153 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile());
155 obfuscation(obfuscation).
160 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
161 List<URL> arr = new ArrayList<URL>();
163 arr.add(root.toURI().toURL());
165 for (Artifact a : deps) {
166 if (a.getFile() != null) {
167 arr.add(a.getFile().toURI().toURL());
170 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());