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