1 package org.apache.maven.plugins.shade.resource;
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.plugins.shade.relocation.Relocator;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.jar.Attributes;
29 import java.util.jar.JarEntry;
30 import java.util.jar.JarFile;
31 import java.util.jar.JarOutputStream;
32 import java.util.jar.Manifest;
33
34
35
36
37
38
39
40
41
42 public class ManifestResourceTransformer
43 implements ResourceTransformer
44 {
45
46
47 private String mainClass;
48
49 private Map<String, Object> manifestEntries;
50
51
52 private boolean manifestDiscovered;
53
54 private Manifest manifest;
55
56 public boolean canTransformResource( String resource )
57 {
58 if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) )
59 {
60 return true;
61 }
62
63 return false;
64 }
65
66 public void processResource( String resource, InputStream is, List<Relocator> relocators )
67 throws IOException
68 {
69
70
71
72 if ( !manifestDiscovered )
73 {
74 manifest = new Manifest( is );
75 manifestDiscovered = true;
76 }
77 }
78
79 public boolean hasTransformedResource()
80 {
81 return true;
82 }
83
84 public void modifyOutputStream( JarOutputStream jos )
85 throws IOException
86 {
87
88 if ( manifest == null )
89 {
90 manifest = new Manifest();
91 }
92
93 Attributes attributes = manifest.getMainAttributes();
94
95 if ( mainClass != null )
96 {
97 attributes.put( Attributes.Name.MAIN_CLASS, mainClass );
98 }
99
100 if ( manifestEntries != null )
101 {
102 for ( Map.Entry<String, Object> entry : manifestEntries.entrySet() )
103 {
104 attributes.put( new Attributes.Name( entry.getKey() ), entry.getValue() );
105 }
106 }
107
108 jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) );
109 manifest.write( jos );
110 }
111 }