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 java.io.IOException;
23 import java.io.InputStream;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.jar.Attributes;
28 import java.util.jar.JarEntry;
29 import java.util.jar.JarFile;
30 import java.util.jar.JarOutputStream;
31 import java.util.jar.Manifest;
32
33 import org.codehaus.plexus.util.IOUtil;
34
35
36
37
38
39
40
41
42
43 public class ManifestResourceTransformer
44 implements ResourceTransformer
45 {
46
47
48 private String mainClass;
49 private Map manifestEntries;
50
51
52 private boolean manifestDiscovered;
53 private Manifest manifest;
54
55 public boolean canTransformResource( String resource )
56 {
57 if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) )
58 {
59 return true;
60 }
61
62 return false;
63 }
64
65 public void processResource( String resource, InputStream is, List relocators )
66 throws IOException
67 {
68
69
70
71 if ( !manifestDiscovered )
72 {
73 manifest = new Manifest( is );
74 manifestDiscovered = true;
75 IOUtil.close( is );
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 ( Iterator i = manifestEntries.keySet().iterator(); i.hasNext(); )
103 {
104 String key = (String) i.next();
105 attributes.put( new Attributes.Name( key ), manifestEntries.get( key ) );
106 }
107 }
108
109 jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) );
110 manifest.write( jos );
111 }
112 }