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.plugin.tomcat7.AbstractWarCatalinaMojo;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.net.URL;
30
31
32
33
34
35
36 public abstract class AbstractDeployMojo
37 extends AbstractWarCatalinaMojo
38 {
39
40
41
42
43
44
45
46
47 @Parameter( property = "maven.tomcat.mode", defaultValue = "war", required = true )
48 private String mode;
49
50
51
52
53 @Parameter( defaultValue = "${project.build.directory}/${project.build.finalName}/META-INF/context.xml" )
54 private File contextFile;
55
56
57
58
59 @Parameter( property = "maven.tomcat.update", defaultValue = "false", required = true )
60 private boolean update;
61
62
63
64
65 @Parameter( property = "maven.tomcat.tag" )
66 private String tag;
67
68
69
70
71
72
73
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
99
100
101
102 protected abstract File getWarFile();
103
104
105
106
107
108
109
110 protected abstract void validateWarFile()
111 throws MojoExecutionException;
112
113
114
115
116
117
118 protected File getContextFile()
119 {
120 return contextFile;
121 }
122
123
124
125
126
127
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
141
142
143
144 protected boolean isUpdate()
145 {
146 return update;
147 }
148
149
150
151
152
153
154 protected String getTag()
155 {
156 return tag;
157 }
158
159
160
161
162
163
164
165
166
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
181
182
183
184
185
186
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
201
202
203
204
205
206
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 }