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.OutputStreamWriter;
24 import java.io.Writer;
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 java.util.jar.Attributes;
32 import java.util.jar.JarEntry;
33 import java.util.jar.JarOutputStream;
34 import java.util.jar.Manifest;
35 import org.apache.maven.artifact.Artifact;
36 import org.apache.maven.plugin.AbstractMojo;
37 import org.apache.maven.plugin.MojoExecutionException;
38 import org.apache.maven.plugin.MojoFailureException;
39 import org.apache.maven.plugins.annotations.Component;
40 import org.apache.maven.plugins.annotations.LifecyclePhase;
41 import org.apache.maven.plugins.annotations.Mojo;
42 import org.apache.maven.plugins.annotations.Parameter;
43 import org.apache.maven.plugins.annotations.ResolutionScope;
44 import org.apache.maven.project.MavenProject;
45 import org.apache.maven.project.MavenProjectHelper;
46 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
47 import org.apidesign.vm4brwsr.Bck2Brwsr;
48 import org.apidesign.vm4brwsr.ObfuscationLevel;
52 * @author Jaroslav Tulach
55 @Mojo(name = "library",
56 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
57 defaultPhase = LifecyclePhase.PACKAGE
59 public class AOTLibrary extends AbstractMojo {
60 @Parameter(defaultValue = "${project}")
61 private MavenProject prj;
64 private MavenProjectHelper projectHelper;
65 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
67 @Parameter(defaultValue = "${project.build.finalName}-min.js")
68 private String minified;
69 @Parameter(defaultValue = "${project.build.finalName}-debug.js")
72 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}-bck2brwsr.jar")
76 private String[] exports;
79 private String[] aotDeps;
81 @Parameter(defaultValue = "true")
82 private boolean ignoreBootClassPath;
85 public void execute() throws MojoExecutionException, MojoFailureException {
86 URLClassLoader loader;
88 loader = buildClassLoader(mainJar, prj.getArtifacts());
89 } catch (MalformedURLException ex) {
90 throw new MojoFailureException("Can't initialize classloader");
94 Manifest m = new Manifest();
95 if (!"false".equals(minified)) {
96 Attributes attr = new Attributes();
97 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
98 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
99 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
100 attr.putValue("Bck2BrwsrMinified", "true");
101 m.getEntries().put(minified, attr);
103 if (!"false".equals(debug)) {
104 Attributes attr = new Attributes();
105 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
106 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
107 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
108 attr.putValue("Bck2BrwsrDebug", "true");
109 m.getEntries().put(debug, attr);
112 if (aotDeps != null) {
113 for (Artifact a : prj.getArtifacts()) {
114 if (!matches(aotDeps, a)) {
119 Attributes attr = new Attributes();
120 attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
121 attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
122 attr.putValue("Bck2BrwsrVersion", a.getVersion());
123 attr.putValue("Bck2BrwsrDebug", "true");
124 m.getEntries().put(artifactName(a, true), attr);
127 Attributes attr = new Attributes();
128 attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
129 attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
130 attr.putValue("Bck2BrwsrVersion", a.getVersion());
131 attr.putValue("Bck2BrwsrMinified", "true");
132 m.getEntries().put(artifactName(a, false), attr);
137 FileOutputStream fos = new FileOutputStream(this.aotJar);
138 JarOutputStream os = new JarOutputStream(fos, m);
140 if (!"false".equals(debug)) {
141 os.putNextEntry(new JarEntry(debug));
142 Writer w = new OutputStreamWriter(os, "UTF-8");
143 configureMain(loader).
144 obfuscation(ObfuscationLevel.NONE).
149 if (!"false".equals(minified)) {
150 os.putNextEntry(new JarEntry(minified));
152 Writer w = new OutputStreamWriter(os, "UTF-8");
153 configureMain(loader).
154 obfuscation(ObfuscationLevel.FULL).
160 if (aotDeps != null) {
161 for (Artifact a : prj.getArtifacts()) {
162 if (!matches(aotDeps, a)) {
165 getLog().info("Generating bck2brwsr for " + a.getFile());
166 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
167 if (exports != null) {
168 for (String e : exports) {
169 c = c.addExported(e.replace('.', '/'));
173 os.putNextEntry(new JarEntry(artifactName(a, true)));
174 Writer w = new OutputStreamWriter(os, "UTF-8");
176 obfuscation(ObfuscationLevel.NONE).
182 os.putNextEntry(new JarEntry(artifactName(a, false)));
184 Writer w = new OutputStreamWriter(os, "UTF-8");
186 obfuscation(ObfuscationLevel.FULL).
195 projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar);
196 } catch (IOException ex) {
197 throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
201 private Bck2Brwsr configureMain(URLClassLoader loader) throws IOException {
202 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
203 if (exports != null) {
204 for (String e : exports) {
205 c = c.addExported(e.replace('.', '/'));
211 private static String artifactName(Artifact a, boolean debug) {
212 return a.getGroupId() + "-" + a.getArtifactId() + (debug ? "-debug.js" : "-min.js");
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());
228 private static boolean matches(String[] aotDeps, Artifact a) {
229 for (String d : aotDeps) {
230 String[] parts = d.split(":");
231 for (int i = 0; i < parts.length; i++) {
232 if ("*".equals(parts[i])) {
237 if (parts[0] != null && !parts[0].equals(a.getGroupId())) {
240 if (parts[1] != null && !parts[1].equals(a.getArtifactId())) {
243 if (parts.length > 2 && parts[2] != null && !parts[2].equals(a.getClassifier())) {