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