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