반응형
src 디렉터리에 package 가 aaa.bbb.ccc의 형태로 되어 있을 때....

       <target name="compile">

             <mkdir dir="${build}"/>

             <javac destdir="${build}">

                    <src path="${src}"/>

                    <include name="**/*.java"/>

             </javac>

             <copy todir="${build}/aaa/bbb/ccc">

                    <fileset dir="${src}/aaa/bbb/ccc">

                           <include name="*.xml"/>

                           <include name="*.jsp"/>

                    </fileset>

             </copy>

       </target>

컴파일(javac)의 경우에는 destdir에 하위 폴더를 지정해주지 않아도 자동 생성된다.
그러나 copy의 경우에는 하위폴더를 지정해주지 않는다면
(만약 copy todir="${build}" 라고만 한다면 build 디렉터리에 카피될 것이다.

반응형
<< 참고사이트 >>
Apache Ant 1.7.0 Manual : http://ant.apache.org/manual/

Ant 기본구조 및 태스크는 아래 블로그를 참조하자.
잘 정리되어 있는 듯...

Ant 자바 빌드 툴 I : 기본 구조 : http://blog.naver.com/thdusin/100005914122
Ant 자바 빌드 툴 II : 태스크 : http://blog.naver.com/thdusin/100005926620

더 자세한 task를 알고 싶다면 apache 사이트를 참조하자.
Overview of Ant Tasks : http://ant.apache.org/manual/tasksoverview.html


build.xml 은 하나의 project 요소를 가진다.
project 요소는 target 요소를 포함하고, 각 target은 여러 task를 포함한다.
간단한 구조는 다음과 같다.

<project name="project_name" default="default" basedir=".">
      
<target name="compile" description="compile">

       <!--compile이라는 target name 대한 설명(description)-->

             ...

       </target>

      

       <target name="dist" depends="compile">

       <!-- dist target compile 의존.
       compile target 실행 후에 dist target이 실행된다. -->

             ...

       </target>

 

       <target name="doc">

             ...

       </target>

</project>  



※ 예제 :

사용자 삽입 이미지
test1과 test2 패키지를 test1_2.jar 파일로 묶고,
test2와 test3 패키지를 묶어 test2_3.jar 파일로 묶는다.
javadoc 을 만들고,
lib폴더와 jar 파일들을 zip으로 묶는다.














<project name="ant" default="test1_2" basedir=".">

      

       <tstamp/>

       <!-- property -->

       <property name="src" location="src"/>

       <property name="build" location="build"/>

       <property name="lib" location="lib"/>

       <property name="dist" location="dist"/>

       <property name="doc" location="doc"/>

            

       <!-- compile -->

       <target name="compile" description="compile">

             <mkdir dir="${build}"/>

             <javac srcdir="${src}" destdir="${build}"

                classpath="${lib}/log4j-1.2.8.jar"/>

       </target>

      

       <!-- test1 test2 jar 묶는다 -->

       <target name="test1_2" depends="compile"

             description="test1 and test2 packaging">

             <mkdir dir="${dist}"/>

             <jar jarfile="${dist}/test1_2.${DSTAMP}.jar">

                    <fileset dir="${build}">

                           <exclude name="test3/*.*"/>

                    </fileset>

             </jar>

       </target>

      

       <!-- test2 test3 jar 묶는다 -->

       <target name="test2_3" depends="compile"

             description="test2 and test3 packaging">

             <mkdir dir="${dist}"/>

             <jar jarfile="${dist}/test2_3.${DSTAMP}.jar">

                    <fileset dir="${build}">

                           <exclude name="test1/*.*"/>

                    </fileset>

             </jar>

       </target>

      

       <!-- doc -->

       <target name="doc" depends="test1_2, test2_3">

             <mkdir dir="${doc}"/>

             <javadoc destdir="${doc}">

                    <fileset dir="${src}"/>

             </javadoc>

       </target>

      

       <!-- zip -->

       <target name="zip" depends="test1_2, test2_3">

             <copy todir="${dist}/lib">

                    <fileset dir="${lib}"/>

             </copy>

             <zip destfile="test_${DSTAMP}.zip">

                    <fileset dir="${dist}"/>

             </zip>

       </target>

      

       <!-- delete -->

       <target name="delete" depends="doc, zip">

             <delete dir="${build}"/>

       </target>

      

</project>


 
※ 설명 :
<!-- project -->
그냥 Run As 했을 경우, default 로 설정된 target이 실행된다.
default : Run as 시, 기본적으로 사용할 target name
basedir : base directory. 현재 디렉터리(.)이다.

<!-- property -->
property : 속성을 지정한다. 대소문자 구별.
<property name="src" location="src" />
src 라는 이름의 속성으로 경로(./src)를 지정한다.
다른 부분에서 이 속성을 참조하려면 "${src}" 라고 쓴다.
<javac srcdir="${src}" destdir="${build}" />
( value 와 location )
value : the value of the property.
location : Sets the property to the absolute filename of the given file. If the value of this attribute is an absolute path, it is left unchanged (with / and \ characters converted to the current platforms conventions). Otherwise it is taken as a path relative to the project's basedir and expanded.


<!-- compile -->
<mkdir dir="${build}"/>
build 라는 디렉터리를 만든다.
<javac srcdir="${src}" destdir="${build}" classpath="${lib}/log4j-1.2.8.jar"/>
src 디렉터리(하위 디렉터리 포함)의 java 파일을 컴파일해 build 디렉터리에 저장하고,
클래스패스에는 log4j-1.2.8.jar가 포함된다.

<!-- test1 test2 jar 묶는다 -->
<jar jarfile="${dist}/test1_2.${DSTAMP}.jar">
    <fileset dir="${build}">
         <exclude name="test3/*.*"/>
    </fileset>
</jar>

property 윗부분에 <tstamp/> 가 있다.
이는 DSTAMP, TSTAMP, TODAY 프로퍼티를 설정하는 것이다.
· DSTAMP : yyyyMMdd
· TSTAMP : hhmm
· TODAY  : MMM dd yyy
build 디렉터리에서 test3 패키지의 파일들을 제외(exclude)한 파일들을 jar로 묶어
dist 에 test1_2.날짜.jar 형태로 저장한다.
(ex. test1_2.20070802.jar)
fileset dir 하의 include, exclude는 여러 개 가능하다.

<!-- doc -->
<javadoc destdir="${doc}">
    <fileset dir="${src}"/>
</javadoc>

src 디렉터리의 파일들을 읽어 javadoc문서를 만들어 doc 디렉터리에 저장한다.

<!-- zip -->
<copy todir="${dist}/lib">
    <fileset dir="${lib}"/>
</copy>

lib 디렉터리를 dist/lib 에 복사한다.
<zip destfile="test_${DSTAMP}.zip">
    <fileset dir="${dist}"/>
</zip>

dist 디렉터리의 파일들을 zip으로 묶는다.
zip 파일이름은 test_날짜.zip  (ex. test_20070802.zip)

<!-- delete -->
<delete dir="${build}"/>
build 디렉터리를 삭제한다.


결과 :
Buildfile: C:\Users\kyh\workspace\ant\ant\build.xml

compile:

    [mkdir] Created dir: C:\Users\kyh\workspace\ant\ant\build

    [javac] Compiling 4 source files to C:\Users\kyh\workspace\ant\ant\build

test1_2:

    [mkdir] Created dir: C:\Users\kyh\workspace\ant\ant\dist

      [jar] Building jar: C:\Users\kyh\workspace\ant\ant\dist\test1_2.20070802.jar

compile:

test1_2:

test2_3:

      [jar] Building jar: C:\Users\kyh\workspace\ant\ant\dist\test2_3.20070802.jar

doc:

    [mkdir] Created dir: C:\Users\kyh\workspace\ant\ant\doc

  [javadoc] Generating Javadoc

  [javadoc] Javadoc execution

  [javadoc] Loading source file C:\Users\kyh\workspace\ant\ant\src\test1\AntTest.java...

  [javadoc] Loading source file C:\Users\kyh\workspace\ant\ant\src\test2\AntTest2.java...

  [javadoc] Loading source file C:\Users\kyh\workspace\ant\ant\src\test2\AntTest2_1.java...

  [javadoc] Loading source file C:\Users\kyh\workspace\ant\ant\src\test3\AntTest3.java...

  [javadoc] Constructing Javadoc information...

  [javadoc] Standard Doclet version 1.6.0_02

  [javadoc] Building tree for all the packages and classes...

  [javadoc] Building index for all the packages and classes...

  [javadoc] Building index for all classes...

zip:

     [copy] Copying 1 file to C:\Users\kyh\workspace\ant\ant\dist\lib

      [zip] Building zip: C:\Users\kyh\workspace\ant\ant\test_20070802.zip

delete:

   [delete] Deleting directory C:\Users\kyh\workspace\ant\ant\build

BUILD SUCCESSFUL

Total time: 2 seconds



사용자 삽입 이미지

반응형

★ Ant 란?

Java 기반의 build 도구로서, file 형식은 XML 이다.
make 와는 달리 플랫폼 독립적인 Java 클래스를 사용하기 때문에 OS에 독립적이다.
이클립스는 Ant 플러그인을 기본으로 내장하므로 따로 설치할 필요 없다.


★ build.xml 생성

사용자 삽입 이미지
← 왼쪽과 같이 파일이 존재한다고 가정한다.
프로젝트(ant)를 선택하고 컨텍스트 메뉴에서 New > File 을 선택한다.


↓ 아래와 같은 창이 뜨면
File Name 에 build.xml 을 입력한다.
자동으로 ant 파일로 인식되어 Package Explorer 에 ant 파일로 표시된다.
(Window > Preference 에서 좌측 Ant 선택, 우측의 Names 에 build.xml 이 있다. 여기에 ,abc.xml 이런식으로 추가입력하면 abc.xml 파일 생성시 ant 파일로 인식된다.)

사용자 삽입 이미지

























사용자 삽입 이미지

← 개미 모양의 build.xml 파일을 볼 수 있을 것이다.




↓ 에디터 창에서 파일을 작성한 후,
Outline View 를 보면 아래와 같이 나타난다.
ant 를 클릭하고 컨텍스트 메뉴에서 Run As를 클릭한다.
Ant Build는 default로 지정된 target이 실행된다.
Ant Build... 를 선택하면 다이얼로그 창이 뜬다.

사용자 삽입 이미지


















사용자 삽입 이미지


↑ Ant Build... 을 선택했을 때의 다이얼로그.
(클릭하면 제대로 된 그림을 볼 수 있을 것이다.)
target name 앞에 체크 박스를 선택하는 순서대로 실행된다.
아랫부분의 Target execution order 란에 선택한 순서대로 표시된다.
순서를 바꾸고 싶으면 체크를 해제한 후 다시 선택하거나,
우측의 Order 버튼을 선택해 위 아래로 위치를 변경하면 된다.

중간에
Sort targets : 체크시, target name이 알파벳 순서대로 정렬된다.
Hide Internal Targets not selected for execution :
Hide Internal Targets 는 description이 없는 타깃을 말한다.
그러므로 체크하면 description이 없는 타깃은 목록에서 사라진다.

+ Recent posts