View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.maven.plugin;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.eclipse.jetty.util.Scanner;
28  
29  /**
30   * <p>
31   *  This goal is used to assemble your webapp into a war and automatically deploy it to Jetty.
32   *  </p>
33   *  <p>
34   *  Once invoked, the plugin can be configured to run continuously, scanning for changes in the project and to the
35   *  war file and automatically performing a 
36   *  hot redeploy when necessary. 
37   *  </p>
38   *  <p>
39   *  You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
40   *  This can be used, for example, to deploy a static webapp that is not part of your maven build. 
41   *  </p>
42   *  <p>
43   *  There is a <a href="run-war-mojo.html">reference guide</a> to the configuration parameters for this plugin, and more detailed information
44   *  with examples in the <a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin/">Configuration Guide</a>.
45   *  </p>
46   * 
47   * @goal run-war
48   * @requiresDependencyResolution compile+runtime
49   * @execute phase="package"
50   * @description Runs jetty on a war file
51   *
52   */
53  public class JettyRunWarMojo extends AbstractJettyMojo
54  {
55  
56      /**
57       * The location of the war file.
58       * @parameter expression="${project.build.directory}/${project.build.finalName}.war"
59       * @required
60       */
61      private File war;
62  
63      
64      /**
65       * @see org.apache.maven.plugin.Mojo#execute()
66       */
67      public void execute() throws MojoExecutionException, MojoFailureException
68      {
69          super.execute();  
70      }
71  
72  
73  
74      
75      public void configureWebApplication () throws Exception
76      {
77          super.configureWebApplication();
78          
79          webApp.setWar(war.getCanonicalPath());
80      }
81   
82  
83  
84      
85      /**
86       * @see AbstractJettyMojo#checkPomConfiguration()
87       */
88      public void checkPomConfiguration() throws MojoExecutionException
89      {
90         return;        
91      }
92  
93  
94  
95      
96      /**
97       * @see AbstractJettyMojo#configureScanner()
98       */
99      public void configureScanner() throws MojoExecutionException
100     {
101         scanList = new ArrayList();
102         scanList.add(project.getFile());
103         scanList.add(war);
104         
105         scannerListeners = new ArrayList();
106         scannerListeners.add(new Scanner.BulkListener()
107         {
108             public void filesChanged(List changes)
109             {
110                 try
111                 {
112                     boolean reconfigure = changes.contains(project.getFile().getCanonicalPath());
113                     restartWebApp(reconfigure);
114                 }
115                 catch (Exception e)
116                 {
117                     getLog().error("Error reconfiguring/restarting webapp after change in watched files",e);
118                 }
119             }
120         });
121     }
122 
123 
124     
125     
126     /** 
127      * @see org.eclipse.jetty.maven.plugin.AbstractJettyMojo#restartWebApp(boolean)
128      */
129     public void restartWebApp(boolean reconfigureScanner) throws Exception 
130     {
131         getLog().info("Restarting webapp ...");
132         getLog().debug("Stopping webapp ...");
133         webApp.stop();
134         getLog().debug("Reconfiguring webapp ...");
135 
136         checkPomConfiguration();
137 
138         // check if we need to reconfigure the scanner,
139         // which is if the pom changes
140         if (reconfigureScanner)
141         {
142             getLog().info("Reconfiguring scanner after change to pom.xml ...");
143             scanList.clear();
144             scanList.add(project.getFile());
145             scanList.add(war);
146             scanner.setScanDirs(scanList);
147         }
148 
149         getLog().debug("Restarting webapp ...");
150         webApp.start();
151         getLog().info("Restart completed.");
152     }
153 
154 }