快速將Maven項目中的jar複製出來

最近本人搭建了一個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();
			}
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章