rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/Java2JavaScript.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 18 Mar 2013 09:04:46 +0100
changeset 847 094e10a97511
parent 772 d382dacfd73f
child 861 f41a1fef7738
permissions -rw-r--r--
Modifying the static calculator sample to pre-compile all its project classes
     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 package org.apidesign.bck2brwsr.mojo;
    19 
    20 import org.apache.maven.plugin.AbstractMojo;
    21 
    22 import java.io.File;
    23 import java.io.FileWriter;
    24 import java.io.IOException;
    25 import java.net.MalformedURLException;
    26 import java.net.URL;
    27 import java.net.URLClassLoader;
    28 import java.util.ArrayList;
    29 import java.util.Collection;
    30 import java.util.List;
    31 import org.apache.maven.artifact.Artifact;
    32 import org.apache.maven.plugin.MojoExecutionException;
    33 import org.apache.maven.plugins.annotations.LifecyclePhase;
    34 import org.apache.maven.plugins.annotations.Mojo;
    35 import org.apache.maven.plugins.annotations.Parameter;
    36 import org.apache.maven.project.MavenProject;
    37 import org.apidesign.vm4brwsr.Bck2Brwsr;
    38 
    39 /** Compiles classes into JavaScript. */
    40 @Mojo(name="j2js", defaultPhase=LifecyclePhase.PROCESS_CLASSES)
    41 public class Java2JavaScript extends AbstractMojo {
    42     public Java2JavaScript() {
    43     }
    44     /** Root of the class files */
    45     @Parameter(defaultValue="${project.build.directory}/classes")
    46     private File classes;
    47     /** JavaScript file to generate */
    48     @Parameter
    49     private File javascript;
    50     
    51     /** Additional classes that should be pre-compiled into the javascript 
    52      * file. By default compiles all classes found under <code>classes</code>
    53      * directory and their transitive closure.
    54      */
    55     @Parameter
    56     private List<String> compileclasses;
    57     
    58     @Parameter(defaultValue="${project}")
    59     private MavenProject prj;
    60     
    61     
    62 
    63     @Override
    64     public void execute() throws MojoExecutionException {
    65         if (!classes.isDirectory()) {
    66             throw new MojoExecutionException("Can't find " + classes);
    67         }
    68 
    69         List<String> arr = new ArrayList<String>();
    70         long newest = collectAllClasses("", classes, arr);
    71         
    72         if (compileclasses != null) {
    73             arr.retainAll(compileclasses);
    74             arr.addAll(compileclasses);
    75         }
    76         
    77         if (javascript.lastModified() > newest) {
    78             return;
    79         }
    80 
    81         try {
    82             URLClassLoader url = buildClassLoader(classes, prj.getDependencyArtifacts());
    83             FileWriter w = new FileWriter(javascript);
    84             Bck2Brwsr.generate(w, url, arr.toArray(new String[0]));
    85             w.close();
    86         } catch (IOException ex) {
    87             throw new MojoExecutionException("Can't compile", ex);
    88         }
    89     }
    90 
    91     private static long collectAllClasses(String prefix, File toCheck, List<String> arr) {
    92         File[] files = toCheck.listFiles();
    93         if (files != null) {
    94             long newest = 0L;
    95             for (File f : files) {
    96                 long lastModified = collectAllClasses(prefix + f.getName() + "/", f, arr);
    97                 if (newest < lastModified) {
    98                     newest = lastModified;
    99                 }
   100             }
   101             return newest;
   102         } else if (toCheck.getName().endsWith(".class")) {
   103             final String cls = prefix.substring(0, prefix.length() - 7);
   104             arr.add(cls);
   105             return toCheck.lastModified();
   106         } else {
   107             return 0L;
   108         }
   109     }
   110 
   111     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   112         List<URL> arr = new ArrayList<URL>();
   113         arr.add(root.toURI().toURL());
   114         for (Artifact a : deps) {
   115             if (a.getFile() != null) {
   116                 arr.add(a.getFile().toURI().toURL());
   117             }
   118         }
   119         return new URLClassLoader(arr.toArray(new URL[0]), Java2JavaScript.class.getClassLoader());
   120     }
   121 }