1 | |
package org.apache.maven.plugin.war; |
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.archiver.MavenArchiver; |
23 | |
import org.apache.maven.artifact.Artifact; |
24 | |
import org.apache.maven.artifact.DependencyResolutionRequiredException; |
25 | |
import org.apache.maven.plugin.MojoExecutionException; |
26 | |
import org.apache.maven.plugin.MojoFailureException; |
27 | |
import org.apache.maven.plugin.war.util.ClassesPackager; |
28 | |
import org.apache.maven.project.MavenProjectHelper; |
29 | |
import org.codehaus.plexus.archiver.ArchiverException; |
30 | |
import org.codehaus.plexus.archiver.jar.ManifestException; |
31 | |
import org.codehaus.plexus.archiver.war.WarArchiver; |
32 | |
import org.codehaus.plexus.util.StringUtils; |
33 | |
|
34 | |
import java.io.File; |
35 | |
import java.io.IOException; |
36 | |
import java.util.Arrays; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 16 | public class WarMojo |
49 | |
extends AbstractWarMojo |
50 | |
{ |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
private String outputDirectory; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private String warName; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private String classifier; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
private String packagingExcludes; |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
private String packagingIncludes; |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private WarArchiver warArchiver; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private MavenProjectHelper projectHelper; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | 16 | private boolean primaryArtifact = true; |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | 16 | private boolean failOnMissingWebXml = true; |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | 16 | private boolean attachClasses = false; |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | 16 | private String classesClassifier = "classes"; |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void execute() |
153 | |
throws MojoExecutionException, MojoFailureException |
154 | |
{ |
155 | 14 | File warFile = getTargetWarFile(); |
156 | |
|
157 | |
try |
158 | |
{ |
159 | 14 | performPackaging( warFile ); |
160 | |
} |
161 | 0 | catch ( DependencyResolutionRequiredException e ) |
162 | |
{ |
163 | 0 | throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e ); |
164 | |
} |
165 | 0 | catch ( ManifestException e ) |
166 | |
{ |
167 | 0 | throw new MojoExecutionException( "Error assembling WAR", e ); |
168 | |
} |
169 | 0 | catch ( IOException e ) |
170 | |
{ |
171 | 0 | throw new MojoExecutionException( "Error assembling WAR", e ); |
172 | |
} |
173 | 1 | catch ( ArchiverException e ) |
174 | |
{ |
175 | 1 | throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e ); |
176 | 13 | } |
177 | 13 | } |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
private void performPackaging( File warFile ) |
192 | |
throws IOException, ArchiverException, ManifestException, DependencyResolutionRequiredException, |
193 | |
MojoExecutionException, MojoFailureException |
194 | |
{ |
195 | 14 | getLog().info( "Packaging webapp" ); |
196 | |
|
197 | 14 | buildExplodedWebapp( getWebappDirectory() ); |
198 | |
|
199 | 14 | MavenArchiver archiver = new MavenArchiver(); |
200 | |
|
201 | 14 | archiver.setArchiver( warArchiver ); |
202 | |
|
203 | 14 | archiver.setOutputFile( warFile ); |
204 | |
|
205 | 14 | getLog().debug( |
206 | |
"Excluding " + Arrays.asList( getPackagingExcludes() ) + " from the generated webapp archive." ); |
207 | 14 | getLog().debug( |
208 | |
"Including " + Arrays.asList( getPackagingIncludes() ) + " in the generated webapp archive." ); |
209 | |
|
210 | 14 | warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() ); |
211 | |
|
212 | 14 | final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" ); |
213 | 14 | if ( webXmlFile.exists() ) |
214 | |
{ |
215 | 12 | warArchiver.setWebxml( webXmlFile ); |
216 | |
} |
217 | 14 | if ( !failOnMissingWebXml ) |
218 | |
{ |
219 | 1 | getLog().debug( "Build won't fail if web.xml file is missing." ); |
220 | |
|
221 | 1 | warArchiver.setIgnoreWebxml( false ); |
222 | |
} |
223 | |
|
224 | |
|
225 | 14 | archiver.createArchive( getProject(), getArchive() ); |
226 | |
|
227 | |
|
228 | 13 | if ( isAttachClasses() ) |
229 | |
{ |
230 | 2 | ClassesPackager packager = new ClassesPackager(); |
231 | 2 | final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() ); |
232 | 2 | if ( classesDirectory.exists() ) |
233 | |
{ |
234 | 2 | getLog().info( "Packaging classes" ); |
235 | 2 | packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getProject(), |
236 | |
getArchive() ); |
237 | 2 | projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() ); |
238 | |
} |
239 | |
} |
240 | |
|
241 | 13 | String classifier = this.classifier; |
242 | 13 | if ( classifier != null ) |
243 | |
{ |
244 | 1 | projectHelper.attachArtifact( getProject(), "war", classifier, warFile ); |
245 | |
} |
246 | |
else |
247 | |
{ |
248 | 12 | Artifact artifact = getProject().getArtifact(); |
249 | 12 | if ( primaryArtifact ) |
250 | |
{ |
251 | 11 | artifact.setFile( warFile ); |
252 | |
} |
253 | 1 | else if ( artifact.getFile() == null || artifact.getFile().isDirectory() ) |
254 | |
{ |
255 | 0 | artifact.setFile( warFile ); |
256 | |
} |
257 | |
} |
258 | 13 | } |
259 | |
|
260 | |
|
261 | |
protected static File getTargetFile( File basedir, String finalName, String classifier, String type ) |
262 | |
{ |
263 | 18 | if ( classifier == null ) |
264 | |
{ |
265 | 13 | classifier = ""; |
266 | |
} |
267 | 5 | else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) ) |
268 | |
{ |
269 | 5 | classifier = "-" + classifier; |
270 | |
} |
271 | |
|
272 | 18 | return new File( basedir, finalName + classifier + "." + type ); |
273 | |
} |
274 | |
|
275 | |
|
276 | |
protected File getTargetWarFile() |
277 | |
{ |
278 | 14 | return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" ); |
279 | |
|
280 | |
} |
281 | |
|
282 | |
protected File getTargetClassesFile() |
283 | |
{ |
284 | 4 | return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" ); |
285 | |
} |
286 | |
|
287 | |
|
288 | |
|
289 | |
public String getClassifier() |
290 | |
{ |
291 | 14 | return classifier; |
292 | |
} |
293 | |
|
294 | |
public void setClassifier( String classifier ) |
295 | |
{ |
296 | 0 | this.classifier = classifier; |
297 | 0 | } |
298 | |
|
299 | |
public String[] getPackagingExcludes() |
300 | |
{ |
301 | 28 | if ( StringUtils.isEmpty( packagingExcludes ) ) |
302 | |
{ |
303 | 28 | return new String[0]; |
304 | |
} |
305 | |
else |
306 | |
{ |
307 | 0 | return StringUtils.split( packagingExcludes, "," ); |
308 | |
} |
309 | |
} |
310 | |
|
311 | |
public void setPackagingExcludes( String packagingExcludes ) |
312 | |
{ |
313 | 0 | this.packagingExcludes = packagingExcludes; |
314 | 0 | } |
315 | |
|
316 | |
public String[] getPackagingIncludes() |
317 | |
{ |
318 | 28 | if ( StringUtils.isEmpty( packagingIncludes ) ) |
319 | |
{ |
320 | 28 | return new String[]{"**"}; |
321 | |
} |
322 | |
else |
323 | |
{ |
324 | 0 | return StringUtils.split( packagingIncludes, "," ); |
325 | |
} |
326 | |
} |
327 | |
|
328 | |
public void setPackagingIncludes( String packagingIncludes ) |
329 | |
{ |
330 | 0 | this.packagingIncludes = packagingIncludes; |
331 | 0 | } |
332 | |
|
333 | |
public String getOutputDirectory() |
334 | |
{ |
335 | 18 | return outputDirectory; |
336 | |
} |
337 | |
|
338 | |
public void setOutputDirectory( String outputDirectory ) |
339 | |
{ |
340 | 0 | this.outputDirectory = outputDirectory; |
341 | 0 | } |
342 | |
|
343 | |
public String getWarName() |
344 | |
{ |
345 | 18 | return warName; |
346 | |
} |
347 | |
|
348 | |
public void setWarName( String warName ) |
349 | |
{ |
350 | 0 | this.warName = warName; |
351 | 0 | } |
352 | |
|
353 | |
public WarArchiver getWarArchiver() |
354 | |
{ |
355 | 0 | return warArchiver; |
356 | |
} |
357 | |
|
358 | |
public void setWarArchiver( WarArchiver warArchiver ) |
359 | |
{ |
360 | 0 | this.warArchiver = warArchiver; |
361 | 0 | } |
362 | |
|
363 | |
public MavenProjectHelper getProjectHelper() |
364 | |
{ |
365 | 0 | return projectHelper; |
366 | |
} |
367 | |
|
368 | |
public void setProjectHelper( MavenProjectHelper projectHelper ) |
369 | |
{ |
370 | 0 | this.projectHelper = projectHelper; |
371 | 0 | } |
372 | |
|
373 | |
public boolean isPrimaryArtifact() |
374 | |
{ |
375 | 0 | return primaryArtifact; |
376 | |
} |
377 | |
|
378 | |
public void setPrimaryArtifact( boolean primaryArtifact ) |
379 | |
{ |
380 | 12 | this.primaryArtifact = primaryArtifact; |
381 | 12 | } |
382 | |
|
383 | |
public boolean isAttachClasses() |
384 | |
{ |
385 | 13 | return attachClasses; |
386 | |
} |
387 | |
|
388 | |
public void setAttachClasses( boolean attachClasses ) |
389 | |
{ |
390 | 2 | this.attachClasses = attachClasses; |
391 | 2 | } |
392 | |
|
393 | |
public String getClassesClassifier() |
394 | |
{ |
395 | 6 | return classesClassifier; |
396 | |
} |
397 | |
|
398 | |
public void setClassesClassifier( String classesClassifier ) |
399 | |
{ |
400 | 1 | this.classesClassifier = classesClassifier; |
401 | 1 | } |
402 | |
|
403 | |
public boolean isFailOnMissingWebXml() |
404 | |
{ |
405 | 0 | return failOnMissingWebXml; |
406 | |
} |
407 | |
|
408 | |
public void setFailOnMissingWebXml( boolean failOnMissingWebXml ) |
409 | |
{ |
410 | 2 | this.failOnMissingWebXml = failOnMissingWebXml; |
411 | 2 | } |
412 | |
} |