jaroslav@1584
|
1 |
/**
|
jaroslav@1584
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@1787
|
3 |
* Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@1584
|
4 |
*
|
jaroslav@1584
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@1584
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@1584
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@1584
|
8 |
*
|
jaroslav@1584
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@1584
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@1584
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@1584
|
12 |
* GNU General Public License for more details.
|
jaroslav@1584
|
13 |
*
|
jaroslav@1584
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@1584
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@1584
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@1584
|
17 |
*/
|
jaroslav@1584
|
18 |
package org.apidesign.bck2brwsr.mojo;
|
jaroslav@1584
|
19 |
|
jaroslav@1584
|
20 |
import java.io.File;
|
jaroslav@1762
|
21 |
import java.io.FileOutputStream;
|
jaroslav@1584
|
22 |
import java.io.IOException;
|
jaroslav@1584
|
23 |
import java.io.InputStream;
|
jaroslav@1762
|
24 |
import java.io.OutputStreamWriter;
|
jaroslav@1762
|
25 |
import java.io.Writer;
|
jaroslav@1584
|
26 |
import java.net.MalformedURLException;
|
jaroslav@1584
|
27 |
import java.net.URL;
|
jaroslav@1584
|
28 |
import java.net.URLClassLoader;
|
jaroslav@1737
|
29 |
import java.nio.file.Files;
|
jaroslav@1584
|
30 |
import java.util.ArrayList;
|
jaroslav@1584
|
31 |
import java.util.Collection;
|
jaroslav@1584
|
32 |
import java.util.List;
|
jaroslav@1737
|
33 |
import java.util.Map;
|
jaroslav@1737
|
34 |
import java.util.jar.Attributes;
|
jaroslav@1737
|
35 |
import java.util.jar.JarFile;
|
jaroslav@1737
|
36 |
import java.util.jar.Manifest;
|
jaroslav@1737
|
37 |
import java.util.zip.ZipEntry;
|
jaroslav@1584
|
38 |
import org.apache.maven.artifact.Artifact;
|
jaroslav@1584
|
39 |
import org.apache.maven.plugin.AbstractMojo;
|
jaroslav@1584
|
40 |
import org.apache.maven.plugin.MojoExecutionException;
|
jaroslav@1584
|
41 |
import org.apache.maven.plugin.MojoFailureException;
|
jaroslav@1584
|
42 |
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
jaroslav@1584
|
43 |
import org.apache.maven.plugins.annotations.Mojo;
|
jaroslav@1584
|
44 |
import org.apache.maven.plugins.annotations.Parameter;
|
jaroslav@1584
|
45 |
import org.apache.maven.plugins.annotations.ResolutionScope;
|
jaroslav@1584
|
46 |
import org.apache.maven.project.MavenProject;
|
jaroslav@1599
|
47 |
import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
|
jaroslav@1584
|
48 |
import org.apidesign.vm4brwsr.Bck2Brwsr;
|
jaroslav@1584
|
49 |
import org.apidesign.vm4brwsr.ObfuscationLevel;
|
jaroslav@1584
|
50 |
|
jaroslav@1584
|
51 |
/**
|
jaroslav@1584
|
52 |
*
|
jaroslav@1584
|
53 |
* @author Jaroslav Tulach
|
jaroslav@1584
|
54 |
* @since 0.9
|
jaroslav@1584
|
55 |
*/
|
jaroslav@1584
|
56 |
@Mojo(name = "aot",
|
jaroslav@1584
|
57 |
requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
|
jaroslav@1584
|
58 |
defaultPhase = LifecyclePhase.PACKAGE
|
jaroslav@1584
|
59 |
)
|
jaroslav@1584
|
60 |
public class AheadOfTime extends AbstractMojo {
|
jaroslav@1584
|
61 |
@Parameter(defaultValue = "${project}")
|
jaroslav@1584
|
62 |
private MavenProject prj;
|
jaroslav@1604
|
63 |
|
jaroslav@1604
|
64 |
@Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
|
jaroslav@1604
|
65 |
private File mainJar;
|
jaroslav@1584
|
66 |
|
jaroslav@1604
|
67 |
@Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.js")
|
jaroslav@1604
|
68 |
private File mainJavaScript;
|
jaroslav@1604
|
69 |
|
jaroslav@1604
|
70 |
@Parameter
|
jaroslav@1604
|
71 |
private String[] exports;
|
jaroslav@1604
|
72 |
|
jaroslav@1584
|
73 |
/**
|
jaroslav@1584
|
74 |
* Directory where to generate ahead-of-time JavaScript files for
|
jaroslav@1584
|
75 |
* required libraries.
|
jaroslav@1584
|
76 |
*/
|
jaroslav@1604
|
77 |
@Parameter(defaultValue = "lib")
|
jaroslav@1604
|
78 |
private String classPathPrefix;
|
jaroslav@1584
|
79 |
|
jaroslav@1584
|
80 |
/** Root JavaScript file to generate */
|
jaroslav@1584
|
81 |
@Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
|
jaroslav@1584
|
82 |
private File vm;
|
jaroslav@1584
|
83 |
|
jaroslav@1737
|
84 |
@Parameter(defaultValue = "true")
|
jaroslav@1737
|
85 |
private boolean generateAotLibraries;
|
jaroslav@1737
|
86 |
|
jaroslav@1769
|
87 |
@Parameter(defaultValue = "true")
|
jaroslav@1769
|
88 |
private boolean ignoreBootClassPath;
|
jaroslav@1769
|
89 |
|
jaroslav@1584
|
90 |
/**
|
jaroslav@1584
|
91 |
* The obfuscation level for the generated JavaScript file.
|
jaroslav@1584
|
92 |
*
|
jaroslav@1584
|
93 |
* @since 0.5
|
jaroslav@1584
|
94 |
*/
|
jaroslav@1584
|
95 |
@Parameter(defaultValue = "NONE")
|
jaroslav@1584
|
96 |
private ObfuscationLevel obfuscation;
|
jaroslav@1584
|
97 |
|
jaroslav@1584
|
98 |
@Override
|
jaroslav@1584
|
99 |
public void execute() throws MojoExecutionException, MojoFailureException {
|
jaroslav@1594
|
100 |
URLClassLoader loader;
|
jaroslav@1584
|
101 |
try {
|
jaroslav@1604
|
102 |
loader = buildClassLoader(mainJar, prj.getArtifacts());
|
jaroslav@1594
|
103 |
} catch (MalformedURLException ex) {
|
jaroslav@1594
|
104 |
throw new MojoFailureException("Can't initialize classloader");
|
jaroslav@1594
|
105 |
}
|
jaroslav@1594
|
106 |
for (Artifact a : prj.getArtifacts()) {
|
jaroslav@1594
|
107 |
if (a.getFile() == null) {
|
jaroslav@1594
|
108 |
continue;
|
jaroslav@1594
|
109 |
}
|
jaroslav@1594
|
110 |
String n = a.getFile().getName();
|
jaroslav@1594
|
111 |
if (!n.endsWith(".jar")) {
|
jaroslav@1594
|
112 |
continue;
|
jaroslav@1594
|
113 |
}
|
jaroslav@1594
|
114 |
if ("provided".equals(a.getScope())) {
|
jaroslav@1594
|
115 |
continue;
|
jaroslav@1594
|
116 |
}
|
jaroslav@1604
|
117 |
File aot = new File(prj.getBuild().getDirectory(), classPathPrefix);
|
jaroslav@1594
|
118 |
aot.mkdirs();
|
jaroslav@1594
|
119 |
File js = new File(aot, n.substring(0, n.length() - 4) + ".js");
|
jaroslav@1618
|
120 |
if (js.exists()) {
|
jaroslav@1618
|
121 |
getLog().info("Skipping " + js + " as it already exists.");
|
jaroslav@1618
|
122 |
continue;
|
jaroslav@1618
|
123 |
}
|
jaroslav@1594
|
124 |
try {
|
jaroslav@1584
|
125 |
aotLibrary(a, js , loader);
|
jaroslav@1594
|
126 |
} catch (IOException ex) {
|
jaroslav@1737
|
127 |
throw new MojoFailureException("Can't compile " + a.getFile(), ex);
|
jaroslav@1584
|
128 |
}
|
jaroslav@1594
|
129 |
}
|
jaroslav@1604
|
130 |
|
jaroslav@1604
|
131 |
try {
|
jaroslav@1618
|
132 |
if (mainJavaScript.exists()) {
|
jaroslav@1618
|
133 |
getLog().info("Skipping " + mainJavaScript + " as it already exists.");
|
jaroslav@1618
|
134 |
} else {
|
jaroslav@1618
|
135 |
getLog().info("Generating " + mainJavaScript);
|
jaroslav@1769
|
136 |
Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
|
jaroslav@1618
|
137 |
if (exports != null) {
|
jaroslav@1618
|
138 |
for (String e : exports) {
|
jaroslav@1618
|
139 |
c = c.addExported(e.replace('.', '/'));
|
jaroslav@1618
|
140 |
}
|
jaroslav@1604
|
141 |
}
|
jaroslav@1762
|
142 |
Writer w = new OutputStreamWriter(new FileOutputStream(mainJavaScript), "UTF-8");
|
jaroslav@1618
|
143 |
c.
|
jaroslav@1618
|
144 |
obfuscation(obfuscation).
|
jaroslav@1618
|
145 |
generate(w);
|
jaroslav@1618
|
146 |
w.close();
|
jaroslav@1604
|
147 |
}
|
jaroslav@1604
|
148 |
} catch (IOException ex) {
|
jaroslav@1604
|
149 |
throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
|
jaroslav@1604
|
150 |
}
|
jaroslav@1584
|
151 |
|
jaroslav@1594
|
152 |
try {
|
jaroslav@1762
|
153 |
Writer w = new OutputStreamWriter(new FileOutputStream(vm), "UTF-8");
|
jaroslav@1584
|
154 |
Bck2Brwsr.newCompiler().
|
jaroslav@1584
|
155 |
obfuscation(obfuscation).
|
jaroslav@1584
|
156 |
standalone(false).
|
jaroslav@1584
|
157 |
resources(new Bck2Brwsr.Resources() {
|
jaroslav@1584
|
158 |
|
jaroslav@1584
|
159 |
@Override
|
jaroslav@1584
|
160 |
public InputStream get(String resource) throws IOException {
|
jaroslav@1584
|
161 |
return null;
|
jaroslav@1584
|
162 |
}
|
jaroslav@1584
|
163 |
}).
|
jaroslav@1584
|
164 |
generate(w);
|
jaroslav@1584
|
165 |
w.close();
|
jaroslav@1584
|
166 |
|
jaroslav@1584
|
167 |
} catch (IOException ex) {
|
jaroslav@1584
|
168 |
throw new MojoExecutionException("Can't compile", ex);
|
jaroslav@1584
|
169 |
}
|
jaroslav@1584
|
170 |
}
|
jaroslav@1584
|
171 |
|
jaroslav@1737
|
172 |
private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException, MojoExecutionException {
|
jaroslav@1737
|
173 |
for (Artifact b : prj.getArtifacts()) {
|
jaroslav@1737
|
174 |
if ("bck2brwsr".equals(b.getClassifier())) { // NOI18N
|
jaroslav@1737
|
175 |
getLog().debug("Inspecting " + b.getFile());
|
jaroslav@1737
|
176 |
JarFile jf = new JarFile(b.getFile());
|
jaroslav@1737
|
177 |
Manifest man = jf.getManifest();
|
jaroslav@1737
|
178 |
for (Map.Entry<String, Attributes> entrySet : man.getEntries().entrySet()) {
|
jaroslav@1737
|
179 |
String entryName = entrySet.getKey();
|
jaroslav@1737
|
180 |
Attributes attr = entrySet.getValue();
|
jaroslav@1737
|
181 |
|
jaroslav@1737
|
182 |
if (
|
jaroslav@1737
|
183 |
a.getArtifactId().equals(attr.getValue("Bck2BrwsrArtifactId")) &&
|
jaroslav@1737
|
184 |
a.getGroupId().equals(attr.getValue("Bck2BrwsrGroupId")) &&
|
jaroslav@1737
|
185 |
a.getVersion().equals(attr.getValue("Bck2BrwsrVersion")) &&
|
jaroslav@1737
|
186 |
(
|
jaroslav@1737
|
187 |
obfuscation == ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrMinified"))
|
jaroslav@1737
|
188 |
||
|
jaroslav@1737
|
189 |
obfuscation != ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrDebug"))
|
jaroslav@1737
|
190 |
)
|
jaroslav@1737
|
191 |
) {
|
jaroslav@1737
|
192 |
getLog().info("Extracting " + js + " from " + b.getFile());
|
jaroslav@1737
|
193 |
InputStream is = jf.getInputStream(new ZipEntry(entryName));
|
jaroslav@1737
|
194 |
Files.copy(is, js.toPath());
|
jaroslav@1737
|
195 |
is.close();
|
jaroslav@1737
|
196 |
return;
|
jaroslav@1737
|
197 |
}
|
jaroslav@1737
|
198 |
}
|
jaroslav@1737
|
199 |
}
|
jaroslav@1737
|
200 |
}
|
jaroslav@1737
|
201 |
if (!generateAotLibraries) {
|
jaroslav@1737
|
202 |
throw new MojoExecutionException("Not generating " + js + " and no precompiled version found!");
|
jaroslav@1737
|
203 |
}
|
jaroslav@1737
|
204 |
getLog().info("Generating " + js);
|
jaroslav@1762
|
205 |
Writer w = new OutputStreamWriter(new FileOutputStream(js), "UTF-8");
|
jaroslav@1769
|
206 |
Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
|
jaroslav@1599
|
207 |
c.
|
jaroslav@1599
|
208 |
obfuscation(obfuscation).
|
jaroslav@1599
|
209 |
generate(w);
|
jaroslav@1584
|
210 |
w.close();
|
jaroslav@1584
|
211 |
}
|
jaroslav@1584
|
212 |
private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
|
jaroslav@1584
|
213 |
List<URL> arr = new ArrayList<URL>();
|
jaroslav@1584
|
214 |
if (root != null) {
|
jaroslav@1584
|
215 |
arr.add(root.toURI().toURL());
|
jaroslav@1584
|
216 |
}
|
jaroslav@1584
|
217 |
for (Artifact a : deps) {
|
jaroslav@1584
|
218 |
if (a.getFile() != null) {
|
jaroslav@1584
|
219 |
arr.add(a.getFile().toURI().toURL());
|
jaroslav@1584
|
220 |
}
|
jaroslav@1584
|
221 |
}
|
jaroslav@1584
|
222 |
return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
|
jaroslav@1584
|
223 |
}
|
jaroslav@1584
|
224 |
}
|