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