rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AOTLibrary.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 11 Jan 2015 09:54:37 +0100
changeset 1769 f6057dc5922c
parent 1765 6e50103c0f1c
child 1787 ea12a3bb4b33
permissions -rw-r--r--
Honor ignoreBootClassPath attribute in all mojos
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 package org.apidesign.bck2brwsr.mojo;
    20 
    21 import java.io.File;
    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;
    27 import java.net.URL;
    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;
    50 
    51 /**
    52  *
    53  * @author Jaroslav Tulach
    54  * @since 0.12
    55  */
    56 @Mojo(name = "library",
    57     requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME,
    58     defaultPhase = LifecyclePhase.PACKAGE
    59 )
    60 public class AOTLibrary extends AbstractMojo {
    61     @Parameter(defaultValue = "${project}")
    62     private MavenProject prj;
    63 
    64     @Component
    65     private MavenProjectHelper projectHelper;
    66     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
    67     private File mainJar;    
    68     @Parameter(defaultValue = "${project.build.finalName}-min.js")
    69     private String minified;
    70     @Parameter(defaultValue = "${project.build.finalName}-debug.js")
    71     private String debug;
    72     
    73     @Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}-bck2brwsr.jar")
    74     private File aotJar;
    75     
    76     @Parameter
    77     private String[] exports;
    78     
    79     @Parameter
    80     private String[] aotDeps;
    81 
    82     @Parameter(defaultValue = "true")
    83     private boolean ignoreBootClassPath;
    84 
    85     @Override
    86     public void execute() throws MojoExecutionException, MojoFailureException {
    87         URLClassLoader loader;
    88         try {
    89             loader = buildClassLoader(mainJar, prj.getArtifacts());
    90         } catch (MalformedURLException ex) {
    91             throw new MojoFailureException("Can't initialize classloader");
    92         }
    93 
    94         try {
    95             Manifest m = new Manifest();
    96             if (!"false".equals(minified)) {
    97                 Attributes attr = new Attributes();
    98                 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
    99                 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
   100                 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
   101                 attr.putValue("Bck2BrwsrMinified", "true");
   102                 m.getEntries().put(minified, attr);
   103             }
   104             if (!"false".equals(debug)) {
   105                 Attributes attr = new Attributes();
   106                 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
   107                 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
   108                 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
   109                 attr.putValue("Bck2BrwsrDebug", "true");
   110                 m.getEntries().put(debug, attr);
   111             }
   112             
   113             if (aotDeps != null) {
   114                 for (Artifact a : prj.getArtifacts()) {
   115                     if (!matches(aotDeps, a)) {
   116                         continue;
   117                     }
   118                     
   119                     {
   120                         Attributes attr = new Attributes();
   121                         attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
   122                         attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
   123                         attr.putValue("Bck2BrwsrVersion", a.getVersion());
   124                         attr.putValue("Bck2BrwsrDebug", "true");
   125                         m.getEntries().put(artifactName(a, true), attr);
   126                     }
   127                     {
   128                         Attributes attr = new Attributes();
   129                         attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
   130                         attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
   131                         attr.putValue("Bck2BrwsrVersion", a.getVersion());
   132                         attr.putValue("Bck2BrwsrMinified", "true");
   133                         m.getEntries().put(artifactName(a, false), attr);
   134                     }
   135                 }
   136             }
   137             
   138             FileOutputStream fos = new FileOutputStream(this.aotJar);
   139             JarOutputStream os = new JarOutputStream(fos, m);
   140 
   141             if (!"false".equals(debug)) {
   142                 os.putNextEntry(new JarEntry(debug));
   143                 Writer w = new OutputStreamWriter(os, "UTF-8");
   144                 configureMain(loader).
   145                     obfuscation(ObfuscationLevel.NONE).
   146                     generate(w);
   147                 w.flush();
   148                 os.closeEntry();
   149             }
   150             if (!"false".equals(minified)) {
   151                 os.putNextEntry(new JarEntry(minified));
   152             
   153                 Writer w = new OutputStreamWriter(os, "UTF-8");
   154                 configureMain(loader).
   155                     obfuscation(ObfuscationLevel.FULL).
   156                     generate(w);
   157                 w.flush();
   158                 os.closeEntry();
   159             }
   160             
   161             if (aotDeps != null) {
   162                 for (Artifact a : prj.getArtifacts()) {
   163                     if (!matches(aotDeps, a)) {
   164                         continue;
   165                     }
   166                     getLog().info("Generating bck2brwsr for " + a.getFile());
   167                     Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader, ignoreBootClassPath);
   168                     if (exports != null) {
   169                         for (String e : exports) {
   170                             c = c.addExported(e.replace('.', '/'));
   171                         }
   172                     }
   173                     {
   174                         os.putNextEntry(new JarEntry(artifactName(a, true)));
   175                         Writer w = new OutputStreamWriter(os, "UTF-8");
   176                         c.
   177                             obfuscation(ObfuscationLevel.NONE).
   178                             generate(w);
   179                         w.flush();
   180                         os.closeEntry();
   181                     }
   182                     {
   183                         os.putNextEntry(new JarEntry(artifactName(a, false)));
   184 
   185                         Writer w = new OutputStreamWriter(os, "UTF-8");
   186                         c.
   187                             obfuscation(ObfuscationLevel.FULL).
   188                             generate(w);
   189                         w.flush();
   190                         os.closeEntry();
   191                     }                    
   192                 }
   193             }
   194             os.close();
   195             
   196             projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar);
   197         } catch (IOException ex) {
   198             throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
   199         }
   200     }
   201 
   202     private Bck2Brwsr configureMain(URLClassLoader loader) throws IOException {
   203         Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader, ignoreBootClassPath);
   204         if (exports != null) {
   205             for (String e : exports) {
   206                 c = c.addExported(e.replace('.', '/'));
   207             }
   208         }
   209         return c;
   210     }
   211 
   212     private static String artifactName(Artifact a, boolean debug) {
   213         return a.getGroupId() + "-" + a.getArtifactId() + (debug ? "-debug.js" : "-min.js");
   214     }
   215 
   216     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   217         List<URL> arr = new ArrayList<URL>();
   218         if (root != null) {
   219             arr.add(root.toURI().toURL());
   220         }
   221         for (Artifact a : deps) {
   222             if (a.getFile() != null) {
   223                 arr.add(a.getFile().toURI().toURL());
   224             }
   225         }
   226         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   227     }
   228 
   229     private static boolean matches(String[] aotDeps, Artifact a) {
   230         for (String d : aotDeps) {
   231             String[] parts = d.split(":");
   232             for (int i = 0; i < parts.length; i++) {
   233                 if ("*".equals(parts[i])) {
   234                     parts[i] = null;
   235                 }
   236             }
   237             
   238             if (parts[0] != null && !parts[0].equals(a.getGroupId())) {
   239                 continue;
   240             }
   241             if (parts[1] != null && !parts[1].equals(a.getArtifactId())) {
   242                 continue;
   243             }
   244             if (parts.length > 2 && parts[2] != null && !parts[2].equals(a.getClassifier())) {
   245                 continue;
   246             }
   247             return true;
   248         }
   249         return false;
   250     }
   251 }