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