1 package org.apache.maven.plugin.assembly.archive;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.project.MavenProject;
26 import org.codehaus.plexus.archiver.AbstractArchiveFinalizer;
27 import org.codehaus.plexus.archiver.Archiver;
28 import org.codehaus.plexus.archiver.ArchiverException;
29 import org.codehaus.plexus.archiver.jar.JarArchiver;
30 import org.codehaus.plexus.archiver.jar.Manifest;
31 import org.codehaus.plexus.archiver.jar.ManifestException;
32 import org.codehaus.plexus.util.IOUtil;
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.InputStreamReader;
39 import java.io.Reader;
40 import java.util.Collections;
41 import java.util.List;
42
43
44
45
46 public class ManifestCreationFinalizer
47 extends AbstractArchiveFinalizer
48 {
49
50 private final MavenProject project;
51
52 private final MavenArchiveConfiguration archiveConfiguration;
53
54
55
56
57
58
59 private final MavenArchiver mavenArchiver = new MavenArchiver();
60
61 public ManifestCreationFinalizer( final MavenProject project, final MavenArchiveConfiguration archiveConfiguration )
62 {
63 this.project = project;
64 this.archiveConfiguration = archiveConfiguration;
65 }
66
67 @Override
68 public void finalizeArchiveCreation( final Archiver archiver ) throws ArchiverException
69 {
70 if ( archiveConfiguration != null )
71 {
72 try
73 {
74 Manifest manifest;
75 final File manifestFile = archiveConfiguration.getManifestFile();
76
77 if ( manifestFile != null )
78 {
79 Reader manifestFileReader = null;
80 try
81 {
82 manifestFileReader = new InputStreamReader( new FileInputStream( manifestFile ), "UTF-8" );
83 manifest = new Manifest( manifestFileReader );
84 }
85 catch ( final FileNotFoundException e )
86 {
87 throw new ArchiverException( "Manifest not found: " + e.getMessage(), e );
88 }
89 catch ( final IOException e )
90 {
91 throw new ArchiverException( "Error processing manifest: " + e.getMessage(), e );
92 }
93 finally
94 {
95 IOUtil.close( manifestFileReader );
96 }
97 }
98 else
99 {
100 manifest = mavenArchiver.getManifest( project, archiveConfiguration );
101 }
102
103 if ( ( manifest != null ) && ( archiver instanceof JarArchiver ) )
104 {
105 final JarArchiver jarArchiver = (JarArchiver) archiver;
106 jarArchiver.addConfiguredManifest( manifest );
107 }
108 }
109 catch ( final ManifestException e )
110 {
111 throw new ArchiverException( "Error creating manifest: " + e.getMessage(), e );
112 }
113 catch ( final DependencyResolutionRequiredException e )
114 {
115 throw new ArchiverException( "Dependencies were not resolved: " + e.getMessage(), e );
116 }
117 }
118 }
119
120 public List<String> getVirtualFiles()
121 {
122 if ( archiveConfiguration != null )
123 {
124 try
125 {
126 if ( mavenArchiver.getManifest( project, archiveConfiguration.getManifest() ) != null )
127 {
128 return Collections.singletonList( "META-INF/MANIFEST.MF" );
129 }
130 }
131 catch ( final ManifestException e )
132 {
133 }
134 catch ( final DependencyResolutionRequiredException e )
135 {
136 }
137 }
138
139 return null;
140 }
141
142 }