루비 'fileutils.rb' 라이브러리에 보면 FileUtils 모듈이 있습니다. FileUtils에는 파일/디렉토리를 다루기 위한 기본적인 유틸리티 메소드가 정의되어 있는데, 메소드명은 유닉스에서 우리가 쓰던 명령과 유사합니다. cp(), mkdir(), rm(), mv() 처럼요 ....

rake는 기본적으로 이 FileUtils 모듈을 include하고 있기 때문에 rake에 기반을 두고 있는 buildr에서도 거리낌없이 FileUtils의 메소드를 사용할 수 있습니다.

mkdir 'build/myproject/bin'
cp 'src/abc.xml.dev' 'build/myproject/bin/abc.xml'

Ant에서는 <copy>, <mkdir>, <delete>, <move> 등의 코어 타스크를 이용하여 이런 작업을 처리했죠.( Maven은 maven-antrun-plugin 플러그인을 통해서 ant의 타스크를 간접 호출해서 작업을 합니다. 좀 귀찮죠)

 <copy file="myfile.txt" tofile="mycopy.txt"/>

작업할 파일이 하나가 아니면 어떡할까요? Ant에서는 파일의 집합을 FileSet으로 표현합니다.
예를들어 소스 디렉토리 내의 java 소스를 제외한 파일들을 빌드 디렉토리로 복사하는 작업은 아래처럼 정의할 수 있습니다. 여기서 **/*.java 와 같은 경로 표현은 Ant에서 정의한 패턴입니다. Ant style 경로패턴이라고도 하죠(?)

  <copy todir="../build/classes">
    <fileset dir="src_dir">
      <exclude name="**/*.java"/>
    </fileset>
  </copy>
            또는
  <copy todir="../build/classes">
    <fileset dir="src_dir" excludes="**/*.java"/>
  </copy>

rake/buildr에는 Ant의 FileSet에 대응하는 Rake::FileList 클래스가 있습니다. FileList는 주어진 glob 패턴과 매치되는 파일의 목록을 리턴합니다.

FileList['src_dir/**/*.java']
   => src_dir 디렉토리 내의 모든 .java 파일 목록 리턴

FileList['web/**/*'].exclude('**/*.jsp','web/WEB-INF') 
   => web 디렉토리 내의 .jsp 파일 및 WEB-INF 디렉토리를 제외한 모든 파일 목록 리턴

FileList와 비슷한 것으로 Dir.glob()이 있는데요, FileList는 CVS, .svn, .bak 등의 디렉토리/파일을 기본적으로 목록에서 제외하는 반면 Dir.glob()은 그런 것들도 포함해서 리턴합니다. 그러니까 빌드스크립트에서는 Dir.glob() 보다는 FileList를 쓰는게 좋습니다.

참고적으로 File.fnmatch(패턴,파일경로) 메소드가 있습니다. 이 메소드는 주어진 파일의 경로가 지정된 패턴에 부합하는지 판단합니다. FileList[]나 Dir.glob()과 마찬가지로 패턴은 glob 형식입니다.
주의하실 점은 FileList[패턴]의 패턴, FileList[].exclude(패턴)의 패턴, File.fnmatch(패턴)의 패턴이 조금씩 차이가 있는 것 같다는 겁니다. 반드시 적용전 테스트를 해보셔야 합니다. 그리고 File.fnmatch가 패턴은 { } 표현을 지원하지 않습니다. 즉 File.fnmatch()에서 'A.html' 파일은 '**/*.{html,gif}' 패턴과 매치하지 않습니다.

내용이 길어져서 파일집합에서 원하는 파일들만 복사하는 작업과 리소스 파일 치환에 대한 예제는 다음글로 넘깁니다.
Posted by 에코지오
,
Maven은 빌드에 필요한 라이브러리를 아래와 같이 설정합니다.

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.2</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>commons-fileupload</groupId>
   <artifactId>commons-fileupload</artifactId>
   <version>1.1.1</version>
  </dependency>
  <dependency>
   <groupId>commons-lang</groupId>
   <artifactId>commons-lang</artifactId>
   <version>2.1</version>
  </dependency>
  ... ...
 </dependencies>

Buildr에서는 compile.with 메소드에 라이브러리 목록을 넘겨주어 의존성을 추가할 수 있습니다.

compile.with "org.apache.axis2:axis2:jar:1.2", "commons-fileupload:commons-fileupload:jar:1.1.1"

compile.with 메소드가 받아들이는 의존 라이브러리의 몇가지의 타입이 존재합니다.

1. 아티팩트(artifact) 스펙
위의 메이븐 pom에서 xml 형식으로 지정한 coordinates를 다음의 형식으로 지정합니다.

groupId:artifactId:packaging:version
groupId:artifactId:packaging:classifier:version

