自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java同步文件到Jboss虛擬目錄

開發(fā) 后端
使用Maven開發(fā)的,有一個問題大家肯定是深惡痛絕。 “每一次開發(fā)完成,都需要maven打包部署”比如某公司自己在maven上開發(fā)了插件,其中包含了自定義邏輯,那么其部署每一次都必須package一把,開發(fā)效率極其低下。使用同步工具,差點讓我吐血,想想還是算了,自己寫個同步的程序.

使用Maven開發(fā)的,有一個問題大家肯定是深惡痛絕。

“每一次開發(fā)完成,都需要maven打包部署”

比如某公司自己在maven上開發(fā)了插件,其中包含了自定義邏輯,那么其部署每一次都必須package一把,開發(fā)效率極其低下。使用同步工具,差點讓我吐血,想想還是算了,自己寫個同步的程序.
 

  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()) {// 目錄不存在會自動創(chuàng)建  
  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()) {// 文件不存在會自動創(chuàng)建  
  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.      * 創(chuàng)建目錄索引 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.  * 這個同步挺管用的,不需要重啟服務(wù).不需要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文件 這里的規(guī)則和同步配置文件一致,就是同步.  
  35.      * 設(shè)置同步的目錄,以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打包部署,如果服務(wù)器支持動態(tài)加載,則不需要重啟。

同步的測試結(jié)果代碼如下:
 

  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]成功! 

很清楚的表示,你修改的文件已經(jīng)同步到目錄目錄了.

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

【編輯推薦】

  1. JBoss的兩種類隔離機制配置說明
責任編輯:艾婧 來源: ITEYE
相關(guān)推薦

2011-03-04 13:35:38

FileZillaSe

2010-05-19 14:46:51

IIS FTP

2010-01-18 17:09:52

VB.NET創(chuàng)建虛擬目

2010-05-18 18:45:10

IIS服務(wù)器

2010-12-10 09:38:55

Windows虛擬目錄

2009-06-16 11:11:25

JBoss目錄數(shù)據(jù)庫連接

2009-06-10 14:00:31

Jboss虛擬主機安裝

2022-12-06 08:29:01

2009-03-09 20:57:28

linuxrsync文件同步備份

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命令復(fù)制文件到多個目錄

2009-06-12 13:59:04

2017-11-14 16:43:13

Java虛擬機線程

2009-06-18 15:15:35

JBoss的配置

2020-11-17 09:55:48

Java

2009-08-31 12:56:36

C#創(chuàng)建文件夾

2013-04-28 15:37:35

JBoss

2013-09-16 16:20:55

自動備份Dropbox
點贊
收藏

51CTO技術(shù)棧公眾號