View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2012 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  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugin.MojoFailureException;
29  import org.eclipse.jetty.util.Scanner;
30  import org.eclipse.jetty.util.resource.Resource;
31  import org.eclipse.jetty.xml.XmlConfiguration;
32  
33  /**
34   * <p>
35   *  This goal is used to assemble your webapp into a war and automatically deploy it to Jetty.
36   *  </p>
37   *  <p>
38   *  Once invoked, the plugin can be configured to run continuously, scanning for changes in the project and to the
39   *  war file and automatically performing a 
40   *  hot redeploy when necessary. 
41   *  </p>
42   *  <p>
43   *  You may also specify the location of a jetty.xml file whose contents will be applied before any plugin configuration.
44   *  This can be used, for example, to deploy a static webapp that is not part of your maven build. 
45   *  </p>
46   *  <p>
47   *  There is a <a href="run-war-mojo.html">reference guide</a> to the configuration parameters for this plugin, and more detailed information
48   *  with examples in the <a href="http://docs.codehaus.org/display/JETTY/Maven+Jetty+Plugin/">Configuration Guide</a>.
49   *  </p>
50   * 
51   * @goal run-war
52   * @requiresDependencyResolution compile+runtime
53   * @execute phase="package"
54   * @description Runs jetty on a war file
55   *
56   */
57  public class JettyRunWarMojo extends AbstractJettyMojo
58  {
59  
60      /**
61       * The location of the war file.
62       * @parameter expression="${project.build.directory}/${project.build.finalName}.war"
63       * @required
64       */
65      private File war;
66  
67      
68      /**
69       * @see org.apache.maven.plugin.Mojo#execute()
70       */
71      public void execute() throws MojoExecutionException, MojoFailureException
72      {
73          super.execute();  
74      }
75  
76  
77  
78      
79      public void configureWebApplication () throws Exception
80      {
81          super.configureWebApplication();
82          
83          webApp.setWar(war.getCanonicalPath());
84      }
85   
86  
87  
88      
89      /**
90       * @see org.mortbay.jetty.plugin.AbstractJettyMojo#checkPomConfiguration()
91       */
92      public void checkPomConfiguration() throws MojoExecutionException
93      {
94         return;        
95      }
96  
97  
98  
99      
100     /**
101      * @see org.eclipse.jetty.server.plugin.AbstractJettyMojo#configureScanner()
102      */
103     public void configureScanner() throws MojoExecutionException
104     {
105         scanList = new ArrayList();
106         scanList.add(project.getFile());
107         scanList.add(war);
108         
109         scannerListeners = new ArrayList();
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             scanList.add(war);
150             scanner.setScanDirs(scanList);
151         }
152 
153         getLog().debug("Restarting webapp ...");
154         webApp.start();
155         getLog().info("Restart completed.");
156     }
157 
158 }