java verifying the jar file downloaded by maven

Keywords: Java SHA1 Maven Spring Apache

Sometimes maven is really a hole!

Sometimes it prompts invalid LOC header (bad signat ure signature),

But sometimes we don't prompt anything, the engineering report is wrong, the situation is more moderate, we don't know that we have met several weird

Today, I encountered the first line error of POM prompt. How can it be? No error is reported anywhere else. jar invalid can only be seen when mvn command is used

I also encountered that the whole spring project only reports errors in test, and others do not report errors. The editor prompts Unknown Error~

It's hard to play. I know there must be a problem with one or several jar downloads. But even if you know, do you want to delete them one by one? One is good, sometimes 5 or 6 are really a waste of time

Why don't you just write a code and run~

 

public class MvnCheckJar {

  public static void main(String[] args) throws Exception {
      
    String localMvnPath = "F:/mvnlib";
    // traverse folder ,Find jar\pom Comparison with validation documents,If not,Delete
    getFile(new File(localMvnPath), "jar,pom");
    System.out.println("Complete");
  }

  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);
      }
    }
  }

  /**
   * Verify, delete if no match found
   *
   * @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)){//If unequal,Delete the current file and 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);
    }
  }
}

Posted by Slippy on Fri, 08 Nov 2019 11:12:47 -0800