注意: repository的路径需根据自己的配置更改
Scala代码:
package system.util import java.io.File /** * 删除maven目录下的.lastUpdated文件以及所在文件夹 * * @author 王大仙儿 * @date 2019/12/25 15:37 */ object CleanMavenFile { def main(args: Array[String]): Unit = { val cleanFileBySuffix = new CleanMavenFile() cleanFileBySuffix.statisticsDirectorys("C:\Users\Administrator\.m2\repository") cleanFileBySuffix.deleteDirectorys() } } import scala.collection.mutable class CleanMavenFile { val suffix = ".lastUpdated" val directorys = mutable.Set[String]() def statisticsDirectorys(path: String) { new File(path).listFiles().foreach(file => { if (file.isDirectory) { statisticsDirectorys(file.getPath) } else { if (file.getName.contains(suffix)) { directorys.add(file.getParent) } } }) } def deleteDirectorys(){ var directory: File = null directorys.foreach(line => { directory = new File(line) if (directory.exists()) { directory.listFiles().foreach(file => { if (file.delete()) { println(s"删除文件: ${file.getPath} 成功") } else { println(s"删除文件: ${file.getPath} 失败") } }) if (directory.delete()) { println(s"删除文件夹: ${directory.getPath} 成功") } else { println(s"删除文件夹: ${directory.getPath} 失败") } } }) } }