View Javadoc

    1   //========================================================================
    2   //$Id: Jetty6RunWar.java 3591 2008-09-03 21:31:12Z jesse $
    3   //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
    4   //------------------------------------------------------------------------
    5   //Licensed under the Apache License, Version 2.0 (the "License");
    6   //you may not use this file except in compliance with the License.
    7   //You may obtain a copy of the License at
    8   //http://www.apache.org/licenses/LICENSE-2.0
    9   //Unless required by applicable law or agreed to in writing, software
    10  //distributed under the License is distributed on an "AS IS" BASIS,
    11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  //See the License for the specific language governing permissions and
    13  //limitations under the License.
    14  //========================================================================
    15
    16  package org.mortbay.jetty.plugin;
    17
    18  import java.io.File;
    19  import java.util.ArrayList;
    20  import java.util.List;
    21
    22  import org.apache.maven.plugin.MojoExecutionException;
    23  import org.apache.maven.plugin.MojoFailureException;
    24  import org.mortbay.util.Scanner;
    25
    26  /**
    27   * <p>
    28   *  This goal is used to assemble your webapp into a war and automatically deploy it to Jetty.
    29   *  </p>
    30   *  <p>
    31   *  Once invoked, the plugin can be configured to run continuously, scanning for changes in the project and to the
    32   *  war file and automatically performing a
    33   *  hot redeploy when necessary.
    34   *  </p>
    35   *  <p>
    36   *  You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
    37   *  This can be used, for example, to deploy a static webapp that is not part of your maven build.
    38   *  </p>
    39   *  <p>
    40   *  There is a <a href="run-war-mojo.html">reference guide</a> to the configuration parameters for this plugin, and more detailed information
    41   *  with examples in the <a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin/">Configuration Guide</a>.
    42   *  </p>
    43   *
    44   * @goal run-war
    45   * @requiresDependencyResolution runtime
    46   * @execute phase="package"
    47   * @description Runs jetty6 on a war file
    48   *
    49   */
    50  public class Jetty6RunWar extends AbstractJetty6Mojo
    51  {
    52
    53      /**
    54       * The location of the war file.
    55       * @parameter expression="${project.build.directory}/${project.build.finalName}.war"
    56       * @required
    57       */
    58      private File webApp;
    59
    60
    61
    62      /**
    63       * @see org.apache.maven.plugin.Mojo#execute()
    64       */
    65      public void execute() throws MojoExecutionException, MojoFailureException
    66      {
    67          super.execute();
    68      }
    69
    70
    71
    72      public void configureWebApplication () throws Exception
    73      {
    74          super.configureWebApplication();
    75
    76          webAppConfig.setWar(webApp.getCanonicalPath());
    77          webAppConfig.configure();
    78      }
    79
    80
    81
    82      /**
    83       * @see org.mortbay.jetty.plugin.AbstractJettyMojo#checkPomConfiguration()
    84       */
    85      public void checkPomConfiguration() throws MojoExecutionException
    86      {
    87         return;
    88      }
    89
    90
    91
    92      /* (non-Javadoc)
    93       * @see org.mortbay.jetty.plugin.util.AbstractJettyMojo#configureScanner()
    94       */
    95      public void configureScanner() throws MojoExecutionException
    96      {
    97          final ArrayList scanList = new ArrayList();
    98          scanList.add(getProject().getFile());
    99          scanList.add(webApp);
    100         setScanList(scanList);
    101
    102         ArrayList listeners = new ArrayList();
    103         listeners.add(new Scanner.BulkListener()
    104         {
    105             public void filesChanged(List changes)
    106             {
    107                 try
    108                 {
    109                     boolean reconfigure = changes.contains(getProject().getFile().getCanonicalPath());
    110                     restartWebApp(reconfigure);
    111                 }
    112                 catch (Exception e)
    113                 {
    114                     getLog().error("Error reconfiguring/restarting webapp after change in watched files",e);
    115                 }
    116             }
    117         });
    118         setScannerListeners(listeners);
    119
    120     }
    121
    122
    123
    124
    125     public void restartWebApp(boolean reconfigureScanner) throws Exception
    126     {
    127         getLog().info("Restarting webapp ...");
    128         getLog().debug("Stopping webapp ...");
    129         webAppConfig.stop();
    130         getLog().debug("Reconfiguring webapp ...");
    131
    132         checkPomConfiguration();
    133
    134         // check if we need to reconfigure the scanner,
    135         // which is if the pom changes
    136         if (reconfigureScanner)
    137         {
    138             getLog().info("Reconfiguring scanner after change to pom.xml ...");
    139             ArrayList scanList = getScanList();
    140             scanList.clear();
    141             scanList.add(getProject().getFile());
    142             scanList.add(webApp);
    143             setScanList(scanList);
    144             getScanner().setScanDirs(scanList);
    145         }
    146
    147         getLog().debug("Restarting webapp ...");
    148         webAppConfig.start();
    149         getLog().info("Restart completed.");
    150     }
    151
    152
    153     /**
    154      * @see org.mortbay.jetty.plugin.AbstractJettyMojo#finishConfigurationBeforeStart()
    155      */
    156     public void finishConfigurationBeforeStart()
    157     {
    158         return;
    159     }
    160
    161
    162
    163
    164 }