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