View Javadoc

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