java校驗maven下載的jar文件

有時候maven真的很坑!spring

有時候提示invalid LOC header (bad signat signature),apache

但又有時候什麼都不提示,工程報錯,狀況有肯多中,不知道你們遇到過幾種詭異的.maven

不少人說加-U參數或在maven插件選擇強制刷新等操做,但很很差使,一點用都沒有.編輯器

今天我就遇到POM提示第一行錯誤,這怎麼可能?其餘任何地方都不報錯,用mvn命令的時候才能看到jar invalid.spa

我還遇到整個spring的項目只有test報錯,其餘的都不報錯,編輯器裏提示的Unknown Error~插件

真無法玩了~我知道確定有一個或幾個jar下載的有問題.但就算你知道了難道一個一個去找刪?一個還好說,有時候5,6個真是浪費時間.code

不如就寫個代碼跑一下吧~blog

 

public class MvnCheckJar {

  public static void main(String[] args) throws Exception {
      
    String localMvnPath = "F:/mvnlib";
    // 遍歷文件夾,找出jar\pom和效驗文件進行對比,若是不相符,則刪除
    getFile(new File(localMvnPath), "jar,pom");
    System.out.println("完畢");
  }

  public static void getFile(File path, String suffixs) throws Exception {
    String[] suffixs_ = new String[] {};
    if (suffixs != null) suffixs_ = suffixs.split(",");
    if (path.isFile()) {
      for (String suffix : suffixs_) {
        if (path.getName().endsWith(suffix)) {
//            System.out.println(path.getAbsolutePath() ); 
          handler(path);
        }
      }
    } else {
      File[] ff = path.listFiles();
      if(ff!=null)
      for (File x : ff) {
        getFile(x, suffixs);
      }
    }
  }

  /**
   * 驗證,發現不匹配則刪除
   *
   * @throws IOException
   */
  public static void handler(File f) throws Exception {
    File fsha1 = new File(f.getAbsolutePath() + ".sha1");
    if (fsha1.exists()) {
      String sha1 =
          FileUtils.readFileToString(fsha1, "utf-8").replaceAll("(?m).*(\\w{40}).*", "$1").replaceAll("\\n|\\r", "");
      String currsha1 = sha1(f);
      if(!sha1.equals(currsha1)){//若是不等,則刪除 當前文件和sha1
//          System.out.println("sha1file: " + sha1 ); 
          fsha1.delete();
          f.delete();
      System.out.println(sha1 + " , " + currsha1 + " , " + f.getAbsolutePath());
      
      }

    } else {
      f.delete();
    }
  }

  public static String sha1(File f) throws Exception {
    try (FileInputStream fis = new FileInputStream(f)) {
      return org.apache.commons.codec.digest.DigestUtils.sha1Hex(fis);
    }
  }
}
相關文章
相關標籤/搜索