1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AOTLibrary.java Sat Dec 06 06:28:17 2014 +0100
1.3 @@ -0,0 +1,155 @@
1.4 +/**
1.5 + * Back 2 Browser Bytecode Translator
1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 + *
1.8 + * This program is free software: you can redistribute it and/or modify
1.9 + * it under the terms of the GNU General Public License as published by
1.10 + * the Free Software Foundation, version 2 of the License.
1.11 + *
1.12 + * This program is distributed in the hope that it will be useful,
1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 + * GNU General Public License for more details.
1.16 + *
1.17 + * You should have received a copy of the GNU General Public License
1.18 + * along with this program. Look for COPYING file in the top folder.
1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 + */
1.21 +
1.22 +package org.apidesign.bck2brwsr.mojo;
1.23 +
1.24 +import java.io.File;
1.25 +import java.io.FileOutputStream;
1.26 +import java.io.IOException;
1.27 +import java.io.OutputStreamWriter;
1.28 +import java.io.Writer;
1.29 +import java.net.MalformedURLException;
1.30 +import java.net.URL;
1.31 +import java.net.URLClassLoader;
1.32 +import java.util.ArrayList;
1.33 +import java.util.Collection;
1.34 +import java.util.List;
1.35 +import java.util.jar.Attributes;
1.36 +import java.util.jar.JarEntry;
1.37 +import java.util.jar.JarOutputStream;
1.38 +import java.util.jar.Manifest;
1.39 +import org.apache.maven.artifact.Artifact;
1.40 +import org.apache.maven.plugin.AbstractMojo;
1.41 +import org.apache.maven.plugin.MojoExecutionException;
1.42 +import org.apache.maven.plugin.MojoFailureException;
1.43 +import org.apache.maven.plugins.annotations.Component;
1.44 +import org.apache.maven.plugins.annotations.LifecyclePhase;
1.45 +import org.apache.maven.plugins.annotations.Mojo;
1.46 +import org.apache.maven.plugins.annotations.Parameter;
1.47 +import org.apache.maven.plugins.annotations.ResolutionScope;
1.48 +import org.apache.maven.project.MavenProject;
1.49 +import org.apache.maven.project.MavenProjectHelper;
1.50 +import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars;
1.51 +import org.apidesign.vm4brwsr.Bck2Brwsr;
1.52 +import org.apidesign.vm4brwsr.ObfuscationLevel;
1.53 +
1.54 +/**
1.55 + *
1.56 + * @author Jaroslav Tulach
1.57 + * @since 0.12
1.58 + */
1.59 +@Mojo(name = "library",
1.60 + requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
1.61 + defaultPhase = LifecyclePhase.PACKAGE
1.62 +)
1.63 +public class AOTLibrary extends AbstractMojo {
1.64 + @Parameter(defaultValue = "${project}")
1.65 + private MavenProject prj;
1.66 +
1.67 + @Component
1.68 + private MavenProjectHelper projectHelper;
1.69 + @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
1.70 + private File mainJar;
1.71 + @Parameter(defaultValue = "${project.build.finalName}-min.js")
1.72 + private String minified;
1.73 + @Parameter(defaultValue = "${project.build.finalName}-debug.js")
1.74 + private String debug;
1.75 +
1.76 + @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}-bck2brwsr.jar")
1.77 + private File aotJar;
1.78 +
1.79 + @Parameter
1.80 + private String[] exports;
1.81 +
1.82 + @Override
1.83 + public void execute() throws MojoExecutionException, MojoFailureException {
1.84 + URLClassLoader loader;
1.85 + try {
1.86 + loader = buildClassLoader(mainJar, prj.getArtifacts());
1.87 + } catch (MalformedURLException ex) {
1.88 + throw new MojoFailureException("Can't initialize classloader");
1.89 + }
1.90 +
1.91 + try {
1.92 + Manifest m = new Manifest();
1.93 + {
1.94 + Attributes attr = new Attributes();
1.95 + attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
1.96 + attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
1.97 + attr.putValue("Bck2BrwsrVersion", prj.getVersion());
1.98 + attr.putValue("Bck2BrwsrMinified", "true");
1.99 + m.getEntries().put(minified, attr);
1.100 + }
1.101 + {
1.102 + Attributes attr = new Attributes();
1.103 + attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
1.104 + attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
1.105 + attr.putValue("Bck2BrwsrVersion", prj.getVersion());
1.106 + attr.putValue("Bck2BrwsrDebug", "true");
1.107 + m.getEntries().put(debug, attr);
1.108 + }
1.109 +
1.110 + FileOutputStream fos = new FileOutputStream(this.aotJar);
1.111 + JarOutputStream os = new JarOutputStream(fos, m);
1.112 +
1.113 + Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader);
1.114 + if (exports != null) {
1.115 + for (String e : exports) {
1.116 + c = c.addExported(e.replace('.', '/'));
1.117 + }
1.118 + }
1.119 + {
1.120 + os.putNextEntry(new JarEntry(debug));
1.121 + Writer w = new OutputStreamWriter(os);
1.122 + c.
1.123 + obfuscation(ObfuscationLevel.NONE).
1.124 + generate(w);
1.125 + w.flush();
1.126 + os.closeEntry();
1.127 + }
1.128 + {
1.129 + os.putNextEntry(new JarEntry(minified));
1.130 +
1.131 + Writer w = new OutputStreamWriter(os);
1.132 + c.
1.133 + obfuscation(ObfuscationLevel.FULL).
1.134 + generate(w);
1.135 + w.flush();
1.136 + os.closeEntry();
1.137 + }
1.138 + os.close();
1.139 +
1.140 + projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar);
1.141 + } catch (IOException ex) {
1.142 + throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
1.143 + }
1.144 + }
1.145 +
1.146 + private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
1.147 + List<URL> arr = new ArrayList<URL>();
1.148 + if (root != null) {
1.149 + arr.add(root.toURI().toURL());
1.150 + }
1.151 + for (Artifact a : deps) {
1.152 + if (a.getFile() != null) {
1.153 + arr.add(a.getFile().toURI().toURL());
1.154 + }
1.155 + }
1.156 + return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
1.157 + }
1.158 +}