View Javadoc
1   package org.apache.maven.plugin.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 org.apache.maven.archiver.MavenArchiver;
23  import org.apache.maven.artifact.DependencyResolutionRequiredException;
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.Component;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.ResolutionScope;
29  import org.codehaus.plexus.archiver.Archiver;
30  import org.codehaus.plexus.archiver.jar.Manifest;
31  import org.codehaus.plexus.archiver.jar.ManifestException;
32  import org.codehaus.plexus.archiver.war.WarArchiver;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.WriterFactory;
35  
36  import java.io.File;
37  import java.io.IOException;
38  import java.io.PrintWriter;
39  
40  /**
41   * Generate a manifest for this webapp. The manifest file is created in the <code>warSourceDirectory</code>.
42   *
43   * @author Mike Perham
44   * @version $Id: WarManifestMojo.html 935522 2015-01-08 20:15:45Z khmarbaise $
45   */
46  // CHECKSTYLE_OFF: LineLength
47  @Mojo( name = "manifest", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME )
48  // CHECKSTYLE_ON: LineLength
49  public class WarManifestMojo
50      extends AbstractWarMojo
51  {
52      /**
53       * The WAR archiver.
54       */
55      @Component( role = Archiver.class, hint = "war" )
56      private WarArchiver warArchiver;
57  
58      /**
59       * Executes this mojo on the current project.
60       *
61       * @throws MojoExecutionException if an error occurred while building the webapp
62       */
63      public void execute()
64          throws MojoExecutionException
65      {
66          File manifestDir = new File( getWarSourceDirectory(), "META-INF" );
67          if ( !manifestDir.exists() )
68          {
69              manifestDir.mkdirs();
70          }
71          File manifestFile = new File( manifestDir, "MANIFEST.MF" );
72          MavenArchiver ma = new MavenArchiver();
73          ma.setArchiver( warArchiver );
74          ma.setOutputFile( manifestFile );
75  
76          PrintWriter printWriter = null;
77          try
78          {
79              Manifest mf = ma.getManifest( getSession(), getProject(), getArchive() );
80              printWriter = new PrintWriter( WriterFactory.newWriter( manifestFile, WriterFactory.UTF_8 ) );
81              mf.write( printWriter );
82          }
83          catch ( ManifestException e )
84          {
85              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
86          }
87          catch ( DependencyResolutionRequiredException e )
88          {
89              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
90          }
91          catch ( IOException e )
92          {
93              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
94          }
95          finally
96          {
97              IOUtil.close( printWriter );
98          }
99      }
100 }