收集部署文件的小工具(我有点懒)

介绍了一款自编的文件查找与部署工具,用于简化Bug修复后的文件部署流程,特别适用于涉及多个文件的复杂Bug修复场景。该工具能够智能识别并收集本地Tomcat服务器上指定的修改文件,包括处理内部类文件,最终将它们部署到目标服务器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在工作中,现在每天都在修改bug并把修改bug的文件部署到服务器上;刚开始的bug还是相对简单的,一般一个bug对应一个文件,然后把文件放到服务器的对应目录下就好了(该替换替换,该重启服务器重启服务器)。

-------------分割线-----------该项目没有涉及到像jenkins这么高级的东东----------------

再后来,涉及到有关业务的bug或者一个bug的修复需要涉及到很多的文件,每次把这些文件在从本地tomcat发布路径中去一个个找着实有点费劲(页面,css,js类型的文件可以直接从项目中复制然后上传服务器相对路径中替换即可,主要是class文件,必须去本地tomcat路径中去找),而我又是一个相对较懒的人,不想在找文件上浪费我宝贵的时间,不如拿这些时间去看漂亮的同事小姐姐呢,所以我就想着自己写一个找文件的小工具,经过自己的使用,发现还可以。

下面就直接上代码之前,先上一下目录结构截图:

pom.xml文件

<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.9</version>
		</dependency>

		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.21</version>
		</dependency>

		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.21</version>
		</dependency>
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-source-plugin</artifactId>
				<version>2.1.1</version>
				<configuration>
					<source>1.8</source>
                    <target>1.8</target>
					<includePom>true</includePom>
					<excludeResources>true</excludeResources>
					<attach>true</attach>
				</configuration>
				<executions>
					<execution>
						<id>attach-sources</id>
						<goals>
							<goal>jar</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
		<finalName>ygzy-0.4.1</finalName>
	</build>

PatternUtil.java

public class PatternUtil {
	/**
	 * 正则表达式
	 */
	private static final String regex = "[$]\\d";

	/**
	 * 判断内部类的class文件
	 */
	public static boolean isInnerClassFile(String innerClassFileName) {
		Matcher matcher = Pattern.compile(regex).matcher(innerClassFileName);
		return matcher.find();
	}

	/**
	 * 内部类文件名字处理
	 */
	public static String innerClassFileName(String classFileName) {
		StringBuffer finalFileName = new StringBuffer();
		String[] classFileNameSplit = classFileName.split(regex);
		finalFileName.append(classFileNameSplit[0] + classFileNameSplit[1]);
		return finalFileName.toString();
	}

}

CollectDeployFile.java

public class CollectDeployFile extends LoggerSuper{
	
	/**
	 * 本地Tomcat服务器发布的路径
	 */
	private String localTomcatPublishPath;

	/**
	 * 收集的部署文件路径
	 */
	private String deployFilePath;

	/**
	 * 文件总数量
	 */
	private Integer fileTotal = 0;

	/**
	 * 重名的文件集合
	 */
	private List<String> duplicateNames = new ArrayList<String>();

	/**
	 * 文件名集合
	 */
	private List<String> fileNames = new ArrayList<String>();
	
	public CollectDeployFile() {}
	
	/**
	 * 有参构造函数
	 * @param localTomcatPublishPath
	 * @param deployFilePath
	 * @param fileNames
	 */
	public CollectDeployFile(String localTomcatPublishPath, String deployFilePath, 
			List<String> fileNames, boolean packToZip) {
		this.localTomcatPublishPath = localTomcatPublishPath;
		this.deployFilePath = deployFilePath;
		fileNames.forEach(tempFileName -> {
			if(StringUtils.isNotBlank(tempFileName)) {
				this.fileNames.add(tempFileName);
			}
		});
	}
	
	/**
	 * 方法入口
	 */
	public void copyFileExecute() {
		initFileNames();
		Logger.info("[~~~~~~本次共有:{}个文件~~~~~~]", fileTotal);
		if(!duplicateNames.isEmpty()) 
			Logger.info("[~~~~~~其中重复的文件有:{}个文件~~~~~~]", duplicateNames.toString());
		Logger.info("[~~~~~~实际处理的文件个数应为:{}个文件~~~~~~]", (fileTotal - duplicateNames.size()));
		File file = new File(localTomcatPublishPath);
		handler(file);
		Logger.info("[~~~~文件路径:{}~~~~]", deployFilePath);
	}

