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.FileOutputStream;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.Writer;
27 import java.net.MalformedURLException;
29 import java.net.URLClassLoader;
30 import java.nio.file.Files;
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;
89 * The obfuscation level for the generated JavaScript file.
93 @Parameter(defaultValue = "NONE")
94 private ObfuscationLevel obfuscation;
97 public void execute() throws MojoExecutionException, MojoFailureException {
98 URLClassLoader loader;
100 loader = buildClassLoader(mainJar, prj.getArtifacts());
101 } catch (MalformedURLException ex) {
102 throw new MojoFailureException("Can't initialize classloader");
104 for (Artifact a : prj.getArtifacts()) {
105 if (a.getFile() == null) {
108 String n = a.getFile().getName();
109 if (!n.endsWith(".jar")) {
112 if ("provided".equals(a.getScope())) {
115 File aot = new File(prj.getBuild().getDirectory(), classPathPrefix);
117 File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
119 getLog().info("Skipping " + js + " as it already exists.");
123 aotLibrary(a, js , loader);
124 } catch (IOException ex) {
125 throw new MojoFailureException("Can't compile " + a.getFile(), ex);
130 if (mainJavaScript.exists()) {
131 getLog().info("Skipping " + mainJavaScript + " as it already exists.");
133 getLog().info("Generating " + mainJavaScript);
134 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader);
135 if (exports != null) {
136 for (String e : exports) {
137 c = c.addExported(e.replace('.', '/'));
140 Writer w = new OutputStreamWriter(new FileOutputStream(mainJavaScript), "UTF-8");
142 obfuscation(obfuscation).
146 } catch (IOException ex) {
147 throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
151 Writer w = new OutputStreamWriter(new FileOutputStream(vm), "UTF-8");
152 Bck2Brwsr.newCompiler().
153 obfuscation(obfuscation).
155 resources(new Bck2Brwsr.Resources() {
158 public InputStream get(String resource) throws IOException {
165 } catch (IOException ex) {
166 throw new MojoExecutionException("Can't compile", ex);
170 private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException, MojoExecutionException {
171 for (Artifact b : prj.getArtifacts()) {
172 if ("bck2brwsr".equals(b.getClassifier())) { // NOI18N
173 getLog().debug("Inspecting " + b.getFile());
174 JarFile jf = new JarFile(b.getFile());
175 Manifest man = jf.getManifest();
176 for (Map.Entry<String, Attributes> entrySet : man.getEntries().entrySet()) {
177 String entryName = entrySet.getKey();
178 Attributes attr = entrySet.getValue();
181 a.getArtifactId().equals(attr.getValue("Bck2BrwsrArtifactId")) &&
182 a.getGroupId().equals(attr.getValue("Bck2BrwsrGroupId")) &&
183 a.getVersion().equals(attr.getValue("Bck2BrwsrVersion")) &&
185 obfuscation == ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrMinified"))
187 obfuscation != ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrDebug"))
190 getLog().info("Extracting " + js + " from " + b.getFile());
191 InputStream is = jf.getInputStream(new ZipEntry(entryName));
192 Files.copy(is, js.toPath());
199 if (!generateAotLibraries) {
200 throw new MojoExecutionException("Not generating " + js + " and no precompiled version found!");
202 getLog().info("Generating " + js);
203 Writer w = new OutputStreamWriter(new FileOutputStream(js), "UTF-8");
204 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader);
206 obfuscation(obfuscation).
210 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
211 List<URL> arr = new ArrayList<URL>();
213 arr.add(root.toURI().toURL());
215 for (Artifact a : deps) {
216 if (a.getFile() != null) {
217 arr.add(a.getFile().toURI().toURL());
220 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());