maven jar 包 自动统一去重升级
程序员文章站
2022-04-13 14:24:45
...
package java2015.java09.javatest; import java.io.File; import java.io.ObjectInputStream.GetField; import java.util.HashMap; import java.util.Map; public class Test { final static String targetFilePath = "C:/Users/Administrator/Desktop/fa-非maven-可启动-lib/"; final static String sourceFilePath = "C:/Users/Administrator/Desktop/fa-maven-lib/"; final static String resultFilePath = "C:/Users/Administrator/Desktop/fa-带版本-非maven-lib/"; static Map<String, String> targetFilemap = new HashMap<String, String>(); static Map<String, String> sourceFilemap = new HashMap<String, String>(); static Map<String, String> resultFilemap = new HashMap<String, String>(); public static void main(String[] args) { File file1 = new File(targetFilePath); File[] targetFileList = file1.listFiles(); for (File targetFile : targetFileList) { String targetFileName = targetFile.getName(); targetFileName = targetFileName.replace(".jar", ""); number(targetFilemap, targetFileName); } File file2 = new File(sourceFilePath); File[] sourceFileList = file2.listFiles(); for (File sourceFile : sourceFileList) { String sourceFileName = sourceFile.getName(); sourceFileName = sourceFileName.replace(".jar", ""); number(sourceFilemap, sourceFileName); } result(resultFilemap, targetFilemap, sourceFilemap); for (String jarKey : resultFilemap.keySet()) { String selectedNum = resultFilemap.get(jarKey); String[] selectedNumArr = selectedNum.split("#"); String fileName = null; if ("1".equals(selectedNumArr[0])) { if (selectedNumArr.length == 1 || "".equals(selectedNumArr[1])) { fileName = jarKey + ".jar"; } else { fileName = jarKey + "-" + selectedNumArr[1] + ".jar"; } FileUtil.copyFile(targetFilePath + fileName, resultFilePath + fileName); } else { if (selectedNumArr.length == 1 || "".equals(selectedNumArr[1])) { fileName = jarKey + ".jar"; } else { fileName = jarKey + "-" + selectedNumArr[1] + ".jar"; } FileUtil.copyFile(sourceFilePath + fileName, resultFilePath + fileName); } } } static void result(Map<String, String> map1, Map<String, String> map2, Map<String, String> map3) { System.out.printf("%30s %20s %20s", "jar包名","非maven包" ,"maven包"); System.out.println(); for (String jarKey : map2.keySet()) { String targetNum = map2.get(jarKey); String sourceNum = map3.get(jarKey); System.out.printf("%30s %20s %20s", jarKey,targetNum ,sourceNum); String selectedNum = selectedNum(targetNum, sourceNum); map1.put(jarKey, selectedNum); } } // 1&1.1.0-cdn // 1 从 target取 ;2 从source取 static String selectedNum(String targetNum, String sourceNum) { String temp=""; if (targetNum == null || targetNum == "" || sourceNum == null || sourceNum == "") { if (targetNum == null || targetNum == "") { temp= 2 + "#" + sourceNum; } else { temp= 1 + "#" + targetNum; } } else { if (targetNum.compareTo(sourceNum) < 0) { System.out.printf("%30s", "maven 比 非maven 版本高"); temp= 2 + "#" + sourceNum; } else { temp= 1 + "#" + targetNum; } } System.out.println(); return temp; } static void number(Map<String, String> map, String fileName) { int i = 0; int index = 0; boolean flag = false; if (!fileName.contains("-")) { index = fileName.length(); flag = true; } char[] charArr = fileName.toCharArray(); while (!flag && i < charArr.length) { char achar = charArr[i]; String astring = String.valueOf(achar); if (astring.contains("-")) { char achar2 = charArr[i + 1]; String astring2 = String.valueOf(achar2); try { int aint2 = Integer.parseInt(astring2); index = i; break; } catch (Exception e) { i++; continue; } } i++; if (i == fileName.length()) { index = fileName.length(); flag = true; } } char[] resultName = null; char[] resultNum = null; if (flag) { resultName = new char[index]; System.arraycopy(charArr, 0, resultName, 0, index); resultNum = new char[0]; } else { resultName = new char[index]; System.arraycopy(charArr, 0, resultName, 0, index); resultNum = new char[charArr.length - index - 1]; System.arraycopy(charArr, index + 1, resultNum, 0, charArr.length - index - 1); } // System.out.println( String.valueOf(resultName )+"|"+String.valueOf(resultNum)+"|"); map.put(String.valueOf(resultName), String.valueOf(resultNum)); } }
package java2015.java09.javatest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.commons.io.FileUtils; /** * 与文件相关的工具类库。<br> * 其内容包括文件/目录更名,文件/目录删除,文件/目录移动。 * * @author baoyou curiousby@163.com */ public class FileUtil { public static boolean copyDirectory(String oldPath, String newPath) { File b = new File(newPath); File a = new File(oldPath); try { FileUtils.copyDirectoryToDirectory(a, b); } catch (IOException e) { return false; } return true; } public static boolean moveDirectory(String oldPath, String newPath) { File b = new File(newPath); File a = new File(oldPath); return a.renameTo(b); } /** * 将文件oldFile移为newFile * * @param oldFile * @param newFile * @return */ public static boolean moveFile(String oldFile, String newFile) { File a = new File(oldFile); File b = new File(newFile); return moveFile(a, b); } /** * 将文件移至指定目录,文件名不变 * * @param file * @param path * @return */ public static boolean moveFile(File file, String path) { File tmp = new File(path); if (!tmp.exists()) tmp.mkdirs(); File b = new File(path + File.separatorChar + file.getName()); return moveFile(file, b); } /** * 将文件移至指定目录,文件名不变 * * @param file * @param path * @return 移动是否成功 */ public static boolean moveFile(File srcFile, File dstFile) { return srcFile.renameTo(dstFile); } public static boolean deleteDirectory(String sPath) { // 如果sPath不以文件分隔符结尾,自动添加文件分隔符 if (!sPath.endsWith(File.separator)) { sPath = sPath + File.separator; } File dirFile = new File(sPath); // 如果dir对应的文件不存在,或者不是一个目录,则退出 if (!dirFile.exists() || !dirFile.isDirectory()) return false; return deleteDirectory(dirFile); } public static boolean copyFile(String oldPath, String newPath) { boolean bool = false; File newFile = new File(newPath); InputStream inStream = null; OutputStream outStream = null; try { // 复制文件(保留上传的原文件,新的文件名为id_name.zip) inStream = new FileInputStream(oldPath); newFile.createNewFile(); outStream = new FileOutputStream(newPath); byte[] by = new byte[2048]; while (inStream.available() > 0) { int i = inStream.read(by); outStream.write(by, 0, i); } } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (null != inStream) { try { inStream.close(); } catch (IOException e) { } } if (null != outStream) { try { outStream.flush(); } catch (IOException e) { System.err.println(e.getCause()); } try { outStream.close(); } catch (IOException e) { System.err.println(e.getCause()); } bool = true; System.out.println("复制完毕! "); } } return bool; } public static boolean deleteDirectory(File f) { boolean flag = true; // 删除文件夹下的所有文件(包括子目录) File[] files = f.listFiles(); for (int i = 0; i < files.length; i++) { // 删除子文件 if (files[i].isFile()) { flag = deleteFile(files[i].getAbsolutePath()); if (!flag) break; } // 删除子目录 else { flag = deleteDirectory(files[i].getAbsolutePath()); if (!flag) break; } } if (!flag) return false; // 删除当前目录 if (f.delete()) { return true; } else { return false; } } public static boolean deleteFile(String sPath) { File file = new File(sPath); if (file.isFile() && file.exists()) { return file.delete(); } return false; } /** * 装输入流写入文件中 * * @param is * @param f * @return */ public static boolean writeFile(InputStream is, File f) { FileOutputStream fos = null; try { fos = new FileOutputStream(f); byte[] buf = new byte[10240]; int i; while ((i = is.read(buf)) > 0) fos.write(buf, 0, i); } catch (IOException e) { e.printStackTrace(); return false; } finally { if (is != null) try { is.close(); } catch (IOException e) { } if (fos != null) try { fos.close(); } catch (IOException e) { } } return true; } }
代码 之前 用的是 string 转字符串 ,后来发现这也太麻烦了,之后转为 字符串匹配。
代码修改后 ,,,,,
package java2015.java09.javatest; import java.io.File; import java.io.ObjectInputStream.GetField; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Test2 { final static String targetFilePath = "C:/Users/Administrator/Desktop/fa-非maven-可启动-lib/"; final static String sourceFilePath = "C:/Users/Administrator/Desktop/fa-maven-lib/"; final static String resultFilePath = "C:/Users/Administrator/Desktop/fa-带版本-非maven-lib/"; static Map<String, String> targetFilemap = new HashMap<String, String>(); static Map<String, String> sourceFilemap = new HashMap<String, String>(); static Map<String, String> resultFilemap = new HashMap<String, String>(); public static void main(String[] args) { File file1 = new File(targetFilePath); File[] targetFileList = file1.listFiles(); for (File targetFile : targetFileList) { String targetFileName = targetFile.getName(); targetFileName = targetFileName.replace(".jar", ""); number(targetFilemap, targetFileName); } File file2 = new File(sourceFilePath); File[] sourceFileList = file2.listFiles(); for (File sourceFile : sourceFileList) { String sourceFileName = sourceFile.getName(); sourceFileName = sourceFileName.replace(".jar", ""); number(sourceFilemap, sourceFileName); } result(resultFilemap, targetFilemap, sourceFilemap); for (String jarKey : resultFilemap.keySet()) { String selectedNum = resultFilemap.get(jarKey); String[] selectedNumArr = selectedNum.split("#"); String fileName = null; if ("1".equals(selectedNumArr[0])) { if (selectedNumArr.length == 1 || "".equals(selectedNumArr[1])) { fileName = jarKey + ".jar"; } else { fileName = jarKey + "-" + selectedNumArr[1] + ".jar"; } FileUtil.copyFile(targetFilePath + fileName, resultFilePath + fileName); } else { if (selectedNumArr.length == 1 || "".equals(selectedNumArr[1])) { fileName = jarKey + ".jar"; } else { fileName = jarKey + "-" + selectedNumArr[1] + ".jar"; } FileUtil.copyFile(sourceFilePath + fileName, resultFilePath + fileName); } } } static void result(Map<String, String> map1, Map<String, String> map2, Map<String, String> map3) { System.out.printf("%30s %20s %20s", "jar包名","非maven包" ,"maven包"); System.out.println(); for (String jarKey : map2.keySet()) { String targetNum = map2.get(jarKey); String sourceNum = map3.get(jarKey); String targetNumSatring = targetNum==null?"非maven不存在":targetNum==""?"没有版本号":targetNum; String sourceNumSatring = sourceNum==null?"maven不存在":sourceNum==""?"没有版本号":sourceNum; System.out.printf("%30s %20s %20s", jarKey,targetNumSatring,sourceNumSatring); String selectedNum = selectedNum(targetNum, sourceNum); map1.put(jarKey, selectedNum); } } // 1&1.1.0-cdn // 1 从 target取 ;2 从source取 static String selectedNum(String targetNum, String sourceNum) { String temp=""; if (targetNum == null || targetNum == "" || sourceNum == null || sourceNum == "") { if (sourceNum == null || sourceNum == "") { temp= 1 + "#" + targetNum; }else{ temp= 2 + "#" + sourceNum; } } else { if (targetNum.compareTo(sourceNum) < 0) { System.out.printf("%30s", "maven比非maven版本高"); temp= 2 + "#" + sourceNum; }else if (targetNum.compareTo(sourceNum) == 0) { System.out.printf("%30s", "版本相同"); temp= 1 + "#" + targetNum; }else { System.out.printf("%30s", "非maven比maven版本高"); temp= 1 + "#" + targetNum; } } System.out.println(); return temp; } static void number(Map<String, String> map, String fileName) { Pattern p = Pattern.compile("\\-\\d"); // 正则表达式 Matcher m = p.matcher(fileName); // 操作的字符串 boolean aboolean = m.find(); String resultName=""; String resultNum =""; if (aboolean) { int start = m.start(0); resultName= fileName.substring(0, start); resultNum = fileName.substring(start+1, fileName.length()); }else{ resultName = fileName; resultNum =""; } map.put( resultName , resultNum ); } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
推荐阅读