Ant脚本文件build文件编写示例

13年前

build.xml文件编写示例

1、设置目录常量
  例:<property name="work.home" value="work" />
2、设置classpath
  例:
  <!-- 设置运行环境 -->
  <path id="classpath">
    <pathelement location="${servlet.jar}"/>
    <pathelement location="${webapp.libs}"/>
    <fileset dir="${webapp.libs}">
      <include name="**/*.jar"/>
      <exclude name="servlet.jar"/>
    </fileset>
    <pathelement path="${compile.classpath}"/>
  </path>
3、设置初始化内容
  例:
  <target name="init">
    <!-- Do we need to copy dependent libraries? -->
    <available property="copy.libs"  file="${webapp.libs}" />
  </target>
4、创建需要的文件目录
  例:
  <target name="prepare" depends="init"
        description="Prepare target directory">
    <mkdir dir="${webapp.target}" />
  </target>
5、设置运行类库lib,将需要的运行类库拷贝到指定目录
  例:
  <target name="libs" depends="prepare" if="copy.libs"
        description="Copy dependent libraries">
    <mkdir   dir="${webapp.target}/WEB-INF/lib" />
    <copy  todir="${webapp.target}/WEB-INF/lib">
      <fileset dir="${webapp.libs}">
        <include name="*.jar"/>
        <exclude name="servlet.jar"/>
      </fileset>
    </copy>
  </target>
6、复制运行此系统所需的文件
  例:
  <target name="static" depends="prepare,libs"
        description="Copy static files">
    <copy  todir="${webapp.target}">
      <fileset dir="${webapp.web}">
        <exclude name="**/CVS/*.*"/>
        <exclude name="Tomcat/**"/>
        <exclude name="WEB-INF/lib/**"/>
        <exclude name="WEB-INF/classes/**"/>
        <exclude name="WEB-INF/log/**"/>
      </fileset>
    </copy>
  </target>
7、编译
  例:
  <target name="compile" depends="static" if="webapp.compile"
        description="Compile Java sources">
    <!-- 编译源码 -->
    <javac  srcdir="${webapp.src}"
            destdir="${webapp.target}/WEB-INF/classes"
            debug="${compile.debug}"
            deprecation="${compile.deprecation}"
            optimize="${compile.optimize}">
      <classpath refid="classpath"/>
    </javac>
    <!-- 将编译得到的class文件拷贝到指定地方 -->
    <copy todir="${webapp.target}/WEB-INF/classes">
      <fileset dir="${webapp.src}">
        <include name="**/*.properties" />
        <include name="**/*.xml" />
        <exclude name="**/CVS/*.*"/>
      </fileset>
    </copy>
  </target>
8、测试并生成测试报告
  例:
  <target name="test" depends="compile,compile.test">
    <property name="tests" value="Test*"/>
    <!-- 创建文件夹 -->
    <mkdir dir="${work.home}/test"/>
    <mkdir dir="${work.home}/test/xml"/>
    <mkdir dir="${work.home}/test/report"/>
    <!--   测试   -->
    <junit dir="${work.home}/test" printsummary="yes" haltonerror="yes" fork="yes">
      <formatter type="plain" usefile="false" />
      <formatter type="xml" />
      <batchtest todir="${work.home}/test/xml">
        <fileset dir="${webapp.test}">
          <include name="**/${tests}.java" />
          <exclude name="**/Test*All*.java"/>
        </fileset>
      </batchtest>
      <classpath>
        <pathelement location="${webapp.target}/WEB-INF/classes" />
        <pathelement location="${work.home}/${webapp.test}"/>
      </classpath>
      <classpath refid="classpath" />
    </junit>
    <!--   生成测试报告   -->
    <junitreport todir="${work.home}/test/xml">
      <fileset dir="${work.home}/test/xml">
        <include name="TEST-*.xml"/>
      </fileset>
      <report format="frames" todir="${work.home}/test/report"/>
    </junitreport>
    <fail if="test.failed"/>
  </target>
9、生成发布程序文件
  例:
  <target name="dist" depends="compile" description="Create web application archive">
    <!-- 将web工程以war形式发布 --?
    <jar basedir="${work.home}/${webapp.name}"
    destfile="${webapp.dist}/${webapp.war}"/>
  </target>
  说明:basedir为要发布的程序文件位置
        destfile为发布程序文件
10、清理旧的测试信息
  例:
  <target name="clean" description="Clean build and distribution directories">
    <delete  dir="${work.home}" />
    <delete  dir="${webapp.dist}" />
  </target>
  说明:可以删除目录也可以删除文件
          delete中为dir时删除指定目录
   delete中为file是删除指定文件

依赖关系基本上从下往上的,也就是说只有上面的成立了,下面的才能正常
运行下去。其中的1、2、3是不需要依赖任何一个的


在build.xml文件中,出现两个或多个同名的属性,在使用该属性时将以第一
个为准。


目录结构规范:
build        类和其他内容的临时存放位置
dist         发布目录
docs         显示格式的文档文件
etc          示例文件
lib          项目依赖库,通常是第三方的.jar文件
src          java源代码的根目录,其下级是包的目录结构
src/xdocs    xml格式的文档,可以通过构建过程转换的显示格式
src/META-INF jar文件的元数据
web          web内容(.html、.jpg、.jsp)的根目录
web/WEB-INF  web配置信息,例如web.xml


build和dist目录不应包含ant无法生成的内容,这样clean可以只通过删除它
们来进行清理。

 

 

转自:http://blog.programfan.com/article.asp?id=18965