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 925069 2014-10-08 17:03:57Z khmarbaise $
45   */
46  @Mojo( name = "manifest", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, threadSafe = true, requiresDependencyResolution = ResolutionScope.RUNTIME )
47  public class WarManifestMojo
48      extends AbstractWarMojo
49  {
50      /**
51       * The WAR archiver.
52       */
53      @Component( role = Archiver.class, hint = "war" )
54      private WarArchiver warArchiver;
55  
56      /**
57       * Executes this mojo on the current project.
58       *
59       * @throws MojoExecutionException if an error occurred while building the webapp
60       */
61      public void execute()
62          throws MojoExecutionException
63      {
64          File manifestDir = new File( getWarSourceDirectory(), "META-INF" );
65          if ( !manifestDir.exists() )
66          {
67              manifestDir.mkdirs();
68          }
69          File manifestFile = new File( manifestDir, "MANIFEST.MF" );
70          MavenArchiver ma = new MavenArchiver();
71          ma.setArchiver( warArchiver );
72          ma.setOutputFile( manifestFile );
73  
74          PrintWriter printWriter = null;
75          try
76          {
77              Manifest mf = ma.getManifest( getSession(), getProject(), getArchive() );
78              printWriter = new PrintWriter( WriterFactory.newWriter( manifestFile, WriterFactory.UTF_8 ) );
79              mf.write( printWriter );
80          }
81          catch ( ManifestException e )
82          {
83              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
84          }
85          catch ( DependencyResolutionRequiredException e )
86          {
87              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
88          }
89          catch ( IOException e )
90          {
91              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
92          }
93          finally
94          {
95              IOUtil.close( printWriter );
96          }
97      }
98  }