	/**
	 * 处理文件的方法
	 */
	private void handler(File file) {
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {
			Arrays.asList(files).forEach(tempFile -> {
				if (tempFile.isFile()) {// 是文件
					String tempFileName = tempFile.getName();
					if (tempFileName.contains(".")) {
						if (fileNames.contains(tempFileName)) {
							String fileAbsolutePath = tempFile.getAbsolutePath();
							copyFile(fileAbsolutePath, tempFileName);
						} else if (PatternUtil.isInnerClassFile(tempFileName)
								&& fileNames.contains(PatternUtil.innerClassFileName(tempFileName))) {
							String fileAbsolutePath = tempFile.getAbsolutePath();
							copyFile(fileAbsolutePath, tempFileName);
						}
					}
				} else if (tempFile.isDirectory()) {
					handler(new File(tempFile.getAbsolutePath()));
				}
			});
		}
	}

	/**
	 * 拷贝文件
	 * 
	 * @param srcDir   源文件全路径
	 * @param fileName 文件名称
	 */
	private void copyFile(String srcDir, String fileName) {
		String destDir = deployFilePath + srcDir.replace(localTomcatPublishPath, "");
		try {
			FileUtils.copyFile(new File(srcDir), new File(destDir));
			Logger.info("[~~~~~~文件:{} 拷贝成功O(∩_∩)O!~~~~~~]" ,fileName);
		} catch (IOException e) {
			Logger.info("[~~~~~~文件:{} 拷贝失败 ̄□ ̄||!~~~~~~{} ]", fileName, e.getMessage());
		}
	}

	/**
	 * 初始化文件名集合
	 */
	private void initFileNames() {
		if (fileNames == null || fileNames.size() <= 0) exit("无文件处理 ");
		fileTotal = fileNames.size();
		List<String> tempFileNames = new ArrayList<String>();
		tempFileNames = fileNames;
		if (tempFileNames.size() == fileNames.size())
			fileNames = new ArrayList<String>();
		tempFileNames.forEach(name -> {
			if (name.contains(".java"))
				name = name.substring(0, name.indexOf(".")) + ".class";
			if (!fileNames.contains(name))
				fileNames.add(name);
			else
				duplicateNames.add(name);
		});
	}
	
	/**
	 * 强制退出
	 */
	private void exit(String msg) {
		Logger.info("[~~~~~~{}程序强制退出~~~~~~]", msg);
		System.exit(0);
	}
}

测试类:

public class MainShell {
	
	public static void main(String[] args) {
		String localDeployPath = "G:\\workspace\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp1\\wtpwebapps\\ygzy_webapp\\";
		String serverDeployPath = "C:\\Users\\dell\\Desktop\\webapps\\ygzy_webapp_202008071030\\";
		
		List<String> fileNames = Arrays.asList(//这里你只管写你修改文件的名字(带后缀)
												"Student.java",
												"students.jsp",
												"students.js",
												"students.css"
											);
		
		CollectDeployFile cdf = new CollectDeployFile(localDeployPath, serverDeployPath, fileNames);
		cdf.copyFileExecute();
	}
}

说明:

收集的文件,除了路径和本地tomcat的发布路劲不同外,文件所在的路径统统相同;如果你的java文件中有使用到内部类的话,该该工具也一并将你的内部类文件收集(例如:StudentController.java文件中有一个方法有内部类的使用,那么经过编译后,会有StudentController.class;StudentController$1.class两个文件,收集工具会将这两个文件都收集起来。当然如果你java文件中有多个内部类的话,会统统给你收集起来的);

截止到目前,我用过几次,还没发现什么问题,有啥问题在优化吧;

朋友,如果你觉得对你有用的话,就点赞或者收藏;如果我哪里代码与问题,还烦请您给我留言,我会感激你的。

jar包下载 

源码包下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值