1 | |
package org.apache.maven.plugins.shade; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.io.FileOutputStream; |
24 | |
import java.io.IOException; |
25 | |
import java.io.InputStream; |
26 | |
import java.util.Enumeration; |
27 | |
import java.util.HashSet; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
import java.util.Set; |
31 | |
import java.util.ArrayList; |
32 | |
import java.util.jar.JarEntry; |
33 | |
import java.util.jar.JarFile; |
34 | |
import java.util.jar.JarOutputStream; |
35 | |
import java.util.regex.Matcher; |
36 | |
import java.util.regex.Pattern; |
37 | |
import java.util.zip.ZipException; |
38 | |
|
39 | |
import org.apache.maven.plugins.shade.relocation.Relocator; |
40 | |
import org.apache.maven.plugins.shade.resource.ResourceTransformer; |
41 | |
import org.apache.maven.plugins.shade.filter.Filter; |
42 | |
import org.codehaus.plexus.logging.AbstractLogEnabled; |
43 | |
import org.codehaus.plexus.util.IOUtil; |
44 | |
import org.objectweb.asm.ClassReader; |
45 | |
import org.objectweb.asm.ClassVisitor; |
46 | |
import org.objectweb.asm.ClassWriter; |
47 | |
import org.objectweb.asm.commons.Remapper; |
48 | |
import org.objectweb.asm.commons.RemappingClassAdapter; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | 7 | public class DefaultShader |
55 | |
extends AbstractLogEnabled |
56 | |
implements Shader |
57 | |
{ |
58 | |
public void shade( Set jars, File uberJar, List filters, List relocators, List resourceTransformers ) |
59 | |
throws IOException |
60 | |
{ |
61 | 7 | Set resources = new HashSet(); |
62 | |
|
63 | 7 | RelocatorRemapper remapper = new RelocatorRemapper( relocators ); |
64 | |
|
65 | 7 | uberJar.getParentFile().mkdirs(); |
66 | 7 | JarOutputStream jos = new JarOutputStream( new FileOutputStream( uberJar ) ); |
67 | |
|
68 | 7 | for ( Iterator i = jars.iterator(); i.hasNext(); ) |
69 | |
{ |
70 | 12 | File jar = (File) i.next(); |
71 | |
|
72 | 12 | List jarFilters = getFilters( jar, filters ); |
73 | |
|
74 | |
JarFile jarFile; |
75 | |
|
76 | |
try |
77 | |
{ |
78 | 12 | jarFile = new JarFile( jar ); |
79 | |
} |
80 | 0 | catch (ZipException zex) |
81 | |
{ |
82 | |
|
83 | |
|
84 | 0 | throw new ZipException( "error in opening zip file " + jar ); |
85 | 12 | } |
86 | |
|
87 | 12 | for ( Enumeration j = jarFile.entries(); j.hasMoreElements(); ) |
88 | |
{ |
89 | 631 | JarEntry entry = (JarEntry) j.nextElement(); |
90 | |
|
91 | 631 | String name = entry.getName(); |
92 | |
|
93 | 631 | if ( "META-INF/INDEX.LIST".equals( name ) ) |
94 | |
{ |
95 | |
|
96 | |
|
97 | |
|
98 | 0 | continue; |
99 | |
} |
100 | |
|
101 | 631 | if ( !entry.isDirectory() && !isFiltered( jarFilters, name ) ) |
102 | |
{ |
103 | 483 | InputStream is = jarFile.getInputStream( entry ); |
104 | |
|
105 | 483 | String mappedName = remapper.map( name ); |
106 | |
|
107 | 483 | int idx = mappedName.lastIndexOf( '/' ); |
108 | 483 | if ( idx != -1 ) |
109 | |
{ |
110 | |
|
111 | 483 | String dir = mappedName.substring( 0, idx ); |
112 | 483 | if ( !resources.contains( dir ) ) |
113 | |
{ |
114 | 81 | addDirectory( resources, jos, dir ); |
115 | |
} |
116 | |
} |
117 | |
|
118 | 483 | if ( name.endsWith( ".class" ) ) |
119 | |
{ |
120 | 422 | addRemappedClass( remapper, jos, jar, name, is ); |
121 | |
} |
122 | |
else |
123 | |
{ |
124 | 61 | if ( !resourceTransformed( resourceTransformers, mappedName, is, relocators ) ) |
125 | |
{ |
126 | |
|
127 | 56 | if ( resources.contains( mappedName ) ) |
128 | |
{ |
129 | 5 | continue; |
130 | |
} |
131 | |
|
132 | 51 | addResource( resources, jos, mappedName, is ); |
133 | |
} |
134 | |
} |
135 | |
|
136 | 478 | IOUtil.close( is ); |
137 | |
} |
138 | |
} |
139 | |
|
140 | 12 | jarFile.close(); |
141 | |
} |
142 | |
|
143 | 7 | for ( Iterator i = resourceTransformers.iterator(); i.hasNext(); ) |
144 | |
{ |
145 | 5 | ResourceTransformer transformer = (ResourceTransformer) i.next(); |
146 | |
|
147 | 5 | if ( transformer.hasTransformedResource() ) |
148 | |
{ |
149 | 5 | transformer.modifyOutputStream( jos ); |
150 | |
} |
151 | |
} |
152 | |
|
153 | 7 | IOUtil.close( jos ); |
154 | 7 | } |
155 | |
|
156 | |
private List getFilters( File jar, List filters ) |
157 | |
{ |
158 | 12 | List list = new ArrayList(); |
159 | |
|
160 | 12 | for ( int i = 0; i < filters.size(); i++ ) |
161 | |
{ |
162 | 0 | Filter filter = (Filter) filters.get( i ); |
163 | |
|
164 | 0 | if ( filter.canFilter( jar ) ) |
165 | |
{ |
166 | 0 | list.add( filter ); |
167 | |
} |
168 | |
|
169 | |
} |
170 | |
|
171 | 12 | return list; |
172 | |
} |
173 | |
|
174 | |
private void addDirectory( Set resources, JarOutputStream jos, String name ) |
175 | |
throws IOException |
176 | |
{ |
177 | 150 | if ( name.lastIndexOf( '/' ) > 0 ) |
178 | |
{ |
179 | 129 | String parent = name.substring( 0, name.lastIndexOf( '/' ) ); |
180 | 129 | if ( !resources.contains( parent ) ) |
181 | |
{ |
182 | 69 | addDirectory( resources, jos, parent ); |
183 | |
} |
184 | |
} |
185 | |
|
186 | |
|
187 | 150 | JarEntry entry = new JarEntry( name + "/" ); |
188 | 150 | jos.putNextEntry( entry ); |
189 | |
|
190 | 150 | resources.add( name ); |
191 | 150 | } |
192 | |
|
193 | |
private void addRemappedClass( RelocatorRemapper remapper, JarOutputStream jos, File jar, String name, |
194 | |
InputStream is ) |
195 | |
throws IOException |
196 | |
{ |
197 | 422 | if ( !remapper.hasRelocators() ) |
198 | |
{ |
199 | |
try |
200 | |
{ |
201 | 0 | jos.putNextEntry( new JarEntry( name ) ); |
202 | 0 | IOUtil.copy( is, jos ); |
203 | |
} |
204 | 0 | catch ( ZipException e ) |
205 | |
{ |
206 | 0 | getLogger().warn( "We have a duplicate " + name + " in " + jar ); |
207 | 0 | } |
208 | |
|
209 | 0 | return; |
210 | |
} |
211 | |
|
212 | 422 | ClassReader cr = new ClassReader( is ); |
213 | |
|
214 | 422 | ClassWriter cw = new ClassWriter( cr, 0 ); |
215 | |
|
216 | 422 | ClassVisitor cv = new RemappingClassAdapter( cw, remapper ); |
217 | |
|
218 | 422 | cr.accept( cv, ClassReader.EXPAND_FRAMES ); |
219 | |
|
220 | 422 | byte[] renamedClass = cw.toByteArray(); |
221 | |
|
222 | |
|
223 | 422 | String mappedName = remapper.map( name.substring( 0, name.indexOf( '.' ) ) ); |
224 | |
|
225 | |
try |
226 | |
{ |
227 | |
|
228 | 422 | jos.putNextEntry( new JarEntry( mappedName + ".class" ) ); |
229 | |
|
230 | 422 | IOUtil.copy( renamedClass, jos ); |
231 | |
} |
232 | 0 | catch ( ZipException e ) |
233 | |
{ |
234 | 0 | getLogger().warn( "We have a duplicate " + mappedName + " in " + jar ); |
235 | 422 | } |
236 | 422 | } |
237 | |
|
238 | |
private boolean isFiltered( List filters, String name ) |
239 | |
{ |
240 | 483 | for ( int i = 0; i < filters.size(); i++ ) |
241 | |
{ |
242 | 0 | Filter filter = (Filter) filters.get( i ); |
243 | |
|
244 | 0 | if ( filter.isFiltered( name ) ) |
245 | |
{ |
246 | 0 | return true; |
247 | |
} |
248 | |
} |
249 | |
|
250 | 483 | return false; |
251 | |
} |
252 | |
|
253 | |
private boolean resourceTransformed( List resourceTransformers, String name, InputStream is, List relocators ) |
254 | |
throws IOException |
255 | |
{ |
256 | 61 | boolean resourceTransformed = false; |
257 | |
|
258 | 61 | for ( Iterator k = resourceTransformers.iterator(); k.hasNext(); ) |
259 | |
{ |
260 | 55 | ResourceTransformer transformer = (ResourceTransformer) k.next(); |
261 | |
|
262 | 55 | if ( transformer.canTransformResource( name ) ) |
263 | |
{ |
264 | 5 | transformer.processResource( name, is, relocators ); |
265 | |
|
266 | 5 | resourceTransformed = true; |
267 | |
|
268 | 5 | break; |
269 | |
} |
270 | |
} |
271 | 61 | return resourceTransformed; |
272 | |
} |
273 | |
|
274 | |
private void addResource( Set resources, JarOutputStream jos, String name, InputStream is ) |
275 | |
throws IOException |
276 | |
{ |
277 | 51 | jos.putNextEntry( new JarEntry( name ) ); |
278 | |
|
279 | 51 | IOUtil.copy( is, jos ); |
280 | |
|
281 | 51 | resources.add( name ); |
282 | 51 | } |
283 | |
|
284 | 7 | class RelocatorRemapper |
285 | |
extends Remapper |
286 | |
{ |
287 | |
|
288 | 7 | private final Pattern classPattern = Pattern.compile( "(\\[*)?L(.+);" ); |
289 | |
|
290 | |
List relocators; |
291 | |
|
292 | |
public RelocatorRemapper( List relocators ) |
293 | 7 | { |
294 | 7 | this.relocators = relocators; |
295 | 7 | } |
296 | |
|
297 | |
public boolean hasRelocators() |
298 | |
{ |
299 | 422 | return !relocators.isEmpty(); |
300 | |
} |
301 | |
|
302 | |
public Object mapValue( Object object ) |
303 | |
{ |
304 | 8212 | if ( object instanceof String ) |
305 | |
{ |
306 | 4603 | String name = (String) object; |
307 | 4603 | String value = name; |
308 | |
|
309 | 4603 | String prefix = ""; |
310 | 4603 | String suffix = ""; |
311 | |
|
312 | 4603 | Matcher m = classPattern.matcher( name ); |
313 | 4603 | if ( m.matches() ) |
314 | |
{ |
315 | 0 | prefix = m.group( 1 ) + "L"; |
316 | 0 | suffix = ";"; |
317 | 0 | name = m.group( 2 ); |
318 | |
} |
319 | |
|
320 | 4603 | for ( Iterator i = relocators.iterator(); i.hasNext(); ) |
321 | |
{ |
322 | 4603 | Relocator r = (Relocator) i.next(); |
323 | |
|
324 | 4603 | if ( r.canRelocateClass( name ) ) |
325 | |
{ |
326 | 1 | value = prefix + r.relocateClass( name ) + suffix; |
327 | 1 | break; |
328 | |
} |
329 | 4602 | else if ( r.canRelocatePath( name ) ) |
330 | |
{ |
331 | 0 | value = prefix + r.relocatePath( name ) + suffix; |
332 | 0 | break; |
333 | |
} |
334 | |
} |
335 | |
|
336 | 4603 | return value; |
337 | |
} |
338 | |
|
339 | 3609 | return super.mapValue( object ); |
340 | |
} |
341 | |
|
342 | |
public String map( String name ) |
343 | |
{ |
344 | 101893 | String value = name; |
345 | |
|
346 | 101893 | String prefix = ""; |
347 | 101893 | String suffix = ""; |
348 | |
|
349 | 101893 | Matcher m = classPattern.matcher( name ); |
350 | 101893 | if ( m.matches() ) |
351 | |
{ |
352 | 0 | prefix = m.group( 1 ) + "L"; |
353 | 0 | suffix = ";"; |
354 | 0 | name = m.group( 2 ); |
355 | |
} |
356 | |
|
357 | 101893 | for ( Iterator i = relocators.iterator(); i.hasNext(); ) |
358 | |
{ |
359 | 101893 | Relocator r = (Relocator) i.next(); |
360 | |
|
361 | 101893 | if ( r.canRelocatePath( name ) ) |
362 | |
{ |
363 | 20935 | value = prefix + r.relocatePath( name ) + suffix; |
364 | 20935 | break; |
365 | |
} |
366 | |
} |
367 | |
|
368 | 101893 | return value; |
369 | |
} |
370 | |
|
371 | |
} |
372 | |
|
373 | |
} |