有個時候多人多team協做開發過程當中,會存在臨時修改的二方包,一樣版本須要從新拉取的狀況。發現大部分人包括本身長久以來也是採用最原始的方法,一層層找到對應的目錄刪除對應的文件。某天實在是受不了了,寫了個小工具分享下,小代碼解決小問題。java
外部依賴:fastjson,commons-io,commons-lang3,不要嘲笑,有工具幹嗎不用呢,非得造輪子嗎。apache
import com.alibaba.fastjson.JSON; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; /** * @author tjw */ public class MavenLocalRepoCleaner { /** * coordinateJson * { * "groupId1":"artifactId1:version1,artifactId2:version2...", * "groupId2":"artifactId:version,..." * } */ public static void main(String[] args) { String coordinateJson="{" + "\"top.xbynet.xxx\":\"\"" + "}"; Map<String,String> coordinateMap=JSON.parseObject(coordinateJson,HashMap.class); Path m2Repo= Paths.get(System.getProperty("user.home"),".m2","repository"); coordinateMap.entrySet().stream().forEach(v->{ String groupId=v.getKey(); groupId = groupId.replace('.', File.separatorChar); if(StringUtils.isBlank(v.getValue())){ Path dir = Paths.get(m2Repo.toString(), groupId); try { FileUtils.deleteDirectory(dir.toFile()); } catch (IOException e) { e.printStackTrace(); } }else { String[] artfactIdVers = v.getValue().split(","); for (String str : artfactIdVers) { String ver = ""; if (str.contains(":")) { ver = str.split(":")[1]; } String artfactId = str.split(":")[0]; Path dir = Paths.get(m2Repo.toString(), groupId, artfactId, ver); try { FileUtils.deleteDirectory(dir.toFile()); } catch (IOException e) { e.printStackTrace(); } } } }); } }