Ability to extract precompiled bck2brwsr JavaScript files from Maven artifacts named bck2brwsr
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.OutputStreamWriter;
25 import java.io.Writer;
26 import java.net.MalformedURLException;
28 import java.net.URLClassLoader;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.jar.Attributes;
33 import java.util.jar.JarEntry;
34 import java.util.jar.JarOutputStream;
35 import java.util.jar.Manifest;
36 import org.apache.maven.artifact.Artifact;
37 import org.apache.maven.plugin.AbstractMojo;
38 import org.apache.maven.plugin.MojoExecutionException;
39 import org.apache.maven.plugin.MojoFailureException;
40 import org.apache.maven.plugins.annotations.Component;
41 import org.apache.maven.plugins.annotations.LifecyclePhase;
42 import org.apache.maven.plugins.annotations.Mojo;
43 import org.apache.maven.plugins.annotations.Parameter;
44 import org.apache.maven.plugins.annotations.ResolutionScope;
45 import org.apache.maven.project.MavenProject;
46 import org.apache.maven.project.MavenProjectHelper;
47 import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
48 import org.apidesign.vm4brwsr.Bck2Brwsr;
49 import org.apidesign.vm4brwsr.ObfuscationLevel;
53 * @author Jaroslav Tulach
56 @Mojo(name = "library",
57 requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
58 defaultPhase = LifecyclePhase.PACKAGE
60 public class AOTLibrary extends AbstractMojo {
61 @Parameter(defaultValue = "${project}")
62 private MavenProject prj;
65 private MavenProjectHelper projectHelper;
66 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
68 @Parameter(defaultValue = "${project.build.finalName}-min.js")
69 private String minified;
70 @Parameter(defaultValue = "${project.build.finalName}-debug.js")
73 @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}-bck2brwsr.jar")
77 private String[] exports;
80 public void execute() throws MojoExecutionException, MojoFailureException {
81 URLClassLoader loader;
83 loader = buildClassLoader(mainJar, prj.getArtifacts());
84 } catch (MalformedURLException ex) {
85 throw new MojoFailureException("Can't initialize classloader");
89 Manifest m = new Manifest();
91 Attributes attr = new Attributes();
92 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
93 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
94 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
95 attr.putValue("Bck2BrwsrMinified", "true");
96 m.getEntries().put(minified, attr);
99 Attributes attr = new Attributes();
100 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
101 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
102 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
103 attr.putValue("Bck2BrwsrDebug", "true");
104 m.getEntries().put(debug, attr);
107 FileOutputStream fos = new FileOutputStream(this.aotJar);
108 JarOutputStream os = new JarOutputStream(fos, m);
110 Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader);
111 if (exports != null) {
112 for (String e : exports) {
113 c = c.addExported(e.replace('.', '/'));
117 os.putNextEntry(new JarEntry(debug));
118 Writer w = new OutputStreamWriter(os);
120 obfuscation(ObfuscationLevel.NONE).
126 os.putNextEntry(new JarEntry(minified));
128 Writer w = new OutputStreamWriter(os);
130 obfuscation(ObfuscationLevel.FULL).
137 projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar);
138 } catch (IOException ex) {
139 throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
143 private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
144 List<URL> arr = new ArrayList<URL>();
146 arr.add(root.toURI().toURL());
148 for (Artifact a : deps) {
149 if (a.getFile() != null) {
150 arr.add(a.getFile().toURI().toURL());
153 return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());