-
Notifications
You must be signed in to change notification settings - Fork 89
/
ManifestResourceTransformer.java
208 lines (177 loc) · 6.4 KB
/
ManifestResourceTransformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package org.apache.maven.plugins.shade.resource;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.apache.maven.plugins.shade.relocation.Relocator;
/**
* A resource processor that allows the arbitrary addition of attributes to
* the first MANIFEST.MF that is found in the set of JARs being processed, or
* to a newly created manifest for the shaded JAR.
*
* @author Jason van Zyl
* @since 1.2
*/
public class ManifestResourceTransformer
extends AbstractCompatibilityTransformer
{
private final List<String> defaultAttributes = Arrays.asList( "Export-Package",
"Import-Package",
"Provide-Capability",
"Require-Capability" );
// Configuration
private String mainClass;
private Map<String, Object> manifestEntries;
private List<String> additionalAttributes;
// Fields
private boolean manifestDiscovered;
private Manifest manifest;
private long time = Long.MIN_VALUE;
private String shade;
public void setMainClass( String mainClass )
{
this.mainClass = mainClass;
}
public void setManifestEntries( Map<String, Object> manifestEntries )
{
this.manifestEntries = manifestEntries;
}
public void setAdditionalAttributes( List<String> additionalAttributes )
{
this.additionalAttributes = additionalAttributes;
}
@Override
public boolean canTransformResource( String resource )
{
if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) )
{
return true;
}
return false;
}
@Override
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
// We just want to take the first manifest we come across as that's our project's manifest. This is the behavior
// now which is situational at best. Right now there is no context passed in with the processing so we cannot
// tell what artifact is being processed.
if ( !manifestDiscovered )
{
manifest = new Manifest( is );
if ( relocators != null && !relocators.isEmpty() )
{
final Attributes attributes = manifest.getMainAttributes();
for ( final String attribute : defaultAttributes )
{
final String attributeValue = attributes.getValue( attribute );
if ( attributeValue != null )
{
String newValue = relocate( attributeValue, relocators );
attributes.putValue( attribute, newValue );
}
}
if ( additionalAttributes != null )
{
for ( final String attribute : additionalAttributes )
{
final String attributeValue = attributes.getValue( attribute );
if ( attributeValue != null )
{
String newValue = relocate( attributeValue, relocators );
attributes.putValue( attribute, newValue );
}
}
}
}
manifestDiscovered = true;
if ( time > this.time )
{
this.time = time;
}
}
}
@Override
public boolean hasTransformedResource()
{
return true;
}
@Override
public void modifyOutputStream( JarOutputStream jos )
throws IOException
{
// If we didn't find a manifest, then let's create one.
if ( manifest == null )
{
manifest = new Manifest();
}
Attributes attributes = manifest.getMainAttributes();
if ( mainClass != null )
{
attributes.put( Attributes.Name.MAIN_CLASS, mainClass );
}
if ( manifestEntries != null )
{
for ( Map.Entry<String, Object> entry : manifestEntries.entrySet() )
{
attributes.put( new Attributes.Name( entry.getKey() ), entry.getValue() );
}
}
JarEntry jarEntry = new JarEntry( JarFile.MANIFEST_NAME );
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );
manifest.write( jos );
}
private String relocate( String originalValue, List<Relocator> relocators )
{
String newValue = originalValue;
for ( Relocator relocator : relocators )
{
String value;
do
{
value = newValue;
newValue = relocator.relocateClass( value );
}
while ( !value.equals( newValue ) );
}
return newValue;
}
/**
* The shades to apply this transformer to or no shades if no filter is applied.
*
* @param shade {@code null}, {@code jar}, {@code test-jar}, {@code sources-jar} or {@code test-sources-jar}.
*/
public void setForShade( String shade )
{
this.shade = shade;
}
public boolean isForShade( String shade )
{
return shade == null || shade.isEmpty() || this.shade.equalsIgnoreCase( shade );
}
}