View Javadoc

1   //========================================================================
2   //$Id: Jetty6RunWarExploded.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   * 
28   *  <p>
29   *  This goal is used to assemble your webapp into an exploded war and automatically deploy it to Jetty.
30   *  </p>
31   *  <p>
32   *  Once invoked, the plugin can be configured to run continuously, scanning for changes in the pom.xml and 
33   *  to WEB-INF/web.xml, WEB-INF/classes or WEB-INF/lib and hot redeploy when a change is detected. 
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-exploded-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-exploded
45   *@execute phase=package
46   */
47  public class Jetty6RunWarExploded extends AbstractJetty6Mojo
48  {
49  
50      
51      
52      /**
53       * The location of the war file.
54       * @parameter expression="${project.build.directory}/${project.build.finalName}"
55       * @required
56       */
57      private File webApp;
58  
59      
60     
61    
62     
63  
64      /**
65       * 
66       * @see org.mortbay.jetty.plugin.AbstractJettyMojo#checkPomConfiguration()
67       */
68      public void checkPomConfiguration() throws MojoExecutionException
69      {
70          return;
71      }
72  
73      /**
74       * @see org.mortbay.jetty.plugin.AbstractJettyMojo#configureScanner()
75       */
76      public void configureScanner() throws MojoExecutionException
77      {
78          final ArrayList scanList = new ArrayList();
79          scanList.add(getProject().getFile());
80          File webInfDir = new File(webApp,"WEB-INF");
81          scanList.add(new File(webInfDir, "web.xml"));
82          File jettyWebXmlFile = findJettyWebXmlFile(webInfDir);
83          if (jettyWebXmlFile != null)
84              scanList.add(jettyWebXmlFile);
85          File jettyEnvXmlFile = new File(webInfDir, "jetty-env.xml");
86          if (jettyEnvXmlFile.exists())
87              scanList.add(jettyEnvXmlFile);
88          scanList.add(new File(webInfDir, "classes"));
89          scanList.add(new File(webInfDir, "lib"));
90          setScanList(scanList);
91          
92          ArrayList listeners = new ArrayList();
93          listeners.add(new Scanner.BulkListener()
94          {
95              public void filesChanged(List changes)
96              {
97                  try
98                  {
99                      boolean reconfigure = changes.contains(getProject().getFile().getCanonicalPath());
100                     restartWebApp(reconfigure);
101                 }
102                 catch (Exception e)
103                 {
104                     getLog().error("Error reconfiguring/restarting webapp after change in watched files",e);
105                 }
106             }
107         });
108         setScannerListeners(listeners);
109     }
110 
111     
112     
113     
114     public void restartWebApp(boolean reconfigureScanner) throws Exception 
115     {
116         getLog().info("Restarting webapp");
117         getLog().debug("Stopping webapp ...");
118         webAppConfig.stop();
119         getLog().debug("Reconfiguring webapp ...");
120 
121         checkPomConfiguration();
122 
123         // check if we need to reconfigure the scanner,
124         // which is if the pom changes
125         if (reconfigureScanner)
126         {
127             getLog().info("Reconfiguring scanner after change to pom.xml ...");
128             ArrayList scanList = getScanList();
129             scanList.clear();
130             scanList.add(getProject().getFile());
131             File webInfDir = new File(webApp,"WEB-INF");
132             scanList.add(new File(webInfDir, "web.xml"));
133             File jettyWebXmlFile = findJettyWebXmlFile(webInfDir);
134             if (jettyWebXmlFile != null)
135                 scanList.add(jettyWebXmlFile);
136             File jettyEnvXmlFile = new File(webInfDir, "jetty-env.xml");
137             if (jettyEnvXmlFile.exists())
138                 scanList.add(jettyEnvXmlFile);
139             scanList.add(new File(webInfDir, "classes"));
140             scanList.add(new File(webInfDir, "lib"));
141             setScanList(scanList);
142             getScanner().setScanDirs(scanList);
143         }
144 
145         getLog().debug("Restarting webapp ...");
146         webAppConfig.start();
147         getLog().info("Restart completed.");
148     }
149 
150         
151     /* (non-Javadoc)
152      * @see org.mortbay.jetty.plugin.util.AbstractJettyMojo#finishConfigurationBeforeStart()
153      */
154     public void finishConfigurationBeforeStart() throws Exception
155     {
156         return;
157     }
158 
159     
160     
161     public void configureWebApplication () throws Exception
162     {
163         super.configureWebApplication();        
164         webAppConfig.setWar(webApp.getCanonicalPath());
165         webAppConfig.configure();
166     }
167     
168     public void execute () throws MojoExecutionException, MojoFailureException
169     {
170         super.execute();
171     }
172     
173 }