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