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