1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
53
54
55
56
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
108 }
109 }
110
111 return null;
112 }
113 }