1 package org.apache.tomcat.maven.plugin.tomcat7.deploy;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
34
35
36
37 public abstract class AbstractDeployMojo
38 extends AbstractWarCatalinaMojo
39 {
40
41
42
43
44
45
46
47
48 @Parameter( property = "maven.tomcat.mode", defaultValue = "war", required = true )
49 private String mode;
50
51
52
53
54 @Parameter( defaultValue = "${project.build.directory}/${project.build.finalName}/META-INF/context.xml" )
55 private File contextFile;
56
57
58
59
60 @Parameter( property = "maven.tomcat.update", defaultValue = "false", required = true )
61 private boolean update;
62
63
64
65
66 @Parameter( property = "maven.tomcat.tag" )
67 private String tag;
68
69
70
71
72
73
74
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
100
101
102
103 protected abstract File getWarFile();
104
105
106
107
108
109
110
111 protected abstract void validateWarFile()
112 throws MojoExecutionException;
113
114
115
116
117
118
119 protected File getContextFile()
120 {
121 return contextFile;
122 }
123
124
125
126
127
128
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
142
143
144
145 protected boolean isUpdate()
146 {
147 return update;
148 }
149
150
151
152
153
154
155 protected String getTag()
156 {
157 return tag;
158 }
159
160
161
162
163
164
165
166
167
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
182
183
184
185
186
187
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
202
203
204
205
206
207
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 }