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