View Javadoc
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.Arrays;
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.apache.maven.plugins.shade.relocation.Relocator;
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  public class ManifestResourceTransformer
44      extends AbstractCompatibilityTransformer
45  {
46      private final List<String> defaultAttributes = Arrays.asList( "Export-Package",
47                                                                    "Import-Package",
48                                                                    "Provide-Capability",
49                                                                    "Require-Capability" ); 
50      
51      // Configuration
52      private String mainClass;
53  
54      private Map<String, Object> manifestEntries;
55  
56      private List<String> additionalAttributes;
57  
58      // Fields
59      private boolean manifestDiscovered;
60  
61      private Manifest manifest;
62  
63      private long time = Long.MIN_VALUE;
64  
65      private String shade;
66  
67      public void setMainClass( String mainClass )
68      {
69          this.mainClass = mainClass;
70      }
71      
72      public void setManifestEntries( Map<String, Object> manifestEntries )
73      {
74          this.manifestEntries = manifestEntries;
75      }
76      
77      public void setAdditionalAttributes( List<String> additionalAttributes )
78      {
79          this.additionalAttributes = additionalAttributes;
80      }
81  
82      @Override
83      public boolean canTransformResource( String resource )
84      {
85          return JarFile.MANIFEST_NAME.equalsIgnoreCase( resource );
86      }
87  
88      @Override
89      public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
90          throws IOException
91      {
92          // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior
93          // now which is situational at best. Right now there is no context passed in with the processing so we cannot
94          // tell what artifact is being processed.
95          if ( !manifestDiscovered )
96          {
97              manifest = new Manifest( is );
98  
99              if ( relocators != null && !relocators.isEmpty() ) 
100             {
101                 final Attributes attributes = manifest.getMainAttributes();
102 
103                 for ( final String attribute : defaultAttributes )
104                 {
105                     final String attributeValue = attributes.getValue( attribute );
106                     if ( attributeValue != null )
107                     {
108                         String newValue = relocate( attributeValue, relocators );
109                         attributes.putValue( attribute, newValue );
110                     }
111                 }
112 
113                 if ( additionalAttributes != null )
114                 {
115                     for ( final String attribute : additionalAttributes )
116                     {
117                         final String attributeValue = attributes.getValue( attribute );
118                         if ( attributeValue != null )
119                         {
120                             String newValue = relocate( attributeValue, relocators );
121                             attributes.putValue( attribute, newValue );
122                         }
123                     }
124                 }
125             }
126 
127             manifestDiscovered = true;
128 
129             if ( time > this.time )
130             {
131                 this.time = time;        
132             }
133         }
134     }
135 
136     @Override
137     public boolean hasTransformedResource()
138     {
139         return true;
140     }
141 
142     @Override
143     public void modifyOutputStream( JarOutputStream jos )
144         throws IOException
145     {
146         // If we didn't find a manifest, then let's create one.
147         if ( manifest == null )
148         {
149             manifest = new Manifest();
150         }
151 
152         Attributes attributes = manifest.getMainAttributes();
153 
154         if ( mainClass != null )
155         {
156             attributes.put( Attributes.Name.MAIN_CLASS, mainClass );
157         }
158 
159         if ( manifestEntries != null )
160         {
161             for ( Map.Entry<String, Object> entry : manifestEntries.entrySet() )
162             {
163                 if ( entry.getValue() == null )
164                 {
165                     attributes.remove( new Attributes.Name( entry.getKey() ) );
166                 }
167                 else
168                 {
169                     attributes.put( new Attributes.Name( entry.getKey() ), entry.getValue() );
170                 }
171             }
172         }
173 
174         JarEntry jarEntry = new JarEntry( JarFile.MANIFEST_NAME );
175         jarEntry.setTime( time );
176         jos.putNextEntry( jarEntry );
177         manifest.write( jos );
178     }
179     
180     private String relocate( String originalValue, List<Relocator> relocators )
181     {
182         String newValue = originalValue;
183         for ( Relocator relocator : relocators )
184         {
185             String value;
186             do
187             {
188                 value = newValue;
189                 newValue = relocator.relocateClass( value );
190             }
191             while ( !value.equals( newValue ) );
192         }
193         return newValue;
194     }
195 
196     /**
197      * The shades to apply this transformer to or no shades if no filter is applied.
198      *
199      * @param shade {@code null}, {@code jar}, {@code test-jar}, {@code sources-jar} or {@code test-sources-jar}.
200      */
201     public void setForShade( String shade )
202     {
203         this.shade = shade;
204     }
205 
206     public boolean isForShade( String shade )
207     {
208         return isUsedForDefaultShading() || this.shade.equalsIgnoreCase( shade );
209     }
210 
211     public boolean isUsedForDefaultShading()
212     {
213         return this.shade == null || this.shade.isEmpty();
214     }
215 }