rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AheadOfTime.java
changeset 1737 82fd3830167d
parent 1710 6db177c4f72c
child 1762 293838e72201
     1.1 --- a/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AheadOfTime.java	Fri Sep 26 13:42:15 2014 +0200
     1.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/AheadOfTime.java	Sat Dec 06 06:28:17 2014 +0100
     1.3 @@ -25,9 +25,15 @@
     1.4  import java.net.MalformedURLException;
     1.5  import java.net.URL;
     1.6  import java.net.URLClassLoader;
     1.7 +import java.nio.file.Files;
     1.8  import java.util.ArrayList;
     1.9  import java.util.Collection;
    1.10  import java.util.List;
    1.11 +import java.util.Map;
    1.12 +import java.util.jar.Attributes;
    1.13 +import java.util.jar.JarFile;
    1.14 +import java.util.jar.Manifest;
    1.15 +import java.util.zip.ZipEntry;
    1.16  import org.apache.maven.artifact.Artifact;
    1.17  import org.apache.maven.plugin.AbstractMojo;
    1.18  import org.apache.maven.plugin.MojoExecutionException;
    1.19 @@ -74,6 +80,9 @@
    1.20      @Parameter(defaultValue="${project.build.directory}/bck2brwsr.js")
    1.21      private File vm;
    1.22      
    1.23 +    @Parameter(defaultValue = "true")
    1.24 +    private boolean generateAotLibraries;
    1.25 +    
    1.26      /**
    1.27       * The obfuscation level for the generated JavaScript file.
    1.28       *
    1.29 @@ -109,10 +118,9 @@
    1.30                  continue;
    1.31              }
    1.32              try {
    1.33 -                getLog().info("Generating " + js);
    1.34                  aotLibrary(a, js , loader);
    1.35              } catch (IOException ex) {
    1.36 -                throw new MojoFailureException("Can't compile" + a.getFile(), ex);
    1.37 +                throw new MojoFailureException("Can't compile " + a.getFile(), ex);
    1.38              }
    1.39          }
    1.40          
    1.41 @@ -157,7 +165,39 @@
    1.42          }
    1.43      }
    1.44  
    1.45 -    private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException {
    1.46 +    private void aotLibrary(Artifact a, File js, URLClassLoader loader) throws IOException, MojoExecutionException {
    1.47 +        for (Artifact b : prj.getArtifacts()) {
    1.48 +            if ("bck2brwsr".equals(b.getClassifier())) { // NOI18N
    1.49 +                getLog().debug("Inspecting " + b.getFile());
    1.50 +                JarFile jf = new JarFile(b.getFile());
    1.51 +                Manifest man = jf.getManifest();
    1.52 +                for (Map.Entry<String, Attributes> entrySet : man.getEntries().entrySet()) {
    1.53 +                    String entryName = entrySet.getKey();
    1.54 +                    Attributes attr = entrySet.getValue();
    1.55 +                    
    1.56 +                    if (
    1.57 +                        a.getArtifactId().equals(attr.getValue("Bck2BrwsrArtifactId")) &&
    1.58 +                        a.getGroupId().equals(attr.getValue("Bck2BrwsrGroupId")) &&
    1.59 +                        a.getVersion().equals(attr.getValue("Bck2BrwsrVersion")) &&
    1.60 +                        (
    1.61 +                            obfuscation == ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrMinified"))
    1.62 +                            ||
    1.63 +                            obfuscation != ObfuscationLevel.FULL && "true".equals(attr.getValue("Bck2BrwsrDebug"))
    1.64 +                        )
    1.65 +                    ) {
    1.66 +                        getLog().info("Extracting " + js + " from " + b.getFile());
    1.67 +                        InputStream is = jf.getInputStream(new ZipEntry(entryName));
    1.68 +                        Files.copy(is, js.toPath());
    1.69 +                        is.close();
    1.70 +                        return;
    1.71 +                    }
    1.72 +                }
    1.73 +            }
    1.74 +        }
    1.75 +        if (!generateAotLibraries) {
    1.76 +            throw new MojoExecutionException("Not generating " + js + " and no precompiled version found!");
    1.77 +        }
    1.78 +        getLog().info("Generating " + js);
    1.79          FileWriter w = new FileWriter(js);
    1.80          Bck2Brwsr c = Bck2BrwsrJars.configureFrom(null, a.getFile(), loader);
    1.81          c.