2010년 7월 25일 일요일

Ant의 FileSet을 Source 이용하기

Apache의 Ant에서는 FileSet라는 파일 및 폴더 관리 방식이 있다.

 

매우 유연하게 폴더 및 파일을 접근할 수 있는데 그부분은 Java 소스에서

 

이용하는 방법을 알아보도록 하자.

 

예제1

  String fileSet = "**/F*.jsp,**/T*.jsp";
  String fileSetExclude = "**/S.jsp";
  File ws = new File("D:/Projects/lucy/text-finder/work/jobs/Test/workspace");
 
        FileSet fs = new FileSet();
        org.apache.tools.ant.Project p = new org.apache.tools.ant.Project();
        fs.setProject(p);
        fs.setDir(ws);  // Root경로를 지정
        fs.setIncludes(fileSet);  //포함할 파일 조건
        fs.setExcludes(fileSetExclude);  //제외할 파일 조건
        DirectoryScanner ds = fs.getDirectoryScanner(p);

        // Any files in the final set
        String[] files = ds.getIncludedFiles();
        if (files.length == 0) {
         System.err.println("FileSet Empty");
            throw new Exception();
        }

        for (String file : files) {
            File f = new File(ws, file);
            if (!f.exists()) {
             System.err.println("Error Find File : " + f);
                continue;
            }
            if (!f.canRead()) {
             System.err.println("Error Read File : " + f);
                continue;
            }
           
            System.out.println("File : " + f.getName());
           
        }

 

 

위의 소스가 간단하기 때문에 쉽게 파악할 수 있을것이다.

 

FileSet의 조건을 주는 방식은 ant의 설명을 참고하길 바라며 콤마(,)를 이용해 다수의 경로조건을 입력할 수 있다.


 

댓글 없음:

댓글 쓰기