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.nio.file.StandardCopyOption;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.List;
35 import java.util.jar.Attributes;
36 import java.util.jar.JarFile;
37 import java.util.jar.Manifest;
38 import java.util.zip.ZipEntry;
39 import org.apache.maven.artifact.Artifact;
40 import org.apache.maven.plugin.AbstractMojo;
41 import org.apache.maven.plugin.MojoExecutionException;
42 import org.apache.maven.plugin.MojoFailureException;
43 import org.apache.maven.plugins.annotations.LifecyclePhase;
44 import org.apache.maven.plugins.annotations.Mojo;
45 import org.apache.maven.plugins.annotations.Parameter;
46 import org.apache.maven.plugins.annotations.ResolutionScope;
47 import org.apache.maven.project.MavenProject;
48 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
49 import org.apidesign.vm4brwsr.Bck2Brwsr;
50 import org.apidesign.vm4brwsr.ObfuscationLevel;
54 * @author Jaroslav Tulach
58 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
59 defaultPhase = LifecyclePhase.PACKAGE
61 public class AheadOfTime extends AbstractMojo {
62 @Parameter(defaultValue = "${project}")
63 private MavenProject prj;
65 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
68 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.js")
69 private File mainJavaScript;
72 private String[] exports;
75 * Directory where to generate ahead-of-time JavaScript files for
78 @Parameter(defaultValue = "lib")
79 private String classPathPrefix;
81 /** Root JavaScript file to generate */
82 @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
85 @Parameter(defaultValue = "true")
86 private boolean generateAotLibraries;
88 @Parameter(defaultValue = "true")
89 private boolean ignoreBootClassPath;
92 * The obfuscation level for the generated JavaScript file.
96 @Parameter(defaultValue = "NONE")
97 private ObfuscationLevel obfuscation;
100 public void execute() throws MojoExecutionException, MojoFailureException {
101 URLClassLoader loader;
103 loader = buildClassLoader(mainJar, prj.getArtifacts());
104 } catch (MalformedURLException ex) {
105 throw new MojoFailureException("Can't initialize classloader");
107 for (Artifact a : prj.getArtifacts()) {
108 if (a.getFile() == null) {
111 String n = a.getFile().getName();
112 if (!n.endsWith(".jar")) {
115 if ("provided".equals(a.getScope())) {
118 File aot = new File(mainJavaScript.getParent(), classPathPrefix);
120 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
121 if (js.lastModified() > a.getFile().lastModified()) {
122 getLog().info("Skipping " + js + " as it already exists.");
126 aotLibrary(a, js , loader);
127 } catch (IOException ex) {
128 throw new MojoFailureException("Can't compile " + a.getFile(), ex);
133 if (mainJavaScript.lastModified() > mainJar.lastModified()) {
134 getLog().info("Skipping " + mainJavaScript + " as it already exists.");
136 getLog().info("Generating " + mainJavaScript);
137 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
138 if (exports != null) {
139 for (String e : exports) {
140 c = c.addExported(e.replace('.', '/'));
143 Writer w = new OutputStreamWriter(new FileOutputStream(mainJavaScript), "UTF-8");
145 obfuscation(obfuscation).
149 } catch (IOException ex) {
150 throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
154 Writer w = new OutputStreamWriter(new FileOutputStream(vm), "UTF-8");
155 Bck2Brwsr.newCompiler().
156 obfuscation(obfuscation).
158 resources(new Bck2Brwsr.Resources() {
161 public InputStream get(String resource) throws IOException {
168 } catch (IOException ex) {
169 throw new MojoExecutionException("Can't compile", ex);
173 private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException, MojoExecutionException {
174 for (Artifact b : prj.getArtifacts()) {
175 if ("bck2brwsr".equals(b.getClassifier())) { // NOI18N
176 getLog().debug("Inspecting " + b.getFile());
177 JarFile jf = new JarFile(b.getFile());
178 Manifest man = jf.getManifest();
179 for (Map.Entry<String, Attributes> entrySet : man.getEntries().entrySet()) {
180 String entryName = entrySet.getKey();
181 Attributes attr = entrySet.getValue();
184 a.getArtifactId().equals(attr.getValue("Bck2BrwsrArtifactId")) &&
185 a.getGroupId().equals(attr.getValue("Bck2BrwsrGroupId")) &&
186 a.getVersion().equals(attr.getValue("Bck2BrwsrVersion")) &&
188 obfuscation == ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrMinified"))
190 obfuscation != ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrDebug"))
193 getLog().info("Extracting " + js + " from " + b.getFile());
194 InputStream is = jf.getInputStream(new ZipEntry(entryName));
195 Files.copy(is, js.toPath(), StandardCopyOption.REPLACE_EXISTING);
202 if (!generateAotLibraries) {
203 throw new MojoExecutionException("Not generating " + js + " and no precompiled version found!");
205 getLog().info("Generating " + js);
206 Writer w = new OutputStreamWriter(new FileOutputStream(js), "UTF-8");
207 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
208 if (exports != null) {
209 c = c.addExported(exports);
212 obfuscation(obfuscation).
216 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
217 List<URL> arr = new ArrayList<URL>();
219 arr.add(root.toURI().toURL());
221 for (Artifact a : deps) {
222 if (a.getFile() != null) {
223 arr.add(a.getFile().toURI().toURL());
226 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());