ant項目構建

Installing Ant
1. choose a directory and copy the distribution files there, and the directory will be named as "ANT_HOME"
2. set the environment
windows:    set ANT_HOME=C:/apache-ant-xx
                set JAVA_HOME=C:/jdk
                set PATH=xxxx;%ANT_HOME%/bin

Using Ant
1. write a class
E:/anttest/src/oata/HelloWorld.java
package oata;
public class HelloWorld{
    public static void main(String[] args){
        System.out.println("hello World!");
    }
}
2. write build.xml
<project name="MyProject" default="dist" basedir=".">
    <description>simple example build file</description>
  <!-- set global properties for this build -->
  <property name="src" location="src"/>
  <property name="build" location="build"/>
  <property name="dist"  location="dist"/>

  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
  </target>

  <target name="compile" depends="init" description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <target name="dist" depends="compile" description="generate the distribution" >
    <!-- Create the distribution directory -->
    <mkdir dir="${dist}/lib"/>
    <!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
    <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
  </target>

  <target name="clean" description="clean up" >
    <!-- Delete the ${build} and ${dist} directory trees -->
    <delete dir="${build}"/>
    <delete dir="${dist}"/>
  </target>
</project>
3. execute:
E:/anttest/ant

Running Ant
ant
* runs the default build.xml
ant -buildfile test.xml
* runs the test.xml
ant -buildfile test.xml dist
* runs using the test.xml in the current directory, on the target called dist
ant -buildfile test.xml -Dbuild=build/classes dist
* runs using the test.xml file the current directory, on the target called dist, setting the build propety to the value build/classes
ant -lib /home/ant/extras
* runs picking up additional task and support jars from the /home/ant/extras location

war ROOT and publish to tomcat webapps
ROOT.xml
<?xml   version="1.0"?> 
<project   name="webapp"   default="clean"   basedir="."> 
    <property name="src" location="src"/>
    <property name="build" location="build"/>
    <property name="dist"  location="dist"/>

    <property   name="mywebs.dir"   value="${build}/myweb"   /> 
    <property   name="web-inf.dir"   value="${mywebs.dir}/WEB-INF" /> 

    <property name="tomcat.home" value="F:/apache-tomcat-5.5.20"/> 
   
    <target name="init">
        <tstamp/>
        <mkdir dir="${build}"/>
        <mkdir dir="${dist}"/>
        <mkdir dir="${mywebs.dir}"/>
        <mkdir dir="${web-inf.dir}"/>
        <mkdir dir="${web-inf.dir}/lib"/>
        <mkdir dir="${web-inf.dir}/classes"/>

        <copy todir="${mywebs.dir}">
           <fileset dir="${src}/jsps" />
        </copy>
        <copy todir="${web-inf.dir}/lib">
           <fileset dir="${src}/libs" />
        </copy>

    </target>

    <target name="compile" depends="init">
        <mkdir dir="build/classes"/>
        <javac srcdir="${src}/java" destdir="build/classes">
            <classpath>
                <fileset dir="${src}/libs">
                    <include name="*.jar"/>
                </fileset>
            </classpath>
        </javac>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="build/jar"/>
        <jar destfile="${web-inf.dir}/lib/MyDemos.jar" basedir="build/classes">

        </jar>
    </target>

    <target name="war" depends="jar"> 
        <copy todir="${web-inf.dir}/classes">
            <fileset dir="${src}/properties" >
                <include name="*.properties"/>
            </fileset>
        </copy>
         <war  destfile="${dist}/ROOT.war"   webxml="${web-inf.dir}/web.xml"   basedir="${mywebs.dir}"/> 
    </target> 

    <target name="publish" depends="war">
        <copy todir="${tomcat.home}/webapps">
            <fileset dir="${dist}" >
                <include name="*.war"/>
            </fileset>
        </copy>
    </target>

    <target name="clean" depends="publish">
        <delete dir="build"/>
        <delete dir="dist"/>
    </target>
</project>
** src 的文件目錄:
src/java:            *.java files
src/jsps:             *.jsp files
src/libs:             *.jar files
src/properties     *.properties files

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章