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