View Javadoc

1   package org.apache.tomcat.maven.plugin.tomcat7.deploy;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.tomcat.maven.common.deployer.TomcatManagerException;
24  import org.apache.tomcat.maven.plugin.tomcat7.AbstractWarCatalinaMojo;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.net.URL;
29  
30  /**
31   * Deploy a WAR to Tomcat.
32   *
33   * @author Mark Hobson <markhobson@gmail.com>
34   * @version $Id: AbstractDeployMojo.html 1305321 2012-03-26 12:01:12Z olamy $
35   */
36  public abstract class AbstractDeployMojo
37      extends AbstractWarCatalinaMojo
38  {
39      // ----------------------------------------------------------------------
40      // Mojo Parameters
41      // ----------------------------------------------------------------------
42  
43      /**
44       * The deployment mode to use. This must be either <code>war</code> to deploy the war, <code>context</code> to
45       * deploy the context XML file, or <code>both</code> to deploy the war with the context XML file.
46       *
47       * @parameter expression = "${maven.tomcat.mode}" default-value = "war"
48       * @required
49       */
50      private String mode;
51  
52      /**
53       * The path of the Tomcat context XML file. This is not used for war deployment mode.
54       *
55       * @parameter expression = "${project.build.directory}/${project.build.finalName}/META-INF/context.xml"
56       */
57      private File contextFile;
58  
59      /**
60       * Whether Tomcat should automatically undeploy webapps that already exist when deploying.
61       *
62       * @parameter expression = "${maven.tomcat.update}" default-value = "false"
63       * @required
64       */
65      private boolean update;
66  
67      /**
68       * The Tomcat webapp tag name to use.
69       *
70       * @parameter expression = "${maven.tomcat.tag}"
71       */
72      private String tag;
73  
74      // ----------------------------------------------------------------------
75      // Protected Methods
76      // ----------------------------------------------------------------------
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      public void invokeManager()
83          throws MojoExecutionException, TomcatManagerException, IOException
84      {
85          if ( "war".equals( mode ) )
86          {
87              deployWar();
88          }
89          else if ( "context".equals( mode ) )
90          {
91              deployContext();
92          }
93          else if ( "both".equals( mode ) )
94          {
95              deployWarAndContext();
96          }
97          else
98          {
99              throw new MojoExecutionException( messagesProvider.getMessage( "AbstractDeployMojo.unknownMode", mode ) );
100         }
101     }
102 
103     /**
104      * Gets the Tomcat WAR file. This may be a file or a directory depending on the deployment mode.
105      *
106      * @return the Tomcat WAR file.
107      */
108     protected abstract File getWarFile();
109 
110     /**
111      * Ensures that the Tomcat WAR file exists and is the correct type for the deployment mode.
112      *
113      * @throws org.apache.maven.plugin.MojoExecutionException
114      *          if the WAR file does not exist or is not the correct type for the deployment mode
115      */
116     protected abstract void validateWarFile()
117         throws MojoExecutionException;
118 
119     /**
120      * Gets the Tomcat context XML file.
121      *
122      * @return the Tomcat context XML file.
123      */
124     protected File getContextFile()
125     {
126         return contextFile;
127     }
128 
129     /**
130      * Ensures that the Tomcat context XML file exists and is indeed a file.
131      *
132      * @throws org.apache.maven.plugin.MojoExecutionException
133      *          if the context file does not exist or is not a file
134      */
135     protected void validateContextFile()
136         throws MojoExecutionException
137     {
138         if ( !contextFile.exists() || !contextFile.isFile() )
139         {
140             throw new MojoExecutionException(
141                 messagesProvider.getMessage( "AbstractDeployMojo.missingContext", contextFile.getPath() ) );
142         }
143     }
144 
145     /**
146      * Gets whether Tomcat should automatically undeploy webapps that already exist when deploying.
147      *
148      * @return whether Tomcat should automatically undeploy webapps that already exist when deploying
149      */
150     protected boolean isUpdate()
151     {
152         return update;
153     }
154 
155     /**
156      * Gets the Tomcat webapp tag name to use.
157      *
158      * @return the Tomcat webapp tag name to use
159      */
160     protected String getTag()
161     {
162         return tag;
163     }
164 
165     /**
166      * Deploys the WAR to Tomcat.
167      *
168      * @throws org.apache.maven.plugin.MojoExecutionException
169      *                             if there was a problem locating the WAR
170      * @throws org.apache.tomcat.maven.common.deployer.TomcatManagerException
171      *                             if the Tomcat manager request fails
172      * @throws java.io.IOException if an i/o error occurs
173      */
174     protected void deployWar()
175         throws MojoExecutionException, TomcatManagerException, IOException
176     {
177         validateWarFile();
178 
179         getLog().info( messagesProvider.getMessage( "AbstractDeployMojo.deployingWar", getDeployedURL() ) );
180 
181         URL warURL = getWarFile().toURL();
182         log( getManager().deploy( getPath(), warURL, isUpdate(), getTag() ).getHttpResponseBody() );
183     }
184 
185     /**
186      * Deploys the context XML file to Tomcat.
187      *
188      * @throws org.apache.maven.plugin.MojoExecutionException
189      *                             if there was a problem locating the context XML file
190      * @throws org.apache.tomcat.maven.common.deployer.TomcatManagerException
191      *                             if the Tomcat manager request fails
192      * @throws java.io.IOException if an i/o error occurs
193      */
194     protected void deployContext()
195         throws MojoExecutionException, TomcatManagerException, IOException
196     {
197         validateContextFile();
198 
199         getLog().info( messagesProvider.getMessage( "AbstractDeployMojo.deployingContext", getDeployedURL() ) );
200 
201         URL contextURL = getContextFile().toURL();
202         log( getManager().deployContext( getPath(), contextURL, isUpdate(), getTag() ).getHttpResponseBody() );
203     }
204 
205     /**
206      * Deploys the WAR and context XML file to Tomcat.
207      *
208      * @throws org.apache.maven.plugin.MojoExecutionException
209      *                             if there was a problem locating either the WAR or the context XML file
210      * @throws org.apache.tomcat.maven.common.deployer.TomcatManagerException
211      *                             if the Tomcat manager request fails
212      * @throws java.io.IOException if an i/o error occurs
213      */
214     protected void deployWarAndContext()
215         throws MojoExecutionException, TomcatManagerException, IOException
216     {
217         validateWarFile();
218         validateContextFile();
219 
220         getLog().info( messagesProvider.getMessage( "AbstractDeployMojo.deployingWarContext", getDeployedURL() ) );
221 
222         URL warURL = getWarFile().toURL();
223         URL contextURL = getContextFile().toURL();
224         log( getManager().deployContext( getPath(), contextURL, warURL, isUpdate(), getTag() ).getHttpResponseBody() );
225     }
226 }