星期日, 11月 07, 2010

[Java] Hello World with Ant

本篇是一個Ant的基本教學,試用於初學者。
只是把Apache Ant User Manual-HelloWorld with Ant裡的範例重新記錄下來而已。
有樣版以後寫起ant會比較方便:)


 HelloWorld Sample
1.新增一個HelloWord專案

2.新增一個bigd.HelloWorld.java package

Java Resources:src -> New ->Class

3.HelloWorld.java Sample

package bigd;
public class HelloWorld {
     public static void main(String[] args) {
            System.out.println("Bigd Say:Hello World");
        }
}


4.加入build.xml
<project name="HelloWorld" basedir="." default="main">

    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="oata.HelloWorld"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
        <javac srcdir="${src.dir}" destdir="${classes.dir}"/>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>


5.Run As -> Ant.Build

當HelloWorld.java引用外部的jar的情況
1.修改先前的HelloWorld.java Sample
package bigd;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

public class HelloWorld {
    static Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String[] args) {
        BasicConfigurator.configure();
        logger.info("Hello World");          // the old SysO-statement
    }
}

2.將log4j-1.2.16.jar加入專案內的WebContent/WEB-INF/lib

3.修改先前的build.xml,請注意你引用的lib路徑,本例是在WebContent/WEB-INF/lib之下。

<project name="HelloWorld" basedir="." default="main">

專案內用的libs目錄
    <property name="lib.dir"  value="WebContent/WEB-INF/lib"/>
     
        <path id="classpath">
            <fileset dir="${lib.dir}" includes="**/*.jar"/>
        </path>


   
    <property name="src.dir"     value="src"/>

    <property name="build.dir"   value="build"/>
    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir"     value="${build.dir}/jar"/>

    <property name="main-class"  value="bigd.HelloWorld"/>

    <target name="clean">
        <delete dir="${build.dir}"/>
    </target>

     <target name="compile">
            <mkdir dir="${classes.dir}"/>
            <javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
        </target>

        <target name="run" depends="jar">
            <java fork="true" classname="${main-class}">
                <classpath>
                    <path refid="classpath"/>
                    <path location="${jar.dir}/${ant.project.name}.jar"/>
                </classpath>

            </java>
        </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}"/>
            </manifest>
        </jar>
    </target>

    <target name="clean-build" depends="clean,jar"/>

    <target name="main" depends="clean,run"/>

</project>


4.執行build.xml你可以看到以下結果
Buildfile: /home/owen/workspace/HelloWorld/build.xml
clean:
   [delete] Deleting directory /home/owen/workspace/HelloWorld/build
compile:
    [mkdir] Created dir: /home/owen/workspace/HelloWorld/build/classes
    [javac] Compiling 1 source file to /home/owen/workspace/HelloWorld/build/classes
jar:
    [mkdir] Created dir: /home/owen/workspace/HelloWorld/build/jar
      [jar] Building jar: /home/owen/workspace/HelloWorld/build/jar/HelloWorld.jar
run:
     [java] 0 [main] INFO bigd.HelloWorld  - Hello World
main:
BUILD SUCCESSFUL
Total time: 737 milliseconds

沒有留言:

張貼留言

留個話吧:)