rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AOTLibrary.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 06 Dec 2014 07:11:02 +0100
changeset 1739 e9b9d9ff0621
parent 1737 82fd3830167d
child 1742 0e949cf1b073
permissions -rw-r--r--
javaquery demo now uses only extracted dependencies because javaquery.api generates additional bck2brwsr js files for core annotations library
     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     @Override
    83     public void execute() throws MojoExecutionException, MojoFailureException {
    84         URLClassLoader loader;
    85         try {
    86             loader = buildClassLoader(mainJar, prj.getArtifacts());
    87         } catch (MalformedURLException ex) {
    88             throw new MojoFailureException("Can't initialize classloader");
    89         }
    90 
    91         try {
    92             Manifest m = new Manifest();
    93             {
    94                 Attributes attr = new Attributes();
    95                 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
    96                 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
    97                 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
    98                 attr.putValue("Bck2BrwsrMinified", "true");
    99                 m.getEntries().put(minified, attr);
   100             }
   101             {
   102                 Attributes attr = new Attributes();
   103                 attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
   104                 attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
   105                 attr.putValue("Bck2BrwsrVersion", prj.getVersion());
   106                 attr.putValue("Bck2BrwsrDebug", "true");
   107                 m.getEntries().put(debug, attr);
   108             }
   109             
   110             if (aotDeps != null) {
   111                 for (Artifact a : prj.getArtifacts()) {
   112                     if (!matches(aotDeps, a)) {
   113                         continue;
   114                     }
   115                     
   116                     {
   117                         Attributes attr = new Attributes();
   118                         attr.putValue("Bck2BrwsrArtifactId", a.getArtifactId());
   119                         attr.putValue("Bck2BrwsrGroupId", a.getGroupId());
   120                         attr.putValue("Bck2BrwsrVersion", a.getVersion());
   121                         attr.putValue("Bck2BrwsrMinified", "true");
   122                         m.getEntries().put(artifactName(a, true), attr);
   123                     }
   124                     {
   125                         Attributes attr = new Attributes();
   126                         attr.putValue("Bck2BrwsrArtifactId", prj.getArtifactId());
   127                         attr.putValue("Bck2BrwsrGroupId", prj.getGroupId());
   128                         attr.putValue("Bck2BrwsrVersion", prj.getVersion());
   129                         attr.putValue("Bck2BrwsrDebug", "true");
   130                         m.getEntries().put(artifactName(a, false), attr);
   131                     }
   132                         }
   133             }
   134             
   135             FileOutputStream fos = new FileOutputStream(this.aotJar);
   136             JarOutputStream os = new JarOutputStream(fos, m);
   137 
   138             Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, mainJar, loader);
   139             if (exports != null) {
   140                 for (String e : exports) {
   141                     c = c.addExported(e.replace('.', '/'));
   142                 }
   143             }
   144             {
   145                 os.putNextEntry(new JarEntry(debug));
   146                 Writer w = new OutputStreamWriter(os);
   147                 c.
   148                     obfuscation(ObfuscationLevel.NONE).
   149                     generate(w);
   150                 w.flush();
   151                 os.closeEntry();
   152             }
   153             {
   154                 os.putNextEntry(new JarEntry(minified));
   155             
   156                 Writer w = new OutputStreamWriter(os);
   157                 c.
   158                     obfuscation(ObfuscationLevel.FULL).
   159                     generate(w);
   160                 w.flush();
   161                 os.closeEntry();
   162             }
   163             
   164             if (aotDeps != null) {
   165                 for (Artifact a : prj.getArtifacts()) {
   166                     if (!matches(aotDeps, a)) {
   167                         continue;
   168                     }
   169                     {
   170                         os.putNextEntry(new JarEntry(artifactName(a, true)));
   171                         Writer w = new OutputStreamWriter(os);
   172                         c.
   173                             obfuscation(ObfuscationLevel.NONE).
   174                             generate(w);
   175                         w.flush();
   176                         os.closeEntry();
   177                     }
   178                     {
   179                         os.putNextEntry(new JarEntry(artifactName(a, false)));
   180 
   181                         Writer w = new OutputStreamWriter(os);
   182                         c.
   183                             obfuscation(ObfuscationLevel.FULL).
   184                             generate(w);
   185                         w.flush();
   186                         os.closeEntry();
   187                     }                    
   188                 }
   189             }
   190             os.close();
   191             
   192             projectHelper.attachArtifact(prj, "jar", "bck2brwsr", aotJar);
   193         } catch (IOException ex) {
   194             throw new MojoFailureException("Cannot generate script for " + mainJar, ex);
   195         }
   196     }
   197 
   198     private static String artifactName(Artifact a, boolean debug) {
   199         return a.getGroupId() + "-" + a.getArtifactId() + (debug ? "-debug.js" : "-min.js");
   200     }
   201 
   202     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   203         List<URL> arr = new ArrayList<URL>();
   204         if (root != null) {
   205             arr.add(root.toURI().toURL());
   206         }
   207         for (Artifact a : deps) {
   208             if (a.getFile() != null) {
   209                 arr.add(a.getFile().toURI().toURL());
   210             }
   211         }
   212         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   213     }
   214 
   215     private static boolean matches(String[] aotDeps, Artifact a) {
   216         for (String d : aotDeps) {
   217             String[] parts = d.split(":");
   218             for (int i = 0; i < parts.length; i++) {
   219                 if ("*".equals(parts[i])) {
   220                     parts[i] = null;
   221                 }
   222             }
   223             
   224             if (parts[0] != null && !parts[0].equals(a.getGroupId())) {
   225                 continue;
   226             }
   227             if (parts[1] != null && !parts[1].equals(a.getArtifactId())) {
   228                 continue;
   229             }
   230             if (parts.length > 2 && parts[2] != null && !parts[2].equals(a.getClassifier())) {
   231                 continue;
   232             }
   233             return true;
   234         }
   235         return false;
   236     }
   237 }