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