View Javadoc
1   package org.apache.maven.plugins.war;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.net.URLClassLoader;
27  import java.util.Arrays;
28  import java.util.List;
29  
30  import org.apache.maven.archiver.MavenArchiver;
31  import org.apache.maven.artifact.Artifact;
32  import org.apache.maven.artifact.DependencyResolutionRequiredException;
33  import org.apache.maven.plugin.MojoExecutionException;
34  import org.apache.maven.plugin.MojoFailureException;
35  import org.apache.maven.plugins.annotations.Component;
36  import org.apache.maven.plugins.annotations.LifecyclePhase;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  import org.apache.maven.plugins.annotations.ResolutionScope;
40  import org.apache.maven.plugins.war.util.ClassesPackager;
41  import org.apache.maven.project.MavenProjectHelper;
42  import org.codehaus.plexus.archiver.Archiver;
43  import org.codehaus.plexus.archiver.ArchiverException;
44  import org.codehaus.plexus.archiver.jar.ManifestException;
45  import org.codehaus.plexus.archiver.war.WarArchiver;
46  import org.codehaus.plexus.util.FileUtils;
47  import org.codehaus.plexus.util.StringUtils;
48  
49  /**
50   * Build a WAR file.
51   *
52   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
53   * @version $Id: WarMojo.java 1781707 2017-02-04 21:05:02Z michaelo $
54   */
55  // CHECKSTYLE_OFF: LineLength
56  @Mojo( name = "war", defaultPhase = LifecyclePhase.PACKAGE, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME )
57  // CHECKSTYLE_ON: LineLength
58  public class WarMojo
59      extends AbstractWarMojo
60  {
61      /**
62       * The directory for the generated WAR.
63       */
64      @Parameter( defaultValue = "${project.build.directory}", required = true )
65      private String outputDirectory;
66  
67      /**
68       * The name of the generated WAR.
69       */
70      @Parameter( defaultValue = "${project.build.finalName}", required = true, readonly = true )
71      private String warName;
72  
73      /**
74       * Classifier to add to the generated WAR. If given, the artifact will be an attachment instead. The classifier will
75       * not be applied to the JAR file of the project - only to the WAR file.
76       */
77      @Parameter
78      private String classifier;
79  
80      /**
81       * The comma separated list of tokens to exclude from the WAR before packaging. This option may be used to implement
82       * the skinny WAR use case. Note that you can use the Java Regular Expressions engine to include and exclude
83       * specific pattern using the expression %regex[]. Hint: read the about (?!Pattern).
84       *
85       * @since 2.1-alpha-2
86       */
87      @Parameter
88      private String packagingExcludes;
89  
90      /**
91       * The comma separated list of tokens to include in the WAR before packaging. By default everything is included.
92       * This option may be used to implement the skinny WAR use case. Note that you can use the Java Regular Expressions
93       * engine to include and exclude specific pattern using the expression %regex[].
94       *
95       * @since 2.1-beta-1
96       */
97      @Parameter
98      private String packagingIncludes;
99  
100     /**
101      * The WAR archiver.
102      */
103     @Component( role = Archiver.class, hint = "war" )
104     private WarArchiver warArchiver;
105 
106     /**
107      */
108     @Component
109     private MavenProjectHelper projectHelper;
110 
111     /**
112      * Whether this is the main artifact being built. Set to <code>false</code> if you don't want to install or deploy
113      * it to the local repository instead of the default one in an execution.
114      */
115     @Parameter( defaultValue = "true" )
116     private boolean primaryArtifact;
117 
118     /**
119      * Whether or not to fail the build if the <code>web.xml</code> file is missing. Set to <code>false</code> if you
120      * want your WAR built without a <code>web.xml</code> file. This may be useful if you are building an overlay that
121      * has no web.xml file.
122      * <p>
123      * Starting with <b>3.1.0</b>, this property defaults to <code>false</code> if the project depends on the Servlet
124      * 3.0 API or newer.
125      *
126      * @since 2.1-alpha-2
127      */
128     @Parameter
129     private Boolean failOnMissingWebXml;
130 
131     /**
132      * Whether classes (that is the content of the WEB-INF/classes directory) should be attached to the project as an
133      * additional artifact.
134      * <p>
135      * By default the classifier for the additional artifact is 'classes'. You can change it with the
136      * <code><![CDATA[<classesClassifier>someclassifier</classesClassifier>]]></code> parameter.
137      * </p>
138      * <p>
139      * If this parameter true, another project can depend on the classes by writing something like:
140      *
141      * <pre>
142      * <![CDATA[<dependency>
143      *   <groupId>myGroup</groupId>
144      *   <artifactId>myArtifact</artifactId>
145      *   <version>myVersion</myVersion>
146      *   <classifier>classes</classifier>
147      * </dependency>]]>
148      * </pre>
149      * </p>
150      *
151      * @since 2.1-alpha-2
152      */
153     @Parameter( defaultValue = "false" )
154     private boolean attachClasses;
155 
156     /**
157      * The classifier to use for the attached classes artifact.
158      *
159      * @since 2.1-alpha-2
160      */
161     @Parameter( defaultValue = "classes" )
162     private String classesClassifier;
163 
164     /**
165      * You can skip the execution of the plugin if you need to. Its use is NOT RECOMMENDED, but quite convenient on
166      * occasion.
167      *
168      * @since 3.0.0
169      */
170     @Parameter( property = "maven.war.skip", defaultValue = "false" )
171     private boolean skip;
172 
173     // ----------------------------------------------------------------------
174     // Implementation
175     // ----------------------------------------------------------------------
176 
177     /**
178      * Executes the WarMojo on the current project.
179      *
180      * @throws MojoExecutionException if an error occurred while building the webapp
181      * @throws MojoFailureException if an error.
182      */
183     public void execute()
184         throws MojoExecutionException, MojoFailureException
185     {
186 
187         if ( isSkip() )
188         {
189             getLog().info( "Skipping the execution." );
190             return;
191         }
192 
193         File warFile = getTargetWarFile();
194 
195         try
196         {
197             performPackaging( warFile );
198         }
199         catch ( DependencyResolutionRequiredException e )
200         {
201             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
202         }
203         catch ( ManifestException e )
204         {
205             throw new MojoExecutionException( "Error assembling WAR", e );
206         }
207         catch ( IOException e )
208         {
209             throw new MojoExecutionException( "Error assembling WAR", e );
210         }
211         catch ( ArchiverException e )
212         {
213             throw new MojoExecutionException( "Error assembling WAR: " + e.getMessage(), e );
214         }
215     }
216 
217     /**
218      * Generates the webapp according to the <tt>mode</tt> attribute.
219      *
220      * @param warFile the target WAR file
221      * @throws IOException if an error occurred while copying files
222      * @throws ArchiverException if the archive could not be created
223      * @throws ManifestException if the manifest could not be created
224      * @throws DependencyResolutionRequiredException if an error occurred while resolving the dependencies
225      * @throws MojoExecutionException if the execution failed
226      * @throws MojoFailureException if a fatal exception occurred
227      */
228     private void performPackaging( File warFile )
229         throws IOException, ManifestException, DependencyResolutionRequiredException, MojoExecutionException,
230         MojoFailureException
231     {
232         getLog().info( "Packaging webapp" );
233 
234         buildExplodedWebapp( getWebappDirectory() );
235 
236         MavenArchiver archiver = new MavenArchiver();
237 
238         archiver.setArchiver( warArchiver );
239 
240         archiver.setOutputFile( warFile );
241 
242         // CHECKSTYLE_OFF: LineLength
243         getLog().debug( "Excluding " + Arrays.asList( getPackagingExcludes() )
244             + " from the generated webapp archive." );
245         getLog().debug( "Including " + Arrays.asList( getPackagingIncludes() ) + " in the generated webapp archive." );
246         // CHECKSTYLE_ON: LineLength
247 
248         warArchiver.addDirectory( getWebappDirectory(), getPackagingIncludes(), getPackagingExcludes() );
249 
250         final File webXmlFile = new File( getWebappDirectory(), "WEB-INF/web.xml" );
251         if ( webXmlFile.exists() )
252         {
253             warArchiver.setWebxml( webXmlFile );
254         }
255 
256         warArchiver.setRecompressAddedZips( isRecompressZippedFiles() );
257 
258         warArchiver.setIncludeEmptyDirs( isIncludeEmptyDirectories() );
259 
260         if ( Boolean.FALSE.equals( failOnMissingWebXml )
261             || ( failOnMissingWebXml == null && isProjectUsingAtLeastServlet30() ) )
262         {
263             getLog().debug( "Build won't fail if web.xml file is missing." );
264             warArchiver.setExpectWebXml( false );
265         }
266 
267         // create archive
268         archiver.createArchive( getSession(), getProject(), getArchive() );
269 
270         // create the classes to be attached if necessary
271         if ( isAttachClasses() )
272         {
273             if ( isArchiveClasses() && getJarArchiver().getDestFile() != null )
274             {
275                 // special handling in case of archived classes: MWAR-240
276                 File targetClassesFile = getTargetClassesFile();
277                 FileUtils.copyFile( getJarArchiver().getDestFile(), targetClassesFile );
278                 projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), targetClassesFile );
279             }
280             else
281             {
282                 ClassesPackager packager = new ClassesPackager();
283                 final File classesDirectory = packager.getClassesDirectory( getWebappDirectory() );
284                 if ( classesDirectory.exists() )
285                 {
286                     getLog().info( "Packaging classes" );
287                     packager.packageClasses( classesDirectory, getTargetClassesFile(), getJarArchiver(), getSession(),
288                                              getProject(), getArchive() );
289                     projectHelper.attachArtifact( getProject(), "jar", getClassesClassifier(), getTargetClassesFile() );
290                 }
291             }
292         }
293 
294         if ( this.classifier != null )
295         {
296             projectHelper.attachArtifact( getProject(), "war", this.classifier, warFile );
297         }
298         else
299         {
300             Artifact artifact = getProject().getArtifact();
301             if ( primaryArtifact )
302             {
303                 artifact.setFile( warFile );
304             }
305             else if ( artifact.getFile() == null || artifact.getFile().isDirectory() )
306             {
307                 artifact.setFile( warFile );
308             }
309         }
310     }
311 
312     /**
313      * Determines if the current Maven project being built uses the Servlet 3.0 API (JSR 315). If it does then the
314      * <code>web.xml</code> file can be omitted.
315      * <p>
316      * This is done by checking if the interface <code>javax.servlet.annotation.WebServlet</code> is in the compile-time
317      * dependencies (which includes provided dependencies) of the Maven project.
318      *
319      * @return <code>true</code> if the project being built depends on Servlet 3.0 API, <code>false</code> otherwise.
320      * @throws DependencyResolutionRequiredException if the compile elements can't be resolved.
321      * @throws MalformedURLException if the path to a dependency file can't be transformed to a URL.
322      */
323     private boolean isProjectUsingAtLeastServlet30()
324         throws DependencyResolutionRequiredException, MalformedURLException
325     {
326         List<String> classpathElements = getProject().getCompileClasspathElements();
327         URL[] urls = new URL[classpathElements.size()];
328         for ( int i = 0; i < urls.length; i++ )
329         {
330             urls[i] = new File( classpathElements.get( i ) ).toURI().toURL();
331         }
332         ClassLoader loader = new URLClassLoader( urls, Thread.currentThread().getContextClassLoader() );
333         try
334         {
335             Class.forName( "javax.servlet.annotation.WebServlet", false, loader );
336             return true;
337         }
338         catch ( ClassNotFoundException e )
339         {
340             return false;
341         }
342     }
343 
344     /**
345      * @param basedir The basedir
346      * @param finalName The finalName
347      * @param classifier The classifier.
348      * @param type The type.
349      * @return {@link File}
350      */
351     protected static File getTargetFile( File basedir, String finalName, String classifier, String type )
352     {
353         if ( classifier == null )
354         {
355             classifier = "";
356         }
357         else if ( classifier.trim().length() > 0 && !classifier.startsWith( "-" ) )
358         {
359             classifier = "-" + classifier;
360         }
361 
362         return new File( basedir, finalName + classifier + "." + type );
363     }
364 
365     /**
366      * @return The war {@link File}
367      */
368     protected File getTargetWarFile()
369     {
370         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassifier(), "war" );
371 
372     }
373 
374     /**
375      * @return The target class {@link File}
376      */
377     protected File getTargetClassesFile()
378     {
379         return getTargetFile( new File( getOutputDirectory() ), getWarName(), getClassesClassifier(), "jar" );
380     }
381 
382     // Getters and Setters
383 
384     /**
385      * @return {@link #classifier}
386      */
387     public String getClassifier()
388     {
389         return classifier;
390     }
391 
392     /**
393      * @param classifier {@link #classifier}
394      */
395     public void setClassifier( String classifier )
396     {
397         this.classifier = classifier;
398     }
399 
400     /**
401      * @return The package excludes.
402      */
403     public String[] getPackagingExcludes()
404     {
405         if ( StringUtils.isEmpty( packagingExcludes ) )
406         {
407             return new String[0];
408         }
409         else
410         {
411             return StringUtils.split( packagingExcludes, "," );
412         }
413     }
414 
415     /**
416      * @param packagingExcludes {@link #packagingExcludes}
417      */
418     public void setPackagingExcludes( String packagingExcludes )
419     {
420         this.packagingExcludes = packagingExcludes;
421     }
422 
423     /**
424      * @return The packaging includes.
425      */
426     public String[] getPackagingIncludes()
427     {
428         if ( StringUtils.isEmpty( packagingIncludes ) )
429         {
430             return new String[] { "**" };
431         }
432         else
433         {
434             return StringUtils.split( packagingIncludes, "," );
435         }
436     }
437 
438     /**
439      * @param packagingIncludes {@link #packagingIncludes}
440      */
441     public void setPackagingIncludes( String packagingIncludes )
442     {
443         this.packagingIncludes = packagingIncludes;
444     }
445 
446     /**
447      * @return {@link #outputDirectory}
448      */
449     public String getOutputDirectory()
450     {
451         return outputDirectory;
452     }
453 
454     /**
455      * @param outputDirectory {@link #outputDirectory}
456      */
457     public void setOutputDirectory( String outputDirectory )
458     {
459         this.outputDirectory = outputDirectory;
460     }
461 
462     /**
463      * @return {@link #warName}
464      */
465     public String getWarName()
466     {
467         return warName;
468     }
469 
470     /**
471      * @param warName {@link #warName}
472      */
473     public void setWarName( String warName )
474     {
475         this.warName = warName;
476     }
477 
478     /**
479      * @return {@link #warArchiver}
480      */
481     public WarArchiver getWarArchiver()
482     {
483         return warArchiver;
484     }
485 
486     /**
487      * @param warArchiver {@link #warArchiver}
488      */
489     public void setWarArchiver( WarArchiver warArchiver )
490     {
491         this.warArchiver = warArchiver;
492     }
493 
494     /**
495      * @return {@link #projectHelper}
496      */
497     public MavenProjectHelper getProjectHelper()
498     {
499         return projectHelper;
500     }
501 
502     /**
503      * @param projectHelper {@link #projectHelper}
504      */
505     public void setProjectHelper( MavenProjectHelper projectHelper )
506     {
507         this.projectHelper = projectHelper;
508     }
509 
510     /**
511      * @return {@link #primaryArtifact}
512      */
513     public boolean isPrimaryArtifact()
514     {
515         return primaryArtifact;
516     }
517 
518     /**
519      * @param primaryArtifact {@link #primaryArtifact}
520      */
521     public void setPrimaryArtifact( boolean primaryArtifact )
522     {
523         this.primaryArtifact = primaryArtifact;
524     }
525 
526     /**
527      * @return {@link #attachClasses}
528      */
529     public boolean isAttachClasses()
530     {
531         return attachClasses;
532     }
533 
534     /**
535      * @param attachClasses {@link #attachClasses}
536      */
537     public void setAttachClasses( boolean attachClasses )
538     {
539         this.attachClasses = attachClasses;
540     }
541 
542     /**
543      * @return {@link #classesClassifier}
544      */
545     public String getClassesClassifier()
546     {
547         return classesClassifier;
548     }
549 
550     /**
551      * @param classesClassifier {@link #classesClassifier}
552      */
553     public void setClassesClassifier( String classesClassifier )
554     {
555         this.classesClassifier = classesClassifier;
556     }
557 
558     /**
559      * @return {@link #failOnMissingWebXml}
560      */
561     public boolean isFailOnMissingWebXml()
562     {
563         return failOnMissingWebXml;
564     }
565 
566     /**
567      * @param failOnMissingWebXml {@link #failOnMissingWebXml}
568      */
569     public void setFailOnMissingWebXml( boolean failOnMissingWebXml )
570     {
571         this.failOnMissingWebXml = failOnMissingWebXml;
572     }
573 
574     public boolean isSkip()
575     {
576         return skip;
577     }
578 }