View Javadoc

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