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.List;
28  import java.util.Map;
29  import java.util.jar.Attributes;
30  import java.util.jar.JarEntry;
31  import java.util.jar.JarFile;
32  import java.util.jar.JarOutputStream;
33  import java.util.jar.Manifest;
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      implements ResourceTransformer
45  {
46  
47      // Configuration
48      private String mainClass;
49  
50      private Map<String, Attributes> manifestEntries;
51  
52      // Fields
53      private boolean manifestDiscovered;
54  
55      private Manifest manifest;
56  
57      public boolean canTransformResource( String resource )
58      {
59          if ( JarFile.MANIFEST_NAME.equalsIgnoreCase( resource ) )
60          {
61              return true;
62          }
63  
64          return false;
65      }
66  
67      public void processResource( String resource, InputStream is, List<Relocator> relocators )
68          throws IOException
69      {
70          // We just want to take the first manifest we come across as that's our project's manifest. This is the behavior
71          // now which is situational at best. Right now there is no context passed in with the processing so we cannot
72          // tell what artifact is being processed.
73          if ( !manifestDiscovered )
74          {
75              manifest = new Manifest( is );
76              manifestDiscovered = true;
77              IOUtil.close( is );
78          }
79      }
80  
81      public boolean hasTransformedResource()
82      {
83          return true;
84      }
85  
86      public void modifyOutputStream( JarOutputStream jos )
87          throws IOException
88      {
89          // If we didn't find a manifest, then let's create one.
90          if ( manifest == null )
91          {
92              manifest = new Manifest();
93          }
94  
95          Attributes attributes = manifest.getMainAttributes();
96  
97          if ( mainClass != null )
98          {
99              attributes.put( Attributes.Name.MAIN_CLASS, mainClass );
100         }
101 
102         if ( manifestEntries != null )
103         {
104             for ( Map.Entry<String, Attributes> entry : manifestEntries.entrySet() )
105             {
106                 attributes.put( new Attributes.Name( entry.getKey() ), entry.getValue() );
107             }
108         }
109 
110         jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) );
111         manifest.write( jos );
112     }
113 }