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.Map; | |
26 | import java.util.jar.Attributes; | |
27 | import java.util.jar.JarEntry; | |
28 | import java.util.jar.JarOutputStream; | |
29 | import java.util.jar.Manifest; | |
30 | ||
31 | import org.codehaus.plexus.util.IOUtil; | |
32 | ||
33 | /** | |
34 | * A resource processor that allows the arbitrary addition of attributes to | |
35 | * the first MANIFEST.MF that is found in the set of JARs being processed, or | |
36 | * to a newly created manifest for the shaded JAR. | |
37 | * | |
38 | * @author Jason van Zyl | |
39 | * @since 1.2 | |
40 | */ | |
41 | 0 | public class ManifestResourceTransformer |
42 | implements ResourceTransformer | |
43 | { | |
44 | private static final String MANIFEST_PATH = "META-INF/MANIFEST.MF"; | |
45 | ||
46 | // Configuration | |
47 | private String mainClass; | |
48 | private Map manifestEntries; | |
49 | ||
50 | // Fields | |
51 | private boolean manifestDiscovered; | |
52 | private Manifest manifest; | |
53 | ||
54 | public boolean canTransformResource( String resource ) | |
55 | { | |
56 | 0 | if ( MANIFEST_PATH.equalsIgnoreCase( resource ) ) |
57 | { | |
58 | 0 | return true; |
59 | } | |
60 | ||
61 | 0 | return false; |
62 | } | |
63 | ||
64 | public void processResource( InputStream is ) | |
65 | throws IOException | |
66 | { | |
67 | // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior | |
68 | // now which is situational at best. Right now there is no context passed in with the processing so we cannot | |
69 | // tell what artifact is being processed. | |
70 | 0 | if ( !manifestDiscovered ) |
71 | { | |
72 | 0 | manifest = new Manifest( is ); |
73 | 0 | manifestDiscovered = true; |
74 | 0 | IOUtil.close( is ); |
75 | } | |
76 | 0 | } |
77 | ||
78 | public boolean hasTransformedResource() | |
79 | { | |
80 | 0 | return true; |
81 | } | |
82 | ||
83 | public void modifyOutputStream( JarOutputStream jos ) | |
84 | throws IOException | |
85 | { | |
86 | // If we didn't find a manifest, then let's create one. | |
87 | 0 | if ( manifest == null ) |
88 | { | |
89 | 0 | manifest = new Manifest(); |
90 | } | |
91 | ||
92 | 0 | Attributes attributes = manifest.getMainAttributes(); |
93 | ||
94 | 0 | if ( mainClass != null ) |
95 | { | |
96 | 0 | attributes.put( Attributes.Name.MAIN_CLASS, mainClass ); |
97 | } | |
98 | ||
99 | 0 | if ( manifestEntries != null ) |
100 | { | |
101 | 0 | for ( Iterator i = manifestEntries.keySet().iterator(); i.hasNext(); ) |
102 | { | |
103 | 0 | String key = (String) i.next(); |
104 | 0 | attributes.put( new Attributes.Name( key ), manifestEntries.get( key ) ); |
105 | } | |
106 | } | |
107 | ||
108 | 0 | jos.putNextEntry( new JarEntry( MANIFEST_PATH ) ); |
109 | 0 | manifest.write( jos ); |
110 | 0 | } |
111 | } |