View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 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  
20  package org.eclipse.jetty.maven.plugin;
21  
22  import java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
27  import org.eclipse.jetty.annotations.AnnotationConfiguration;
28  import org.eclipse.jetty.util.IO;
29  import org.eclipse.jetty.util.resource.Resource;
30  import org.eclipse.jetty.util.thread.QueuedThreadPool;
31  
32  /**
33   * JettyEffectiveWebXml
34   *
35   * @goal effective-web-xml
36   * @requiresDependencyResolution test
37   * @execute phase="test-compile"
38   * @description Runs jetty on the unassembled webapp to generate the effective web.xml
39   */
40  public class JettyEffectiveWebXml extends JettyRunMojo
41  {
42      /**
43       * The target directory
44       * 
45       * @parameter expression="${project.build.directory}"
46       * @required
47       * @readonly
48       */
49      protected File target;
50      
51      /**
52       * The target directory
53       * 
54       * @parameter 
55       */
56      protected File effectiveWebXml;
57      
58      
59      protected boolean deleteOnExit = true;
60      
61  
62      /**
63       * @see org.apache.maven.plugin.Mojo#execute()
64       */
65      public void execute() throws MojoExecutionException, MojoFailureException
66      {
67          super.execute();
68      }
69      
70      
71      @Override
72      public void startJetty() throws MojoExecutionException
73      {
74          //Only do enough setup to be able to produce a quickstart-web.xml file 
75          
76          //if the user didn't nominate a file to generate into, pick the name and
77          //make sure that it is deleted on exit
78          if (effectiveWebXml == null)
79          {
80              deleteOnExit = true;
81              effectiveWebXml = new File(target, "effective-web.xml");
82              effectiveWebXml.deleteOnExit();
83          }
84          
85          Resource descriptor = Resource.newResource(effectiveWebXml);
86          
87          QueuedThreadPool tpool = null;
88          
89          try
90          {
91              printSystemProperties();
92  
93              //apply any config from a jetty.xml file first to our "fake" server instance
94              //TODO probably not necessary
95              applyJettyXml ();  
96  
97          
98              server.configureHandlers();
99                     
100             //ensure config of the webapp based on settings in plugin
101             configureWebApplication();
102             
103             
104             //set the webapp up to do very little other than generate the quickstart-web.xml
105             webApp.setCopyWebDir(false);
106             webApp.setCopyWebInf(false);
107             webApp.setGenerateQuickStart(true);
108     
109             if (!effectiveWebXml.getParentFile().exists())
110                 effectiveWebXml.getParentFile().mkdirs();
111             if (!effectiveWebXml.exists())
112                 effectiveWebXml.createNewFile();
113             
114             webApp.setQuickStartWebDescriptor(descriptor);
115             
116             server.addWebApplication(webApp);
117                        
118             //if our server has a thread pool associated we can do any annotation scanning multithreaded,
119             //otherwise scanning will be single threaded
120             tpool = server.getBean(QueuedThreadPool.class);
121             if (tpool != null)
122                 tpool.start();
123             else
124                 webApp.setAttribute(AnnotationConfiguration.MULTI_THREADED, Boolean.FALSE.toString());
125             
126              webApp.start(); //just enough to generate the quickstart           
127            
128         }
129         catch (Exception e)
130         {
131             throw new MojoExecutionException("Effective web.xml generation failed", e);
132         }
133         finally
134         {
135             try {webApp.stop();}catch (Exception x) {};
136             
137             try {if (tpool != null) tpool.stop();} catch (Exception x) {};
138         }
139          
140        
141         if (deleteOnExit)
142         {
143             try
144             {
145                 //just show the result in the log
146                 getLog().info(IO.toString(descriptor.getInputStream()));
147             }
148             catch (IOException e)
149             {
150                throw new MojoExecutionException("Unable to output effective web.xml", e);
151             }
152             
153         }
154         
155     }
156 }