2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 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;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.net.MalformedURLException;
28 import java.net.URLClassLoader;
29 import java.nio.file.Files;
30 import java.util.ArrayList;
31 import java.util.Collection;
32 import java.util.List;
34 import java.util.jar.Attributes;
35 import java.util.jar.JarFile;
36 import java.util.jar.Manifest;
37 import java.util.zip.ZipEntry;
38 import org.apache.maven.artifact.Artifact;
39 import org.apache.maven.plugin.AbstractMojo;
40 import org.apache.maven.plugin.MojoExecutionException;
41 import org.apache.maven.plugin.MojoFailureException;
42 import org.apache.maven.plugins.annotations.LifecyclePhase;
43 import org.apache.maven.plugins.annotations.Mojo;
44 import org.apache.maven.plugins.annotations.Parameter;
45 import org.apache.maven.plugins.annotations.ResolutionScope;
46 import org.apache.maven.project.MavenProject;
47 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
48 import org.apidesign.vm4brwsr.Bck2Brwsr;
49 import org.apidesign.vm4brwsr.ObfuscationLevel;
53 * @author Jaroslav Tulach
57 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
58 defaultPhase = LifecyclePhase.PACKAGE
60 public class AheadOfTime extends AbstractMojo {
61 @Parameter(defaultValue = "${project}")
62 private MavenProject prj;
64 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
67 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.js")
68 private File mainJavaScript;
71 private String[] exports;
74 * Directory where to generate ahead-of-time JavaScript files for
77 @Parameter(defaultValue = "lib")
78 private String classPathPrefix;
80 /** Root JavaScript file to generate */
81 @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
84 @Parameter(defaultValue = "true")
85 private boolean generateAotLibraries;
87 @Parameter(defaultValue = "true")
88 private boolean ignoreBootClassPath;
91 * The obfuscation level for the generated JavaScript file.
95 @Parameter(defaultValue = "NONE")
96 private ObfuscationLevel obfuscation;
99 public void execute() throws MojoExecutionException, MojoFailureException {
100 URLClassLoader loader;
102 loader = buildClassLoader(mainJar, prj.getArtifacts());
103 } catch (MalformedURLException ex) {
104 throw new MojoFailureException("Can't initialize classloader");
106 for (Artifact a : prj.getArtifacts()) {
107 if (a.getFile() == null) {
110 String n = a.getFile().getName();
111 if (!n.endsWith(".jar")) {
114 if ("provided".equals(a.getScope())) {
117 File aot = new File(mainJavaScript.getParent(), classPathPrefix);
119 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
121 getLog().info("Skipping " + js + " as it already exists.");
125 aotLibrary(a, js , loader);
126 } catch (IOException ex) {
127 throw new MojoFailureException("Can't compile " + a.getFile(), ex);
132 if (mainJavaScript.exists()) {
133 getLog().info("Skipping " + mainJavaScript + " as it already exists.");
135 getLog().info("Generating " + mainJavaScript);
136 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
137 if (exports != null) {
138 for (String e : exports) {
139 c = c.addExported(e.replace('.', '/'));
142 Writer w = new OutputStreamWriter(new FileOutputStream(mainJavaScript), "UTF-8");
144 obfuscation(obfuscation).
148 } catch (IOException ex) {
149 throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
153 Writer w = new OutputStreamWriter(new FileOutputStream(vm), "UTF-8");
154 Bck2Brwsr.newCompiler().
155 obfuscation(obfuscation).
157 resources(new Bck2Brwsr.Resources() {
160 public InputStream get(String resource) throws IOException {
167 } catch (IOException ex) {
168 throw new MojoExecutionException("Can't compile", ex);
172 private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException, MojoExecutionException {
173 for (Artifact b : prj.getArtifacts()) {
174 if ("bck2brwsr".equals(b.getClassifier())) { // NOI18N
175 getLog().debug("Inspecting " + b.getFile());
176 JarFile jf = new JarFile(b.getFile());
177 Manifest man = jf.getManifest();
178 for (Map.Entry<String, Attributes> entrySet : man.getEntries().entrySet()) {
179 String entryName = entrySet.getKey();
180 Attributes attr = entrySet.getValue();
183 a.getArtifactId().equals(attr.getValue("Bck2BrwsrArtifactId")) &&
184 a.getGroupId().equals(attr.getValue("Bck2BrwsrGroupId")) &&
185 a.getVersion().equals(attr.getValue("Bck2BrwsrVersion")) &&
187 obfuscation == ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrMinified"))
189 obfuscation != ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrDebug"))
192 getLog().info("Extracting " + js + " from " + b.getFile());
193 InputStream is = jf.getInputStream(new ZipEntry(entryName));
194 Files.copy(is, js.toPath());
201 if (!generateAotLibraries) {
202 throw new MojoExecutionException("Not generating " + js + " and no precompiled version found!");
204 getLog().info("Generating " + js);
205 Writer w = new OutputStreamWriter(new FileOutputStream(js), "UTF-8");
206 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
207 if (exports != null) {
208 c = c.addExported(exports);
211 obfuscation(obfuscation).
215 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
216 List<URL> arr = new ArrayList<URL>();
218 arr.add(root.toURI().toURL());
220 for (Artifact a : deps) {
221 if (a.getFile() != null) {
222 arr.add(a.getFile().toURI().toURL());
225 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());