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