diff -r 6db177c4f72c -r 82fd3830167d rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AOTLibrary.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AOTLibrary.java Sat Dec 06 06:28:17 2014 +0100 @@ -0,0 +1,155 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ + +package org.apidesign.bck2brwsr.mojo; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.jar.Attributes; +import java.util.jar.JarEntry; +import java.util.jar.JarOutputStream; +import java.util.jar.Manifest; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugins.annotations.Component; +import org.apache.maven.plugins.annotations.LifecyclePhase; +import org.apache.maven.plugins.annotations.Mojo; +import org.apache.maven.plugins.annotations.Parameter; +import org.apache.maven.plugins.annotations.ResolutionScope; +import org.apache.maven.project.MavenProject; +import org.apache.maven.project.MavenProjectHelper; +import org.apidesign.bck2brwsr.aot.Bck2BrwsrJars; +import org.apidesign.vm4brwsr.Bck2Brwsr; +import org.apidesign.vm4brwsr.ObfuscationLevel; + +/** + * + * @author Jaroslav Tulach + * @since 0.12 + */ +@Mojo(name = "library", + requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, + defaultPhase = LifecyclePhase.PACKAGE +) +public class AOTLibrary extends AbstractMojo { + @Parameter(defaultValue = "${project}") + private MavenProject prj; + + @Component + private MavenProjectHelper projectHelper; + @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar") + private File mainJar; + @Parameter(defaultValue = "${project.build.finalName}-min.js") + private String minified; + @Parameter(defaultValue = "${project.build.finalName}-debug.js") + private String debug; + + @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}-bck2brwsr.jar") + private File aotJar; + + @Parameter + private String[] exports; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + URLClassLoader loader; + try { + loader = buildClassLoader(mainJar, prj.getArtifacts()); + } catch (MalformedURLException ex) { + throw new MojoFailureException("Can't initialize classloader"); + } + + try { + Manifest m = new Manifest(); + { + Attributes attr = new Attributes(); + attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId()); + attr.putValue("Bck2BrwsrGroupId", prj.getGroupId()); + attr.putValue("Bck2BrwsrVersion", prj.getVersion()); + attr.putValue("Bck2BrwsrMinified", "true"); + m.getEntries().put(minified, attr); + } + { + Attributes attr = new Attributes(); + attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId()); + attr.putValue("Bck2BrwsrGroupId", prj.getGroupId()); + attr.putValue("Bck2BrwsrVersion", prj.getVersion()); + attr.putValue("Bck2BrwsrDebug", "true"); + m.getEntries().put(debug, attr); + } + + FileOutputStream fos = new FileOutputStream(this.aotJar); + JarOutputStream os = new JarOutputStream(fos, m); + + Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader); + if (exports != null) { + for (String e : exports) { + c = c.addExported(e.replace('.', '/')); + } + } + { + os.putNextEntry(new JarEntry(debug)); + Writer w = new OutputStreamWriter(os); + c. + obfuscation(ObfuscationLevel.NONE). + generate(w); + w.flush(); + os.closeEntry(); + } + { + os.putNextEntry(new JarEntry(minified)); + + Writer w = new OutputStreamWriter(os); + c. + obfuscation(ObfuscationLevel.FULL). + generate(w); + w.flush(); + os.closeEntry(); + } + os.close(); + + projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar); + } catch (IOException ex) { + throw new MojoFailureException("Cannot generate script for " + mainJar, ex); + } + } + + private static URLClassLoader buildClassLoader(File root, Collection deps) throws MalformedURLException { + List arr = new ArrayList(); + if (root != null) { + arr.add(root.toURI().toURL()); + } + for (Artifact a : deps) { + if (a.getFile() != null) { + arr.add(a.getFile().toURI().toURL()); + } + } + return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader()); + } +}