Can change obfuscation of the minified library - probably only meaningful to use MINIMAL instead of FULL
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 * The obfuscation level for the generated minified JavaScript file.
88 @Parameter(defaultValue = "FULL")
89 private ObfuscationLevel obfuscation;
92 public void execute() throws MojoExecutionException, MojoFailureException {
93 URLClassLoader loader;
95 loader = buildClassLoader(mainJar, prj.getArtifacts());
96 } catch (MalformedURLException ex) {
97 throw new MojoFailureException("Can't initialize classloader");
101 Manifest m = new Manifest();
102 if (!"false".equals(minified)) {
103 Attributes attr = new Attributes();
104 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
105 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
106 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
107 attr.putValue("Bck2BrwsrMinified", "true");
108 m.getEntries().put(minified, attr);
110 if (!"false".equals(debug)) {
111 Attributes attr = new Attributes();
112 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
113 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
114 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
115 attr.putValue("Bck2BrwsrDebug", "true");
116 m.getEntries().put(debug, attr);
119 if (aotDeps != null) {
120 for (Artifact a : prj.getArtifacts()) {
121 if (!matches(aotDeps, a)) {
126 Attributes attr = new Attributes();
127 attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
128 attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
129 attr.putValue("Bck2BrwsrVersion", a.getVersion());
130 attr.putValue("Bck2BrwsrDebug", "true");
131 m.getEntries().put(artifactName(a, true), attr);
134 Attributes attr = new Attributes();
135 attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
136 attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
137 attr.putValue("Bck2BrwsrVersion", a.getVersion());
138 attr.putValue("Bck2BrwsrMinified", "true");
139 m.getEntries().put(artifactName(a, false), attr);
144 FileOutputStream fos = new FileOutputStream(this.aotJar);
145 JarOutputStream os = new JarOutputStream(fos, m);
147 if (!"false".equals(debug)) {
148 os.putNextEntry(new JarEntry(debug));
149 Writer w = new OutputStreamWriter(os, "UTF-8");
150 configureMain(loader).
151 obfuscation(ObfuscationLevel.NONE).
156 if (!"false".equals(minified)) {
157 os.putNextEntry(new JarEntry(minified));
159 Writer w = new OutputStreamWriter(os, "UTF-8");
160 configureMain(loader).
161 obfuscation(obfuscation).
167 if (aotDeps != null) {
168 for (Artifact a : prj.getArtifacts()) {
169 if (!matches(aotDeps, a)) {
172 getLog().info("Generating bck2brwsr for " + a.getFile());
173 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
174 if (exports != null) {
175 for (String e : exports) {
176 c = c.addExported(e.replace('.', '/'));
180 os.putNextEntry(new JarEntry(artifactName(a, true)));
181 Writer w = new OutputStreamWriter(os, "UTF-8");
183 obfuscation(ObfuscationLevel.NONE).
189 os.putNextEntry(new JarEntry(artifactName(a, false)));
191 Writer w = new OutputStreamWriter(os, "UTF-8");
193 obfuscation(ObfuscationLevel.FULL).
202 projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar);
203 } catch (IOException ex) {
204 throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
208 private Bck2Brwsr configureMain(URLClassLoader loader) throws IOException {
209 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
210 if (exports != null) {
211 for (String e : exports) {
212 c = c.addExported(e.replace('.', '/'));
218 private static String artifactName(Artifact a, boolean debug) {
219 return a.getGroupId() + "-" + a.getArtifactId() + (debug ? "-debug.js" : "-min.js");
222 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
223 List<URL> arr = new ArrayList<URL>();
225 arr.add(root.toURI().toURL());
227 for (Artifact a : deps) {
228 if (a.getFile() != null) {
229 arr.add(a.getFile().toURI().toURL());
232 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
235 private static boolean matches(String[] aotDeps, Artifact a) {
236 for (String d : aotDeps) {
237 String[] parts = d.split(":");
238 for (int i = 0; i < parts.length; i++) {
239 if ("*".equals(parts[i])) {
244 if (parts[0] != null && !parts[0].equals(a.getGroupId())) {
247 if (parts[1] != null && !parts[1].equals(a.getArtifactId())) {
250 if (parts.length > 2 && parts[2] != null && !parts[2].equals(a.getClassifier())) {