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
42   * <code>warSourceDirectory</code>.
43   *
44   * @author Mike Perham
45   * @version $Id: WarManifestMojo.html 867902 2013-06-30 13:58:43Z olamy $
46   */
47  @Mojo( name = "manifest", defaultPhase = LifecyclePhase.PROCESS_RESOURCES, threadSafe = true,
48         requiresDependencyResolution = ResolutionScope.RUNTIME )
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      /**
60       * Executes this mojo on the current project.
61       *
62       * @throws MojoExecutionException if an error occurred while building the webapp
63       */
64      public void execute()
65          throws MojoExecutionException
66      {
67          File manifestDir = new File( getWarSourceDirectory(), "META-INF" );
68          if ( !manifestDir.exists() )
69          {
70              manifestDir.mkdirs();
71          }
72          File manifestFile = new File( manifestDir, "MANIFEST.MF" );
73          MavenArchiver ma = new MavenArchiver();
74          ma.setArchiver( warArchiver );
75          ma.setOutputFile( manifestFile );
76  
77          PrintWriter printWriter = null;
78          try
79          {
80              Manifest mf = ma.getManifest( getSession(), getProject(), getArchive() );
81              printWriter = new PrintWriter( WriterFactory.newWriter( manifestFile, WriterFactory.UTF_8 ) );
82              mf.write( printWriter );
83          }
84          catch ( ManifestException e )
85          {
86              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
87          }
88          catch ( DependencyResolutionRequiredException e )
89          {
90              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
91          }
92          catch ( IOException e )
93          {
94              throw new MojoExecutionException( "Error preparing the manifest: " + e.getMessage(), e );
95          }
96          finally
97          {
98              IOUtil.close( printWriter );
99          }
100     }
101 }