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