快速将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();
			}
		}
	}
}

 

 

 

 
 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值