rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/BrwsrMojo.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 28 May 2013 21:49:11 +0200
changeset 1164 a55680f825fc
parent 1047 338e0ce0e2dd
child 1173 aa82f9de8e33
permissions -rw-r--r--
Need transitive runtime dependencies
     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 java.io.Closeable;
    21 import org.apache.maven.plugin.AbstractMojo;
    22 
    23 import java.io.File;
    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.plugins.annotations.ResolutionScope;
    37 import org.apache.maven.project.MavenProject;
    38 import org.apidesign.bck2brwsr.launcher.Launcher;
    39 
    40 /** Executes given HTML page in a browser. */
    41 @Mojo(
    42     name="brwsr",
    43     requiresDependencyResolution = ResolutionScope.RUNTIME,
    44     defaultPhase=LifecyclePhase.NONE
    45 )
    46 public class BrwsrMojo extends AbstractMojo {
    47     public BrwsrMojo() {
    48     }
    49     
    50     /** The identification of a launcher to use. Known values <code>fxbrwsr</code>, 
    51      * <code>bck2brwsr</code>, or 
    52      * name of an external process to execute.
    53      */
    54     @Parameter
    55     private String launcher;
    56     
    57     
    58     /** Resource to show as initial page */
    59     @Parameter
    60     private String startpage;
    61 
    62     @Parameter(defaultValue="${project}")
    63     private MavenProject prj;
    64     
    65     /** Root of the class files */
    66     @Parameter(defaultValue="${project.build.directory}/classes")
    67     private File classes;
    68     
    69     /** Root of all pages, and files, etc. */
    70     @Parameter
    71     private File directory;
    72 
    73     @Override
    74     public void execute() throws MojoExecutionException {
    75         if (startpage == null) {
    76             throw new MojoExecutionException("You have to provide a start page");
    77         }
    78         
    79         try {
    80             Closeable httpServer;
    81             if (directory != null) {
    82                 httpServer = Launcher.showDir(directory, startpage);
    83             } else {
    84                 URLClassLoader url = buildClassLoader(classes, prj.getArtifacts());
    85                 try {
    86                     httpServer = Launcher.showURL(launcher, url, startpage());
    87                 } catch (Exception ex) {
    88                     throw new MojoExecutionException("Can't open " + startpage(), ex);
    89                 }
    90             }
    91             System.in.read();
    92             httpServer.close();
    93         } catch (IOException ex) {
    94             throw new MojoExecutionException("Can't show the browser", ex);
    95         }
    96     }
    97     
    98     private String startpage() {
    99         return startpage;
   100     }
   101 
   102     private static URLClassLoader buildClassLoader(File root, Collection<Artifact> deps) throws MalformedURLException {
   103         List<URL> arr = new ArrayList<URL>();
   104         arr.add(root.toURI().toURL());
   105         for (Artifact a : deps) {
   106             final File f = a.getFile();
   107             if (f != null) {
   108                 arr.add(f.toURI().toURL());
   109             }
   110         }
   111         return new URLClassLoader(arr.toArray(new URL[0]), BrwsrMojo.class.getClassLoader());
   112     }
   113 }