View Javadoc

1   package org.apache.maven.plugins.shade.filter;
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.File;
23  import java.io.FileInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.plugin.logging.Log;
32  import org.apache.maven.project.MavenProject;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.vafer.jdependency.Clazz;
35  import org.vafer.jdependency.Clazzpath;
36  import org.vafer.jdependency.ClazzpathUnit;
37  
38  /**
39   * A filter that prevents the inclusion of classes not required in the final jar.
40   * 
41   * @author Torsten Curdt
42   */
43  public class MinijarFilter
44      implements Filter
45  {
46  
47      private Log log;
48  
49      private Set removable;
50  
51      private int classes_kept;
52  
53      private int classes_removed;
54  
55      public MinijarFilter( MavenProject project, Log log )
56          throws IOException
57      {
58  
59          this.log = log;
60  
61          Clazzpath cp = new Clazzpath();
62  
63          ClazzpathUnit artifactUnit =
64              cp.addClazzpathUnit( new FileInputStream( project.getArtifact().getFile() ), project.toString() );
65  
66          for ( Iterator it = project.getArtifacts().iterator(); it.hasNext(); )
67          {
68              Artifact dependency = (Artifact) it.next();
69  
70              InputStream is = null;
71              try
72              {
73                  is = new FileInputStream( dependency.getFile() );
74                  cp.addClazzpathUnit( is, dependency.toString() );
75              }
76              finally
77              {
78                  IOUtil.close( is );
79              }
80          }
81  
82          removable = cp.getClazzes();
83          removePackages(artifactUnit);
84          removable.removeAll( artifactUnit.getClazzes() );
85          removable.removeAll( artifactUnit.getTransitiveDependencies() );
86      }
87  
88      private void removePackages(ClazzpathUnit artifactUnit)
89      {
90          Set packageNames = new HashSet();
91          removePackages(artifactUnit.getClazzes(), packageNames);
92          removePackages(artifactUnit.getTransitiveDependencies(), packageNames);
93      }
94  
95      private void removePackages(Set clazzes, Set packageNames)
96      {
97          Iterator it = clazzes.iterator();
98          while(it.hasNext())
99          {
100             Clazz clazz = (Clazz) it.next();
101             String name = clazz.getName();
102             while(name.contains("."))
103             {
104                 name = name.substring(0, name.lastIndexOf('.'));
105                 if(packageNames.add(name))
106                 {
107                     removable.remove(new Clazz(name + ".package-info"));
108                 }
109             }
110         }
111     }
112 
113     public boolean canFilter( File jar )
114     {
115         return true;
116     }
117 
118     public boolean isFiltered( String classFile )
119     {
120         String className = classFile.replace( '/', '.' ).replaceFirst( "\\.class$", "" );
121         Clazz clazz = new Clazz( className );
122 
123         if ( removable.contains( clazz ) )
124         {
125             log.debug( "Removing " + className );
126             classes_removed += 1;
127             return true;
128         }
129 
130         classes_kept += 1;
131         return false;
132     }
133 
134     public void finished()
135     {
136         int classes_total = classes_removed + classes_kept;
137         log.info( "Minimized " + classes_total + " -> " + classes_kept + " ("
138             + (int) ( 100 * classes_kept / classes_total ) + "%)" );
139     }
140 }