* 위의 형식 뿐 아니라 다양한 포맷으로 스펙을 지정할 수 있습니다. 여기를 참조하세요.

이렇게 아티팩트 스펙 형식으로 의존성을 추가하면 Buildr은 메이븐 레포지토리에서 해당 라이브러리를 다운받아 로컬에 설치합니다.

2. 다른 프로젝트
만일 다른 프로젝트의 패키징 결과물에 의존하고 있다면 그 프로젝트를 넘겨주면 됩니다.

compile.with projects('another-project', 'other-project')

3. 파일/디렉토리 경로
로컬에 존재하는 파일/디렉토리의 경로를 지정하여 의존성을 추가할 수 있습니다.
메이븐에서는 로컬 파일을 의존성에 추가하기 위해서 스코프를 system으로 잡고 파일의 절대경로를 지정하였습니다.

  <dependency>
   <groupId>weblogic</groupId>
   <artifactId>weblogic-api</artifactId>
   <version>10.0</version>
   <scope>system</scope>
   <systemPath>C:/bea/weblogic/xxx/wls-api.jar</systemPath>
  </dependency>

Buildr에서는 프로젝트에 대한 상대경로 또는 절대경로 모두 가능합니다.

compile.with 'C:/bea/weblogic/xxx/wls-api.jar' , 'lib/my-api.jar',

경로 지정시 path_to 메소드를 활용하는 것이 좋습니다.

compile.with path_to(:source,:main,:webapp,'WEB-INF/lib/my-api.jar')

또한 glob 패턴을 적용하여 파일을 지정할 수 있습니다. 메이븐도 이런게 되면 참 좋겠습니다.

compile.with FileList[path_to(:source,:main,:webapp,'WEB-INF/lib/*.jar'), 'lib/**/*.jar']

여기서 Rake::FileList를 사용했습니다. FileList 없이 그냥 패턴 문자열을 그대로 compile.with 메소드에 넘겨줄 수도 있습니다. 그러나 그렇게 되면 패턴 문자열이 해석없이 그대로 compile.dependencies에 할당되기 때문에 (단위테스트 등에서) 예상치못한 문제가 발생할 수 있습니다. 그래서 FileList를 통해 패턴을 해석한 뒤에 compile.with에 넘겨주는 것을 권장합니다.

Buildr에서는 놀랍게도 파일뿐 아니라 디렉토리도 의존성에 추가하는게 가능합니다.

compile.with '../AAA/classes'

다만, 이렇게 디렉토리를 의존성에 추가하고 war로 패키징하게 되면 war에 그 디렉토리가 포함이 됩니다. 이때는 그 디렉토리를 아래처럼 패키지 libs에서 빼주면 됩니다.

DEPEND_CLASSES =  '../AAA/classes'
compile.with DEPEND_CLASSES
package(:war).libs -= artifacts(DEPEND_CLASSES)

Posted by 에코지오
,
Maven에서는 자바 소스 컴파일시 옵션을 아래와 같이 추가할 수 있었습니다.

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
      <source>1.5</source>
      <target>1.5</target>
      <encoding>UTF-8</encoding>

    </configuration>
  </plugin>

Buildr에서는 compile 타스크의 options 속성을 통하거나

compile.options.target = '1.5'
compile.options.source = '1.5'
compile.options.other = ['-encoding', 'UTF-8']

또는 using 메소드를 사용하여 컴파일 옵션을 설정할 수 있습니다.
 
compile.using :target=>'1.5', :source=>'1.5', :other=>['-encoding', 'UTF-8']

