rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java
branchclosure
changeset 1615 80e39583b35d
parent 1371 fd2d4ca28bd3
child 1787 ea12a3bb4b33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/mojo/src/main/java/org/apidesign/bck2brwsr/mojo/ShowMojo.java	Mon Jun 09 09:38:03 2014 +0200
     1.3 @@ -0,0 +1,84 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.mojo;
    1.22 +
    1.23 +import java.io.Closeable;
    1.24 +import org.apache.maven.plugin.AbstractMojo;
    1.25 +
    1.26 +import java.io.File;
    1.27 +import java.io.IOException;
    1.28 +import java.net.MalformedURLException;
    1.29 +import java.net.URL;
    1.30 +import java.net.URLClassLoader;
    1.31 +import java.util.ArrayList;
    1.32 +import java.util.Collection;
    1.33 +import java.util.List;
    1.34 +import org.apache.maven.artifact.Artifact;
    1.35 +import org.apache.maven.model.Resource;
    1.36 +import org.apache.maven.plugin.MojoExecutionException;
    1.37 +import org.apache.maven.plugins.annotations.LifecyclePhase;
    1.38 +import org.apache.maven.plugins.annotations.Mojo;
    1.39 +import org.apache.maven.plugins.annotations.Parameter;
    1.40 +import org.apache.maven.plugins.annotations.ResolutionScope;
    1.41 +import org.apache.maven.project.MavenProject;
    1.42 +import org.apidesign.bck2brwsr.launcher.Launcher;
    1.43 +
    1.44 +/** Executes given HTML page in a browser. */
    1.45 +@Mojo(
    1.46 +    name="show",
    1.47 +    requiresDependencyResolution = ResolutionScope.RUNTIME,
    1.48 +    defaultPhase=LifecyclePhase.NONE
    1.49 +)
    1.50 +public class ShowMojo extends AbstractMojo {
    1.51 +    public ShowMojo() {
    1.52 +    }
    1.53 +    
    1.54 +    /** The identification of a launcher to use. Known values <code>fxbrwsr</code>, 
    1.55 +     * <code>bck2brwsr</code>, or 
    1.56 +     * name of an external process to execute.
    1.57 +     */
    1.58 +    @Parameter
    1.59 +    private String launcher;
    1.60 +    
    1.61 +    
    1.62 +    /** Resource to show as initial page */
    1.63 +    @Parameter
    1.64 +    private String startpage;
    1.65 +
    1.66 +    /** Root of all pages, and files, etc. */
    1.67 +    @Parameter
    1.68 +    private File directory;
    1.69 +    
    1.70 +    @Override
    1.71 +    public void execute() throws MojoExecutionException {
    1.72 +        if (startpage == null) {
    1.73 +            throw new MojoExecutionException("You have to provide a start page");
    1.74 +        }
    1.75 +        if (directory == null) {
    1.76 +            throw new MojoExecutionException("You have to provide a root directory");
    1.77 +        }
    1.78 +        try {
    1.79 +            Closeable httpServer;
    1.80 +            httpServer = Launcher.showDir(launcher, directory, null, startpage);
    1.81 +            System.in.read();
    1.82 +            httpServer.close();
    1.83 +        } catch (IOException ex) {
    1.84 +            throw new MojoExecutionException("Can't show the browser", ex);
    1.85 +        }
    1.86 +    }
    1.87 +}