快速將Maven專案中的jar複製出來
阿新 • • 發佈:2019-02-18
最近本人搭建了一個SSH的Maven專案,想要將其中所依賴的Jar們複製出來,發現了一個批量方法,整理了這篇文章。
1. 整理Maven Jar包們的路徑:
1)在Project中開啟Maven Dependencies的目錄,選中需要複製的Jar們,Ctrl+C ,會發現複製的是Jar們的絕對路徑:
2)將複製的Jar們的絕對路徑們複製到Excel中,在路徑的前後單元格分別加上 逗號前雙引號 & 後雙引號,結合Excel的函式 =CONCATENATE(B3,TRIM(C3),D3) 組合出來新的字串;
2. 寫java類複製檔案:
1)replace掉複製出來的路徑中左斜線/為右斜線\;
2)寫一個複製檔案的java類,如下,執行main函式就可以複製檔案了;replace掉複製出來的路徑中左斜線/為右斜線\;
package com.zmz.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileTest { public static String tmpArr[]={ "D:/workspaces/soft/Sample_repository/org/apache/struts/struts2-core/2.3.16.3/struts2-core-2.3.16.3.jar" ,"D:/workspaces/soft/Sample_repository/org/apache/struts/xwork/xwork-core/2.3.16.3/xwork-core-2.3.16.3.jar" ,"D:/workspaces/soft/Sample_repository/asm/asm/3.3/asm-3.3.jar" ,"D:/workspaces/soft/Sample_repository/asm/asm-commons/3.3/asm-commons-3.3.jar" ,"D:/workspaces/soft/Sample_repository/asm/asm-tree/3.3/asm-tree-3.3.jar" ,"D:/workspaces/soft/Sample_repository/org/freemarker/freemarker/2.3.19/freemarker-2.3.19.jar" ,"D:/workspaces/soft/Sample_repository/ognl/ognl/3.0.6/ognl-3.0.6.jar" ,"D:/workspaces/soft/Sample_repository/javassist/javassist/3.11.0.GA/javassist-3.11.0.GA.jar" ,"D:/workspaces/soft/Sample_repository/commons-fileupload/commons-fileupload/1.3.1/commons-fileupload-1.3.1.jar" }; public static void main(String[] args) throws Exception { for(String tmpStr : tmpArr){ String strArr [] = tmpStr.split("/"); String fileName = strArr[(strArr.length-1)]; System.out.println( fileName ); copyFile(tmpStr, "D:/TestJar/"+fileName, true); } } public static void copyFile(String srcFilename, String destFilename, boolean overwrite) throws IOException { File srcFile = new File(srcFilename); if (!srcFile.exists()) { throw new FileNotFoundException("Cannot find the source file: " + srcFile.getAbsolutePath()); } if (!srcFile.canRead()) { throw new IOException("Cannot read the source file: " + srcFile.getAbsolutePath()); } File destFile = new File(destFilename); if (overwrite == false) { if (destFile.exists()) { return; } } else { if (destFile.exists()) { if (!destFile.canWrite()) { throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath()); } } else { if (!destFile.createNewFile()) { throw new IOException("Cannot write the destination file: " + destFile.getAbsolutePath()); } } } BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; byte[] block = new byte[1024]; try { inputStream = new BufferedInputStream(new FileInputStream(srcFile)); outputStream = new BufferedOutputStream(new FileOutputStream( destFile)); while (true) { int readLength = inputStream.read(block); if (readLength == -1) { break;// end of file } outputStream.write(block, 0, readLength); } } finally { if (inputStream != null) { inputStream.close(); } if (outputStream != null) { outputStream.close(); } } } }