`
gaoyuntao2005
  • 浏览: 319512 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

---JarCleaner工具

阅读更多

java的jar是一个很不错的技术。可是现在开源的发展,使得一个项目中会用到很多很多的jar文件(我们的一个项目中,刚开始lib目录下有超过100兆的jar文件),一直怀疑有些文件是用不到的,但是又不太确定哪些是有用的,哪些是没用的。

想了想,决定还是做个小工具,一劳永逸地解决这个问题吧。

此小工具能完成如下功能:

1、将原来lib路径的所有jar备份到lib\bak目录下;

2、删除不用的jar

那怎么判断哪些是没用的呢?

这里用到了java -verbose:class,这个命令会将所有加载class的过程打印出来,如果是jar中的class, 还会指明是从哪个jar中加载的。

因此,通过在tomcat或者其他应用服务器的启动脚本中,添加-verbose:class,然后运行,即可获得加载信息。将其复制到一个log文件中,作为参数传递给JarCleaner即可。

  1.  
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.HashSet;
  11. import java.util.Set;
  12. /**
  13.  * 
  14.  * JarCleaner is an utility class to remove all un-referenced jar under the webapp\lib path.<br/>
  15.  * Before run this class, you need have two informations prepared. <br/>
  16.  * 1. The webapp lib path, such as: c:\tomcat\webapps\demo\WEB-INF\lib <br/>
  17.  * 2. The log file, which is generated by console, when adding "-verbose:class" argument <br/>
  18.  * in the end of the java command which is used to start the server. 
  19.  * 
  20.  * 
  21.  * @author Leo Liu
  22.  * <a href="http://www.smilingleo.cn">Homepage</a>
  23.  *
  24.  */
  25. public class JarCleaner {
  26.     
  27.     public Set<String> getNecessaryJarSet(String appLibPath , String logFile){
  28.         
  29.         String pattern = "\\[Loaded.*[\\/].*\\.jar\\]";     
  30.         Set<String> jarSet = new HashSet<String>();
  31.         
  32.         BufferedReader br = null;
  33.         try {
  34.             br = new BufferedReader(new FileReader(new File(logFile)));
  35.         } catch (FileNotFoundException ee) {
  36.             System.out.println("Can't find the log file." + ee.getMessage());
  37.         }
  38.         
  39.         String aLine = "";
  40.  
  41.         while(true){
  42.             
  43.             try {
  44.                 aLine = br.readLine();
  45.             } catch (IOException e) {
  46.                 System.out.println("End of log file.");
  47.             }
  48.             
  49.             if (aLine == nullbreak;
  50.             
  51.             if (aLine.matches(pattern)){
  52.                 String[] strs = aLine.split("from");
  53.                 if (strs.length <= 0continue;
  54.                 String jarName = strs[strs.length - 1].trim();
  55.                 if (jarName.startsWith("file")){
  56.                     jarName = jarName.substring(6);
  57.                 }
  58.                 jarName = jarName.substring(0, jarName.length() - 1);
  59.                 if (jarName.indexOf("/") > 0){                  
  60.                     jarName = jarName.replaceAll("[/]""\\\\");
  61.                 }
  62.                 // Only filter the jars in the web app path.
  63.                 if (jarName.startsWith(appLibPath)){                    
  64.                     if (!jarSet.contains(jarName))
  65.                         jarSet.add(jarName);                
  66.                 }
  67.             }
  68.         }
  69.         return jarSet;
  70.     }
  71.  
  72.     public Set<String> backupOriginalJars(String appLibPath){
  73.         File libPath = new File(appLibPath);
  74.         File[] jars = libPath.listFiles();
  75.         Set<String> jarSet = new HashSet<String>();
  76.  
  77.         File bakPath = new File(appLibPath + "\\bak");
  78.         if (!bakPath.exists())
  79.             bakPath.mkdir();
  80.         
  81.         for (File jar : jars){
  82.             String jarName = jar.getAbsolutePath();
  83.             jarName.replaceAll("[/]""\\\\");
  84.             jarSet.add(jarName);
  85.             // Backup the file to bak path
  86.             File newJar = new File(appLibPath + "\\bak\\" + jar.getName());
  87.             copyFile(newJar, jar);
  88.         }
  89.         
  90.         return jarSet;
  91.     }
  92.     
  93.     public void cleanJars(Set<String> allJars, Set<String> jarSet){
  94.         allJars.removeAll(jarSet);
  95.         
  96.         for (String jarName : allJars){
  97.             File oldJar = new File(jarName);
  98.             oldJar.delete();
  99.         }
  100.         
  101.     }
  102.     
  103.     private void copyFile(File targetFile, File file) {
  104.         if (targetFile.exists()) {
  105.             System.out.println("File:" + targetFile.getAbsolutePath()
  106.                     + " already existed, skip that file!");
  107.             return;
  108.         } else {
  109.             createFile(targetFile, true);
  110.         }
  111.         System.out.println("copied " + file.getAbsolutePath() + " to "
  112.                 + targetFile.getAbsolutePath());
  113.         try {
  114.             InputStream is = new FileInputStream(file);
  115.             FileOutputStream fos = new FileOutputStream(targetFile);
  116.             byte[] buffer = new byte[1024];
  117.             while (is.read(buffer) != -1) {
  118.                 fos.write(buffer);
  119.             }
  120.             is.close();
  121.             fos.close();
  122.         } catch (FileNotFoundException e) {
  123.             e.printStackTrace();
  124.         } catch (IOException e) {
  125.             e.printStackTrace();
  126.         }
  127.     }
  128.  
  129.     private void createFile(File file, boolean isFile) {
  130.         if (!file.exists()) {
  131.             if (!file.getParentFile().exists()) {
  132.                 createFile(file.getParentFile(), false);
  133.             } else {
  134.                 if (isFile) {
  135.                     try {
  136.                         file.createNewFile();
  137.                     } catch (IOException e) {
  138.                         e.printStackTrace();
  139.                     }
  140.                 } else {
  141.                     file.mkdir();
  142.                 }
  143.             }
  144.         }
  145.     }
  146.     
  147.     
  148.     /**
  149.      * @param args
  150.      */
  151.     public static void main(String[] args) throws Exception{
  152.         if (args.length != 2){
  153.             System.out.println("Usage : java JarCleaner <webapp lib path> <class verbose log file>\n" +
  154.                     "This command will backup all jar files in your specified webapp lib path,\n" +
  155.                     "and remain the necessary jars only.\n" + 
  156.                     "  <webapp lib path>    : the full path of your web application located.\n" +
  157.                     "  <class verbose file> : the full path of your log file. \n" +
  158.                     "                         use:java -verbose:class to get the log content.\n\n" + 
  159.                     "Note: You must shut down the server before run this command.");
  160.             System.exit(-1);
  161.         }
  162.         String appLibPath = args[0].trim();
  163.         String logFile = args[1].trim();
  164.         
  165.         JarCleaner cleaner = new JarCleaner();
  166.         // Get the referenced jars.
  167.         Set<String> jarSet = cleaner.getNecessaryJarSet(appLibPath, logFile);
  168.         
  169.         // Get all jars in web app lib path.
  170.         Set<String> allJars = cleaner.backupOriginalJars(appLibPath);
  171.         
  172.         // Remove all non-referenced jar files.
  173.         cleaner.cleanJars(allJars, jarSet);
  174.     }
  175.  
  176. }
  177.  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics