View Javadoc
1   package org.apache.maven.plugins.assembly.archive;
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.MavenArchiveConfiguration;
23  import org.apache.maven.archiver.MavenArchiver;
24  import org.apache.maven.artifact.DependencyResolutionRequiredException;
25  import org.apache.maven.execution.MavenSession;
26  import org.apache.maven.project.MavenProject;
27  import org.codehaus.plexus.archiver.AbstractArchiveFinalizer;
28  import org.codehaus.plexus.archiver.Archiver;
29  import org.codehaus.plexus.archiver.ArchiverException;
30  import org.codehaus.plexus.archiver.jar.JarArchiver;
31  import org.codehaus.plexus.archiver.jar.Manifest;
32  import org.codehaus.plexus.archiver.jar.ManifestException;
33  
34  import java.io.File;
35  import java.io.FileInputStream;
36  import java.io.FileNotFoundException;
37  import java.io.IOException;
38  import java.io.InputStream;
39  import java.util.Collections;
40  import java.util.List;
41  
42  /**
43   *
44   */
45  public class ManifestCreationFinalizer
46      extends AbstractArchiveFinalizer
47  {
48  
49      private final MavenProject project;
50  
51      private final MavenSession session;
52  
53      private final MavenArchiveConfiguration archiveConfiguration;
54  
55      // TODO: I'd really prefer to rewrite MavenArchiver as either a
56      // separate manifest creation utility (and to
57      // create an include pom.properties etc into another archiver), or
58      // an implementation of an archiver
59      // (the first is preferable).
60      private final MavenArchiver mavenArchiver = new MavenArchiver();
61  
62      public ManifestCreationFinalizer( final MavenSession session, final MavenProject project,
63                                        final MavenArchiveConfiguration archiveConfiguration )
64      {
65          this.session = session;
66          this.project = project;
67          this.archiveConfiguration = archiveConfiguration;
68      }
69  
70      @Override
71      public void finalizeArchiveCreation( final Archiver archiver )
72      {
73          if ( archiveConfiguration != null )
74          {
75              try
76              {
77                  Manifest manifest;
78                  final File manifestFile = archiveConfiguration.getManifestFile();
79  
80                  if ( manifestFile != null )
81                  {
82                      try ( InputStream in = new FileInputStream( manifestFile ) )
83                      {
84                          manifest = new Manifest( in );
85                      }
86                      catch ( final FileNotFoundException e )
87                      {
88                          throw new ArchiverException( "Manifest not found: " + e.getMessage(), e );
89                      }
90                      catch ( final IOException e )
91                      {
92                          throw new ArchiverException( "Error processing manifest: " + e.getMessage(), e );
93                      }
94                  }
95                  else
96                  {
97                      manifest = mavenArchiver.getManifest( session, project, archiveConfiguration );
98                  }
99  
100                 if ( ( manifest != null ) && ( archiver instanceof JarArchiver ) )
101                 {
102                     final JarArchiver jarArchiver = (JarArchiver) archiver;
103                     jarArchiver.addConfiguredManifest( manifest );
104                 }
105             }
106             catch ( final ManifestException e )
107             {
108                 throw new ArchiverException( "Error creating manifest: " + e.getMessage(), e );
109             }
110             catch ( final DependencyResolutionRequiredException e )
111             {
112                 throw new ArchiverException( "Dependencies were not resolved: " + e.getMessage(), e );
113             }
114         }
115     }
116 
117     @Override
118     public List<String> getVirtualFiles()
119     {
120         if ( archiveConfiguration != null )
121         {
122             try
123             {
124                 if ( mavenArchiver.getManifest( project, archiveConfiguration.getManifest() ) != null )
125                 {
126                     return Collections.singletonList( "META-INF/MANIFEST.MF" );
127                 }
128             }
129             catch ( final ManifestException | DependencyResolutionRequiredException ignore )
130             {
131                 // noop
132             }
133         }
134 
135         return null;
136     }
137 
138 }