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.maven.plugins.annotations.Parameter;
24  import org.apache.tomcat.maven.common.deployer.TomcatManagerException;
25  import org.apache.tomcat.maven.plugin.tomcat7.AbstractWarCatalinaMojo;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.net.URL;
30  
31  /**
32   * Deploy a WAR to Tomcat.
33   *
34   * @author Mark Hobson <markhobson@gmail.com>
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( property = "maven.tomcat.mode", defaultValue = "war", required = true )
48      private String mode;
49  
50      /**
51       * The path of the Tomcat context XML file. This is not used for war deployment mode.
52       */
53      @Parameter( defaultValue = "${project.build.directory}/${project.build.finalName}/META-INF/context.xml" )
54      private File contextFile;
55  
56      /**
57       * Whether Tomcat should automatically undeploy webapps that already exist when deploying.
58       */
59      @Parameter( property = "maven.tomcat.update", defaultValue = "false", required = true )
60      private boolean update;
61  
62      /**
63       * The Tomcat webapp tag name to use.
64       */
65      @Parameter( property = "maven.tomcat.tag" )
66      private String tag;
67  
68      // ----------------------------------------------------------------------
69      // Protected Methods
70      // ----------------------------------------------------------------------
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public void invokeManager()
77          throws MojoExecutionException, TomcatManagerException, IOException
78      {
79          if ( "war".equals( mode ) )
80          {
81              deployWar();
82          }
83          else if ( "context".equals( mode ) )
84          {
85              deployContext();
86          }
87          else if ( "both".equals( mode ) )
88          {
89              deployWarAndContext();
90          }
91          else
92          {
93              throw new MojoExecutionException( messagesProvider.getMessage( "AbstractDeployMojo.unknownMode", mode ) );
94          }
95      }
96  
97      /**
98       * Gets the Tomcat WAR file. This may be a file or a directory depending on the deployment mode.
99       *
100      * @return the Tomcat WAR file.
101      */
102     protected abstract File getWarFile();
103 
104     /**
105      * Ensures that the Tomcat WAR file exists and is the correct type for the deployment mode.
106      *
107      * @throws org.apache.maven.plugin.MojoExecutionException
108      *          if the WAR file does not exist or is not the correct type for the deployment mode
109      */
110     protected abstract void validateWarFile()
111         throws MojoExecutionException;
112 
113     /**
114      * Gets the Tomcat context XML file.
115      *
116      * @return the Tomcat context XML file.
117      */
118     protected File getContextFile()
119     {
120         return contextFile;
121     }
122 
123     /**
124      * Ensures that the Tomcat context XML file exists and is indeed a file.
125      *
126      * @throws org.apache.maven.plugin.MojoExecutionException
127      *          if the context file does not exist or is not a file
128      */
129     protected void validateContextFile()
130         throws MojoExecutionException
131     {
132         if ( !contextFile.exists() || !contextFile.isFile() )
133         {
134             throw new MojoExecutionException(
135                 messagesProvider.getMessage( "AbstractDeployMojo.missingContext", contextFile.getPath() ) );
136         }
137     }
138 
139     /**
140      * Gets whether Tomcat should automatically undeploy webapps that already exist when deploying.
141      *
142      * @return whether Tomcat should automatically undeploy webapps that already exist when deploying
143      */
144     protected boolean isUpdate()
145     {
146         return update;
147     }
148 
149     /**
150      * Gets the Tomcat webapp tag name to use.
151      *
152      * @return the Tomcat webapp tag name to use
153      */
154     protected String getTag()
155     {
156         return tag;
157     }
158 
159     /**
160      * Deploys the WAR to Tomcat.
161      *
162      * @throws org.apache.maven.plugin.MojoExecutionException
163      *                             if there was a problem locating the WAR
164      * @throws org.apache.tomcat.maven.common.deployer.TomcatManagerException
165      *                             if the Tomcat manager request fails
166      * @throws java.io.IOException if an i/o error occurs
167      */
168     protected void deployWar()
169         throws MojoExecutionException, TomcatManagerException, IOException
170     {
171         validateWarFile();
172 
173         getLog().info( messagesProvider.getMessage( "AbstractDeployMojo.deployingWar", getDeployedURL() ) );
174 
175         URL warURL = getWarFile().toURL();
176         log( getManager().deploy( getPath(), warURL, isUpdate(), getTag() ).getHttpResponseBody() );
177     }
178 
179     /**
180      * Deploys the context XML file to Tomcat.
181      *
182      * @throws org.apache.maven.plugin.MojoExecutionException
183      *                             if there was a problem locating the context XML file
184      * @throws org.apache.tomcat.maven.common.deployer.TomcatManagerException
185      *                             if the Tomcat manager request fails
186      * @throws java.io.IOException if an i/o error occurs
187      */
188     protected void deployContext()
189         throws MojoExecutionException, TomcatManagerException, IOException
190     {
191         validateContextFile();
192 
193         getLog().info( messagesProvider.getMessage( "AbstractDeployMojo.deployingContext", getDeployedURL() ) );
194 
195         URL contextURL = getContextFile().toURL();
196         log( getManager().deployContext( getPath(), contextURL, isUpdate(), getTag() ).getHttpResponseBody() );
197     }
198 
199     /**
200      * Deploys the WAR and context XML file to Tomcat.
201      *
202      * @throws org.apache.maven.plugin.MojoExecutionException
203      *                             if there was a problem locating either the WAR or the context XML file
204      * @throws org.apache.tomcat.maven.common.deployer.TomcatManagerException
205      *                             if the Tomcat manager request fails
206      * @throws java.io.IOException if an i/o error occurs
207      */
208     protected void deployWarAndContext()
209         throws MojoExecutionException, TomcatManagerException, IOException
210     {
211         validateWarFile();
212         validateContextFile();
213 
214         getLog().info( messagesProvider.getMessage( "AbstractDeployMojo.deployingWarContext", getDeployedURL() ) );
215 
216         URL warURL = getWarFile().toURL();
217         URL contextURL = getContextFile().toURL();
218         log( getManager().deployContext( getPath(), contextURL, warURL, isUpdate(), getTag() ).getHttpResponseBody() );
219     }
220 }