옵션 설정에 사용할 수 있는 심볼은 다음과 같습니다.

  • :warnings — Issue warnings when compiling. True when running in verbose mode.
  • :debug — Generates bytecode with debugging information. Set from the debug              
  • :deprecation — If true, shows deprecation messages. False by default.
  • :source — Source code compatibility.
  • :target — Bytecode compatibility.
  • :lint — Lint option is one of true, false (default), name (e.g. ‘cast’) or array.
  • :other — Array of options passed to the compiler (e.g. ’-implicit:none’)
  • Posted by 에코지오
    ,
    Buildr의 기본 프로젝트 구조는 Maven2의 그것과 거의 동일합니다.

      src
      |__main
      |    |__java              <-- Source files to compile
      |    |__resources      <-- Resources to copy
      |    |__webapp         <-- For WARs
      |__test
      |    |__java              <-- Source files to compile (tests)
      |    |__resources      <-- Resources to copy (tests)
    target                       <-- Packages created here
      |__classes             <-- Generated when compiling
      |__resources          <-- Copied (and filtered) from resources
      |__test/classes       <-- Generated when compiling tests
      |__test/resources    <-- Copied (and filtered) from resources
    reports                     <-- Test, coverage and other reports

    Maven에서 기본 프로젝트 디렉토리 구조를 아래처럼 세팅하여 변경할 수 있었습니다.

     <build>
      <directory>target</directory>
      <sourceDirectory>src</sourceDirectory>
      <outputDirectory>web/WEB-INF/classes</outputDirectory>
      <resources>
       <resource>
        <directory>src</directory>
        <excludes>
         <exclude>**/*.java</exclude>
        </excludes>
       </resource>
      </resources>
        ... ....
       <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.1-alpha-2</version>
        <configuration>
         <warSourceDirectory>web</warSourceDirectory>
         <webappDirectory>${project.build.directory}/webapp</webappDirectory>
        </configuration>
       </plugin>


    Buildr도 디렉토리 구조를 바꿀 수 있습니다. 프로젝트 정의 메소드에 Layout 인스턴스를 전달해주면 됩니다.
    ( http://incubator.apache.org/buildr/extending.html#using_alternative_layouts )

    my_layout = Layout.new
    my_layout[:source, :main, :java] = 'src'
    my_layout[:source, :main, :resources] = 'src'
    my_layout[:source, :test, :java] = 'test'
    my_layout[:source, :test, :resources] = 'test'
    my_layout[:source, :main, :webapp] = 'web'

    my_layout[:target, :main, :classes] = 'web/WEB-INF/classes'
    my_layout[:target, :main, :resources] = 'web/WEB-INF/classes'
    my_layout[:target, :test, :classes] = 'target/test-classes'
    my_layout[:target, :test, :resources] = 'target/test-classes'
    my_layout[:target, :main, :webapp] = 'target/webapp'
    my_layout[:target, :main, :htdocs] = 'target/htdocs'

    my_layout[:reports] = 'target/reports'

    define "MyProject", :layout=>my_layout do

      project.version = VERSION_NUMBER
      project.group = GROUP
      manifest["Implementation-Vendor"] = COPYRIGHT
      ... ...
      resources.exclude '**/*.java'
      test.resources.exclude '**/*.java'
      ... ...
    end


    (위에서 :htdocs는 제가 임의로 사용한 심볼입니다)

    'Build&Deploy > Buildr' 카테고리의 다른 글

    Buildr : 리소스 필터링  (0) 2008.12.09
    Buildr : 파일집합 복사하기  (0) 2008.12.05
    Buildr : 파일 집합 선택  (0) 2008.12.01
    Buildr : 의존성(dependencies) 설정하기  (4) 2008.11.19
    Buildr : 컴파일 옵션 설정하기  (2) 2008.11.18
    Posted by 에코지오
    ,
    Ant는 세밀하게 빌드를 설정하기 좋지만 미리 정해진 규칙이나 프로세스가 없기 때문에 알파부터 오메가까지 모든걸 내가 다 정의해야한다. Ant는 나에게 거의 모든 자유를 주며, 복잡한 빌드환경에서도 적절히 대처할 수 있는 온갖 task를 제공해준다. 그러나 자유에는 책임이 따르듯이 규칙과 절차를 만드는 몫은 나에게 있다. 이렇게 수립된 규칙과 절차는 프로젝트마다 제각각이어서 대부분 호환되지 않는다.

    Maven은 Ant에 비해 자유도는 낮지만 규칙을 잘 따른다면 작업을 쉽게 처리할 수 있다. 널리 통용되는 빌드 프로세스(phase)와 수행할 작업이 내장되어 있어서 설정을 최소화해주며, 의존관계의 라이브러리를 체계적으로 다루는 능력이 뛰어나다.(원래 ant는 의존성 관리를 하지 않으나 ivy를 이용하면 ant에서도 충분히 maven처럼 의존성 관리를 할 수 있다고 함) 그러나 Ant만큼 풍부하고 작은 단위의 task는 제공하지 않고 대신, plugin이라는 다소 큰 덩어리의 도구를 제공한다. 딱딱한 빌드절차, 묵직한 plugin 등 커스터마이징이 극히 제한적이어서 국내 SI프로젝트처럼 국제적인 상식(?)을 잘 따르지 않는 복잡한 빌드환경에서는 적합하지 않을 수 있다.

    그럼 Ant의 높은 자유도와 Maven의 다듬어진 프로세스를 두루 갖는 그런 빌드 도구는 없는가?
    최근 Ant와 Maven을 뛰어넘기 위해 스크립트 언어 기반으로 빌드를 설정하려는 시도가 많아지고 있다.

    찾아보면 BuildrGradle, Raven 대충 이정도 나온다.

    Raven과 Buildr은 ruby 언어로, Gradle은 groovy 언어로 빌드를 작성한다.
    Raven은 Ant 스타일과 비슷하고 Buildr과 Gradle은 Maven의 사상이 들어있다.

    자 이쯤에서 하나 골라잡아 공부해야 하는건가?
    Posted by 에코지오
    ,