View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.archive;
20  
21  import java.io.File;
22  import java.io.FileInputStream;
23  import java.io.FileNotFoundException;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.maven.archiver.MavenArchiveConfiguration;
30  import org.apache.maven.archiver.MavenArchiver;
31  import org.apache.maven.artifact.DependencyResolutionRequiredException;
32  import org.apache.maven.execution.MavenSession;
33  import org.apache.maven.project.MavenProject;
34  import org.codehaus.plexus.archiver.AbstractArchiveFinalizer;
35  import org.codehaus.plexus.archiver.Archiver;
36  import org.codehaus.plexus.archiver.ArchiverException;
37  import org.codehaus.plexus.archiver.jar.JarArchiver;
38  import org.codehaus.plexus.archiver.jar.Manifest;
39  import org.codehaus.plexus.archiver.jar.ManifestException;
40  
41  /**
42   *
43   */
44  public class ManifestCreationFinalizer extends AbstractArchiveFinalizer {
45  
46      private final MavenProject project;
47  
48      private final MavenSession session;
49  
50      private final MavenArchiveConfiguration archiveConfiguration;
51  
52      // TODO: I'd really prefer to rewrite MavenArchiver as either a
53      // separate manifest creation utility (and to
54      // create an include pom.properties etc into another archiver), or
55      // an implementation of an archiver
56      // (the first is preferable).
57      private final MavenArchiver mavenArchiver = new MavenArchiver();
58  
59      public ManifestCreationFinalizer(
60              final MavenSession session,
61              final MavenProject project,
62              final MavenArchiveConfiguration archiveConfiguration) {
63          this.session = session;
64          this.project = project;
65          this.archiveConfiguration = archiveConfiguration;
66      }
67  
68      @Override
69      public void finalizeArchiveCreation(final Archiver archiver) {
70          if (archiveConfiguration != null) {
71              try {
72                  Manifest manifest;
73                  final File manifestFile = archiveConfiguration.getManifestFile();
74  
75                  if (manifestFile != null) {
76                      try (InputStream in = new FileInputStream(manifestFile)) {
77                          manifest = new Manifest(in);
78                      } catch (final FileNotFoundException e) {
79                          throw new ArchiverException("Manifest not found: " + e.getMessage(), e);
80                      } catch (final IOException e) {
81                          throw new ArchiverException("Error processing manifest: " + e.getMessage(), e);
82                      }
83                  } else {
84                      manifest = mavenArchiver.getManifest(session, project, archiveConfiguration);
85                  }
86  
87                  if ((manifest != null) && (archiver instanceof JarArchiver)) {
88                      final JarArchiver jarArchiver = (JarArchiver) archiver;
89                      jarArchiver.addConfiguredManifest(manifest);
90                  }
91              } catch (final ManifestException e) {
92                  throw new ArchiverException("Error creating manifest: " + e.getMessage(), e);
93              } catch (final DependencyResolutionRequiredException e) {
94                  throw new ArchiverException("Dependencies were not resolved: " + e.getMessage(), e);
95              }
96          }
97      }
98  
99      @Override
100     public List<String> getVirtualFiles() {
101         if (archiveConfiguration != null) {
102             try {
103                 if (mavenArchiver.getManifest(project, archiveConfiguration.getManifest()) != null) {
104                     return Collections.singletonList("META-INF/MANIFEST.MF");
105                 }
106             } catch (final ManifestException | DependencyResolutionRequiredException ignore) {
107                 // noop
108             }
109         }
110 
111         return null;
112     }
113 }