View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 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.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.eclipse.jetty.util.PathWatcher;
27  import org.eclipse.jetty.util.PathWatcher.PathWatchEvent;
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 runs continuously and can be configured to scan for changes in the project and to the
35   *  war file and automatically perform a hot redeploy when necessary. 
36   *  </p>
37   *  <p>
38   *  You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
39   *  This can be used, for example, to deploy a static webapp that is not part of your maven build. 
40   *  </p>
41   * 
42   * @goal run-war
43   * @requiresDependencyResolution compile+runtime
44   * @execute phase="package"
45   * @description Runs jetty on a war file
46   *
47   */
48  public class JettyRunWarMojo extends AbstractJettyMojo
49  {
50  
51      /**
52       * The location of the war file.
53       * @parameter default-value="${project.build.directory}/${project.build.finalName}.war"
54       * @required
55       */
56      private File war;
57  
58      
59      /**
60       * @see org.apache.maven.plugin.Mojo#execute()
61       */
62      public void execute() throws MojoExecutionException, MojoFailureException
63      {
64          super.execute();  
65      }
66  
67  
68      @Override
69      public void finishConfigurationBeforeStart() throws Exception
70      {
71          server.setStopAtShutdown(true); //as we will normally be stopped with a cntrl-c, ensure server stopped 
72          super.finishConfigurationBeforeStart();
73      }
74  
75  
76      
77      public void configureWebApplication () throws Exception
78      {
79          super.configureWebApplication();
80          
81          webApp.setWar(war.getCanonicalPath());
82      }
83   
84  
85  
86      
87      /**
88       * @see AbstractJettyMojo#checkPomConfiguration()
89       */
90      public void checkPomConfiguration() throws MojoExecutionException
91      {
92         return;        
93      }
94  
95  
96  
97      
98      /**
99       * @see AbstractJettyMojo#configureScanner()
100      */
101     public void configureScanner() throws MojoExecutionException
102     {
103         scanner.watch(project.getFile().toPath());
104         scanner.watch(war.toPath());
105 
106         scanner.addListener(new PathWatcher.EventListListener()
107         {
108 
109             @Override
110             public void onPathWatchEvents(List<PathWatchEvent> events)
111             {
112                 try
113                 {
114                     boolean reconfigure = false;
115                     for (PathWatchEvent e:events)
116                     {
117                         if (e.getPath().equals(project.getFile().toPath()))
118                         {
119                             reconfigure = true;
120                             break;
121                         }
122                     }
123                     restartWebApp(reconfigure);
124                 }
125                 catch (Exception e)
126                 {
127                     getLog().error("Error reconfiguring/restarting webapp after change in watched files",e);
128                 }
129             }
130         });
131     }
132 
133 
134     
135     
136     /** 
137      * @see org.eclipse.jetty.maven.plugin.AbstractJettyMojo#restartWebApp(boolean)
138      */
139     public void restartWebApp(boolean reconfigureScanner) throws Exception 
140     {
141         getLog().info("Restarting webapp ...");
142         getLog().debug("Stopping webapp ...");
143         stopScanner();
144         webApp.stop();
145         getLog().debug("Reconfiguring webapp ...");
146 
147         checkPomConfiguration();
148 
149         // check if we need to reconfigure the scanner,
150         // which is if the pom changes
151         if (reconfigureScanner)
152         {
153             getLog().info("Reconfiguring scanner after change to pom.xml ...");
154             scanner.reset();
155             configureScanner();
156         }
157 
158         getLog().debug("Restarting webapp ...");
159         webApp.start();
160         startScanner();
161         getLog().info("Restart completed.");
162     }
163 
164 }