Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ManifestResourceTransformer |
|
| 2.75;2.75 |
1 | package org.apache.maven.plugins.shade.resource; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
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 | * A resource processor that allows the arbitrary addition of attributes to | |
37 | * the first MANIFEST.MF that is found in the set of JARs being processed, or | |
38 | * to a newly created manifest for the shaded JAR. | |
39 | * | |
40 | * @author Jason van Zyl | |
41 | * @since 1.2 | |
42 | */ | |
43 | 0 | public class ManifestResourceTransformer |
44 | implements ResourceTransformer | |
45 | { | |
46 | ||
47 | // Configuration | |
48 | private String mainClass; | |
49 | private Map manifestEntries; | |
50 | ||
51 | // Fields | |
52 | private boolean manifestDiscovered; | |
53 | private Manifest manifest; | |
54 | ||
55 | public boolean canTransformResource( String resource ) | |
56 | { | |
57 | 0 | if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) ) |
58 | { | |
59 | 0 | return true; |
60 | } | |
61 | ||
62 | 0 | return false; |
63 | } | |
64 | ||
65 | public void processResource( String resource, InputStream is, List relocators ) | |
66 | throws IOException | |
67 | { | |
68 | // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior | |
69 | // now which is situational at best. Right now there is no context passed in with the processing so we cannot | |
70 | // tell what artifact is being processed. | |
71 | 0 | if ( !manifestDiscovered ) |
72 | { | |
73 | 0 | manifest = new Manifest( is ); |
74 | 0 | manifestDiscovered = true; |
75 | 0 | IOUtil.close( is ); |
76 | } | |
77 | 0 | } |
78 | ||
79 | public boolean hasTransformedResource() | |
80 | { | |
81 | 0 | return true; |
82 | } | |
83 | ||
84 | public void modifyOutputStream( JarOutputStream jos ) | |
85 | throws IOException | |
86 | { | |
87 | // If we didn't find a manifest, then let's create one. | |
88 | 0 | if ( manifest == null ) |
89 | { | |
90 | 0 | manifest = new Manifest(); |
91 | } | |
92 | ||
93 | 0 | Attributes attributes = manifest.getMainAttributes(); |
94 | ||
95 | 0 | if ( mainClass != null ) |
96 | { | |
97 | 0 | attributes.put( Attributes.Name.MAIN_CLASS, mainClass ); |
98 | } | |
99 | ||
100 | 0 | if ( manifestEntries != null ) |
101 | { | |
102 | 0 | for ( Iterator i = manifestEntries.keySet().iterator(); i.hasNext(); ) |
103 | { | |
104 | 0 | String key = (String) i.next(); |
105 | 0 | attributes.put( new Attributes.Name( key ), manifestEntries.get( key ) ); |
106 | 0 | } |
107 | } | |
108 | ||
109 | 0 | jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) ); |
110 | 0 | manifest.write( jos ); |
111 | 0 | } |
112 | } |