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 org.apache.maven.plugins.shade.relocation.Relocator;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.jar.Attributes;
29  import java.util.jar.JarEntry;
30  import java.util.jar.JarFile;
31  import java.util.jar.JarOutputStream;
32  import java.util.jar.Manifest;
33  
34  /**
35   * A resource processor that allows the arbitrary addition of attributes to
36   * the first MANIFEST.MF that is found in the set of JARs being processed, or
37   * to a newly created manifest for the shaded JAR.
38   *
39   * @author Jason van Zyl
40   * @since 1.2
41   */
42  public class ManifestResourceTransformer
43      implements ResourceTransformer
44  {
45  
46      // Configuration
47      private String mainClass;
48  
49      private Map<String, Object> manifestEntries;
50  
51      // Fields
52      private boolean manifestDiscovered;
53  
54      private Manifest manifest;
55  
56      public boolean canTransformResource( String resource )
57      {
58          if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) )
59          {
60              return true;
61          }
62  
63          return false;
64      }
65  
66      public void processResource( String resource, InputStream is, List<Relocator> relocators )
67          throws IOException
68      {
69          // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior
70          // now which is situational at best. Right now there is no context passed in with the processing so we cannot
71          // tell what artifact is being processed.
72          if ( !manifestDiscovered )
73          {
74              manifest = new Manifest( is );
75              manifestDiscovered = true;
76          }
77      }
78  
79      public boolean hasTransformedResource()
80      {
81          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          if ( manifest == null )
89          {
90              manifest = new Manifest();
91          }
92  
93          Attributes attributes = manifest.getMainAttributes();
94  
95          if ( mainClass != null )
96          {
97              attributes.put( Attributes.Name.MAIN_CLASS, mainClass );
98          }
99  
100         if ( manifestEntries != null )
101         {
102             for ( Map.Entry<String, Object> entry : manifestEntries.entrySet() )
103             {
104                 attributes.put( new Attributes.Name( entry.getKey() ), entry.getValue() );
105             }
106         }
107 
108         jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) );
109         manifest.write( jos );
110     }
111 }