Java同步文件到Jboss虚拟目录

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

使用Maven开发的,有一个问题大家肯定是深恶痛绝。

“每一次开发完成,都需要maven打包部署”

比如某公司自己在maven上开发了插件,其中包含了自定义逻辑,那么其部署每一次都必须package一把,开发效率极其低下。使用同步工具,差点让我吐血,想想还是算了,自己写个同步的程序.
 

  1. package com.ycl.filter.FileCleaner;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileFilter;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileReader;  
  7. import java.io.FileWriter;  
  8. import java.io.IOException;  
  9. import java.io.Reader;  
  10. import java.io.Writer;  
  11. import java.util.HashMap;  
  12. import java.util.Map;  
  13.  
  14. import org.apache.commons.io.IOUtils;  
  15.  
  16. public class FileSyn {  
  17.  
  18.     static Map<String, String> map = new HashMap<String, String>();  
  19.  
  20.     /**  
  21.      * 同步一个源文件目录到目标文件目录  
  22.      *              默认去.svn不进行同步  
  23.      * @param target  
  24.      * @param source  
  25.      * @param fileName  
  26.      */ 
  27.     public static void synFile(String target, String source) {  
  28.         File sourcePath = new File(source);  
  29.         File targetPath = new File(target);  
  30.         index(sourcePath);  
  31.         synPath(sourcePath, targetPath,new FileFilter(){  
  32.             @Override 
  33.             public boolean accept(File file) {  
  34.                 if(file.getAbsolutePath().contains(".svn")){  
  35.                     return false;  
  36.                 }  
  37.                 return true;  
  38.             }  
  39.  
  40.         });  
  41.     }  
  42.  
  43.     /**  
  44.      * 同步一个源文件目录到目标文件目录  
  45.      *  
  46.      * @param target  
  47.      * @param source  
  48.      * @param fileName  
  49.      */ 
  50.     public static void synFile(String target, String source,FileFilter fileFilter) {  
  51.         File sourcePath = new File(source);  
  52.         File targetPath = new File(target);  
  53.         index(sourcePath);  
  54.         synPath(sourcePath, targetPath,fileFilter);  
  55.     }  
  56.  
  57.     /**  
  58.      * 同步两目录下的文件  
  59.      *                 过滤.svn文件夹下的不需要同步  
  60.      * @param sourcePath  
  61.      * @param targetPath  
  62.      */ 
  63.     public static void synPath(File sourcePath, File targetPath,FileFilter fileFilter) {  
  64.  
  65.             if (sourcePath.isDirectory()) {  
  66.                 File[] files = sourcePath.listFiles(fileFilter);  
  67.                 for (File sourceFile : files) {  
  68.                     try {  
  69.                         if (sourceFile.isDirectory()) {  
  70.                             File targetFile = new File(targetPath, sourceFile  
  71.                                     .getName());  
  72.                             if (!targetFile.exists()) {// 目录不存在会自动创建  
  73.                                 targetFile.createNewFile();  
  74.                             }  
  75.                             synPath(sourceFile, targetFile,fileFilter);  
  76.                         } else if (sourceFile.isFile()) {  
  77.                             File targetFile = new File(targetPath, sourceFile  
  78.                                     .getName());  
  79.                             if (!targetFile.exists()) {// 文件不存在会自动创建  
  80.                                 targetFile.createNewFile();  
  81.                             }  
  82.                             boolean flag = synFile(sourceFile, targetFile);  
  83.                             if (flag) {  
  84.                                 System.out.println("同步文件:[" 
  85.                                         + sourceFile.getAbsolutePath() + "," 
  86.                                         + targetFile.getAbsolutePath() + "]成功!");  
  87.                             }  
  88.                         }  
  89.                     } catch (IOException e) {  
  90.                         System.out.println("源文件:"+sourceFile.getAbsolutePath());  
  91.                         e.printStackTrace();  
  92.                     }  
  93.                 }  
  94.             }  
  95.  
  96.     }  
  97.  
  98.     /**  
  99.      * 同步一个源文件目录到目标文件目录 指定某个文件名. 用源文件覆盖目录文件.[当源文件比目录文件新的时候].  
  100.      *  
  101.      * @param target  
  102.      * @param source  
  103.      * @param fileName  
  104.      */ 
  105.     public static boolean synFile(File sourceFile, File targetFile) {  
  106.         boolean flag = false;  
  107.         Reader reader = null;  
  108.         Writer writer = null;  
  109.         try {  
  110.             if(sourceFile.getAbsolutePath().contains("AddRole")){  
  111.             System.out.println("source最后修改时间:"+sourceFile.lastModified());  
  112.             System.out.println("target最后修改时间:"+targetFile.lastModified());  
  113.             }  
  114.             if (sourceFile.lastModified() > targetFile.lastModified()) {  
  115.                 reader = new FileReader(sourceFile);  
  116.                 writer = new FileWriter(targetFile);  
  117.                 IOUtils.copy(reader, writer);  
  118.                 flag = true;  
  119.             }  
  120.         } catch (FileNotFoundException e) {  
  121.             e.printStackTrace();  
  122.             flag = false;  
  123.         } catch (IOException e) {  
  124.             e.printStackTrace();  
  125.             flag = false;  
  126.         } finally {  
  127.             try {  
  128.                 if (reader != null) {  
  129.                     reader.close();  
  130.                 }  
  131.                 if (writer != null) {  
  132.                     writer.close();  
  133.                 }  
  134.             } catch (IOException e) {  
  135.                 // TODO Auto-generated catch block  
  136.                 e.printStackTrace();  
  137.             }  
  138.         }  
  139.         return flag;  
  140.     }  
  141.  
  142.     /**  
  143.      * 创建目录索引 fileName ==> filePath  
  144.      *  
  145.      * @param sourcePath  
  146.      */ 
  147.     private static void index(File sourcePath) {  
  148.         if (sourcePath.isDirectory()) {  
  149.             File[] files = sourcePath.listFiles();  
  150.             for (File file : files) {  
  151.                 if (file.isDirectory()) {  
  152.                     // map.put(file.h, file.get)  
  153.                 }  
  154.             }  
  155.         }  
  156.     }  

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

测试代码如下:
 

  1. package com.ycl.filter.FileCleaner;  
  2.  
  3. import java.io.File;  
  4. import java.io.FileFilter;  
  5.  
  6. /**  
  7.  * 这个同步挺管用的,不需要重启服务.不需要copy文件  
  8.  * @author yangchunlong.tw  
  9.  *  
  10.  */ 
  11. public class TestFileSyn {  
  12.  
  13.     /**  
  14.      * @param args  
  15.      */ 
  16.     public static void main(String[] args) {  
  17.         // TODO Auto-generated method stub  
  18.         //synConfigFile();1308563280000  
  19.         synClassFile();  
  20.     }  
  21.  
  22.     /**  
  23.      * 同步配置文件  
  24.      * @param target  
  25.      * @param source  
  26.      */ 
  27.     public static void synConfigFile(){  
  28.         String source = "D:\\workspace\\branches\\sportalModule\\sportal-web\\src\\main\\webapp\\web";  
  29.         String target = "D:\\workspace\\branches\\sportalModule\\target\\sportal.war\\web";  
  30.         FileSyn.synFile(target, source);  
  31.     }  
  32.  
  33.     /**  
  34.      * 同步class文件,只同步class文件 这里的规则和同步配置文件一致,就是同步.  
  35.      * 设置同步的目录,以source为基本同步target目录文件  
  36.      */ 
  37.     public static void synClassFile(){  
  38.         String source = "D:\\workspace\\branches\\sportalModule\\sportal-web\\target\\classes";  
  39.         String target = "D:\\workspace\\branches\\sportalModule\\target\\sportal.war\\WEB-INF\\classes";  
  40.         FileSyn.synFile(target, source);  
  41.     }  
  42.  

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

同步的测试结果代码如下:
 

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

很清楚的表示,你修改的文件已经同步到目录目录了.

原文链接:http://a123159521.iteye.com/blog/1099233

【编辑推荐】

  1. JBoss的两种类隔离机制配置说明
责任编辑:艾婧 来源: ITEYE
相关推荐

2011-03-04 13:35:38

FileZillaSe

2010-05-19 14:46:51

IIS FTP

2010-01-18 17:09:52

VB.NET创建虚拟目

2010-05-18 18:45:10

IIS服务器

2010-12-10 09:38:55

Windows虚拟目录

2009-06-16 11:11:25

JBoss目录数据库连接

2009-03-09 20:57:28

linuxrsync文件同步备份

2009-06-10 14:00:31

Jboss虚拟主机安装

2022-12-06 08:29:01

2009-07-30 14:10:40

ASP.NET版本

2009-06-15 17:31:07

2009-06-12 13:40:25

JBoss下载JBoss安装

2016-12-20 09:47:17

Linux命令复制文件到多个目录

2017-11-14 16:43:13

Java虚拟机线程

2020-11-17 09:55:48

Java

2009-06-12 13:59:04

2009-06-18 15:15:35

JBoss的配置

2013-04-28 15:37:35

JBoss

2009-08-31 12:56:36

C#创建文件夹

2017-06-29 09:28:37

OracleMariaDB复制
点赞
收藏

51CTO技术栈公众号