Java同步文件到Jboss虚拟目录

介绍了一种无需频繁Maven打包部署的方法,通过自定义程序实现文件的实时同步,提高开发效率。适用于支持动态加载的服务器环境。

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

使用Maven开发的,有一个问题大家肯定是深恶痛绝。
[b]“每一次开发完成,都需要maven打包部署”[/b]
比如某公司自己在maven上开发了插件,其中包含了自定义逻辑,那么其部署每一次都必须package一把,开发效率极其低下.
使用同步工具,差点让我吐血,想想还是算了,自己写个同步的程序.

package com.ycl.filter.FileCleaner;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;

public class FileSyn {

static Map<String, String> map = new HashMap<String, String>();

/**
* 同步一个源文件目录到目标文件目录
* 默认去.svn不进行同步
* @param target
* @param source
* @param fileName
*/
public static void synFile(String target, String source) {
File sourcePath = new File(source);
File targetPath = new File(target);
index(sourcePath);
synPath(sourcePath, targetPath,new FileFilter(){
@Override
public boolean accept(File file) {
if(file.getAbsolutePath().contains(".svn")){
return false;
}
return true;
}

});
}

/**
* 同步一个源文件目录到目标文件目录
*
* @param target
* @param source
* @param fileName
*/
public static void synFile(String target, String source,FileFilter fileFilter) {
File sourcePath = new File(source);
File targetPath = new File(target);
index(sourcePath);
synPath(sourcePath, targetPath,fileFilter);
}

/**
* 同步两目录下的文件
* 过滤.svn文件夹下的不需要同步
* @param sourcePath
* @param targetPath
*/
public static void synPath(File sourcePath, File targetPath,FileFilter fileFilter) {

if (sourcePath.isDirectory()) {
File[] files = sourcePath.listFiles(fileFilter);
for (File sourceFile : files) {
try {
if (sourceFile.isDirectory()) {
File targetFile = new File(targetPath, sourceFile
.getName());
if (!targetFile.exists()) {// 目录不存在会自动创建
targetFile.createNewFile();
}
synPath(sourceFile, targetFile,fileFilter);
} else if (sourceFile.isFile()) {
File targetFile = new File(targetPath, sourceFile
.getName());
if (!targetFile.exists()) {// 文件不存在会自动创建
targetFile.createNewFile();
}
boolean flag = synFile(sourceFile, targetFile);
if (flag) {
System.out.println("同步文件:["
+ sourceFile.getAbsolutePath() + ","
+ targetFile.getAbsolutePath() + "]成功!");
}
}
} catch (IOException e) {
System.out.println("源文件:"+sourceFile.getAbsolutePath());
e.printStackTrace();
}
}
}

}

/**
* 同步一个源文件目录到目标文件目录 指定某个文件名. 用源文件覆盖目录文件.[当源文件比目录文件新的时候].
*
* @param target
* @param source
* @param fileName
*/
public static boolean synFile(File sourceFile, File targetFile) {
boolean flag = false;
Reader reader = null;
Writer writer = null;
try {
if(sourceFile.getAbsolutePath().contains("AddRole")){
System.out.println("source最后修改时间:"+sourceFile.lastModified());
System.out.println("target最后修改时间:"+targetFile.lastModified());
}
if (sourceFile.lastModified() > targetFile.lastModified()) {
reader = new FileReader(sourceFile);
writer = new FileWriter(targetFile);
IOUtils.copy(reader, writer);
flag = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
flag = false;
} catch (IOException e) {
e.printStackTrace();
flag = false;
} finally {
try {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}

/**
* 创建目录索引 fileName ==> filePath
*
* @param sourcePath
*/
private static void index(File sourcePath) {
if (sourcePath.isDirectory()) {
File[] files = sourcePath.listFiles();
for (File file : files) {
if (file.isDirectory()) {
// map.put(file.h, file.get)
}
}
}
}
}



这里本来想做索引的,后来想想就算了,同步整个目录得了,因为程序的速度太快了,我就不浪费程序代码了,代码多了本身就会崩溃。
测试代码如下:

package com.ycl.filter.FileCleaner;

import java.io.File;
import java.io.FileFilter;

/**
* 这个同步挺管用的,不需要重启服务.不需要copy文件
* @author yangchunlong.tw
*
*/
public class TestFileSyn {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//synConfigFile();1308563280000
synClassFile();
}

/**
* 同步配置文件
* @param target
* @param source
*/
public static void synConfigFile(){
String source = "D:\\workspace\\branches\\sportalModule\\sportal-web\\src\\main\\webapp\\web";
String target = "D:\\workspace\\branches\\sportalModule\\target\\sportal.war\\web";
FileSyn.synFile(target, source);
}

/**
* 同步class文件,只同步class文件 这里的规则和同步配置文件一致,就是同步.
* 设置同步的目录,以source为基本同步target目录文件
*/
public static void synClassFile(){
String source = "D:\\workspace\\branches\\sportalModule\\sportal-web\\target\\classes";
String target = "D:\\workspace\\branches\\sportalModule\\target\\sportal.war\\WEB-INF\\classes";
FileSyn.synFile(target, source);
}

}



你可以测试修改配置文件,或者Java文件,借助eclipse的auto编译功能,可以copy 文件[class,xml...]到指定的目录,不需要重新maven打包部署,如果服务器支持动态加载,则不需要重启。
同步的测试结果代码如下:

同步文件:[D:\workspace\branches\sportalModule\sportal-web\target\classes\sql-map.xml,D:\workspace\branches\sportalModule\target\sportal.war\WEB-INF\classes\sql-map.xml]成功!

很清楚的表示,你修改的文件已经同步到目录目录了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值