Maven库中.lastUpdated文件自动清除工具

本文转载自 http://mushiqianmeng.blog.51cto.com/3970029/720448

最近开发noCloud过程中,在更新maven库时,如果网络问不定或者是一些自己手动安装到本地maven库的jar包,在中心库找不到对应的jar,会生成一些.lastUpdated文件,会导致m2e工具无法找到依赖的jar包,从而提示编译错误。

对于该问题,我也没有找到很好的解决方案,只能手动删除一下lastUpdated文件。文件多时十分繁琐。网上看到别人的解决方案也有利用命令行命令,匹配文件扩展名批量删除的。命令行不会,于是就写了几行代码用于删除.lastUpdated文件。

如有其他直接的解决方案,望不吝赐教,写代码实属无奈之举。

  1. public class DelLastUpdated { 
  2.  
  3.     private static PropertyHelper propHelper = new PropertyHelper("config"); 
  4.     private static final String KEY_MAVEN_REPO = "maven.repo"
  5.     private static final String MAVEN_REPO_PATH = propHelper 
  6.             .getValue(KEY_MAVEN_REPO); 
  7.     private static final String FILE_SUFFIX = "lastUpdated"
  8.     private static final Log _log = LogFactory.getLog(DelLastUpdated.class); 
  9.  
  10.     /** 
  11.      * @param args 
  12.      */ 
  13.     public static void main(String[] args) { 
  14.         File mavenRep = new File(MAVEN_REPO_PATH); 
  15.         if (!mavenRep.exists()) { 
  16.             _log.warn("Maven repos is not exist."); 
  17.             return
  18.         } 
  19.         File[] files = mavenRep.listFiles((FilenameFilter) FileFilterUtils 
  20.                 .directoryFileFilter()); 
  21.         delFileRecr(files,null); 
  22.         _log.info("Clean lastUpdated files finished."); 
  23.     } 
  24.  
  25.     private static void delFileRecr(File[] dirs, File[] files) { 
  26.         if (dirs != null && dirs.length > 0) { 
  27.             for(File dir: dirs){ 
  28.                 File[] childDir = dir.listFiles((FilenameFilter) FileFilterUtils 
  29.                 .directoryFileFilter()); 
  30.                 File[] childFiles = dir.listFiles((FilenameFilter) FileFilterUtils 
  31.                 .suffixFileFilter(FILE_SUFFIX)); 
  32.                 delFileRecr(childDir,childFiles); 
  33.             } 
  34.         } 
  35.         if(files!=null&&files.length>0){ 
  36.             for(File file: files){ 
  37.                 if(file.delete()){ 
  38.                     _log.info("File: ["+file.getName()+"] has been deleted."); 
  39.                 } 
  40.             } 
  41.         } 
  42.     } 
  43.  

配置文件:config.properties

  1. maven.repo=D:\\.m2\\repository 

源码下载地址:

 svn: https://svn.code.sf.net/p/maventools/code/trunk/maven-tools

工程里还包括一个批量安装jar包到本地maven库的工具,以前发过,后来做了一些改进和修正。

原文地址:https://www.cnblogs.com/chenying99/p/3127933.html