JAVA进行式
让我们把JAVA进行到底!
posts - 13,comments - 21,trackbacks - 0

说明:
        下面是我在开发中使用的build.xml文件以及其附属文件,为使层次清晰我将公用任务放入common.xml中,另外还有一些配置文件,这个build.xml会在今后开发中不断完善。

        1、build.xml文件
<?xml version="1.0" encoding="gb2312"?>
<project name="message" basedir="." default="help">
  <property name="webapp.name" value="message"/>
  <import file="../message/common.xml"/>

  <!-- "test" 任务用于执行junit的单元测试 -->
  <target name="test" description="Run junit test.">
    <mkdir dir="${build.dir}/test"/>
    <mkdir dir="${build.dir}/report"/>
  
    <junit printsummary="yes">
      <formatter type="xml"/>
   <test name="cn.hxex.message.test.AllTests"/>
   <classpath refid="classpath"/>
    </junit>
   
    <junitreport todir="${build.dir}/test">
      <fileset dir=".">
     <include name="TEST-*.xml"/>
   </fileset>
   <report format="noframes" todir="${build.dir}/report"/>
    </junitreport>
  </target>
 
  <!-- "run"任务用于运行指定程序 --> 
  <target name="run">
    <java classname="cn.hxex.basic.exercise.LifeCycle">
      <classpath refid="classpath"/>
    </java>
  </target>
</project>

        2、common.xml文件
<?xml version="1.0" encoding="gb2312"?>
<project>
  <!-- 直接设置项目文件夹变量,也可以写一个配置文件,用file属性引入 -->
  <!--
  <property file="webapp.properties"/>
   -->
  <property name="src.dir" value="src"/>
  <property name="build.dir" value="WebRoot"/>
  <property name="webpage.dir" value="${build.dir}/webpage"/>
  <property name="webinf.dir" value="${build.dir}/webinf"/>
  <property name="lib.dir" value="${webinf.dir}/lib"/>
  <property name="classes.dir" value="${webinf.dir}/classes"/>
 
  <!-- 取得环境变量TOMCAT_HOME的值 -->
  <property environment="env"/>
  <property name="tomcat.home" value="${env.TOMCAT_HOME}"/>
  <property name="ant.home" value="${env.ANT_HOME}"/>
 
  <!-- 引入有关tomcat的变量配置文件,也可以象上面一样,直接在这里设置 -->
  <property file="tomcat.properties"/>
  <!--
  <property name="tomcat.server" value="localhost"/>
  <property name="tomcat.manager.url" value="http://${tomcat.server}/manager"/>
  <property name="tomcat.username" value="admin"/>
  <property name="tomcat.password" value=""/>
   -->
 
  <!-- 设置任务要执行的类操作 -->
  <!-- 可引入设置tomcat任务的配置文件 -->
  <taskdef file="tomcatTasks.properties">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <!-- 也可逐一设置任务
  <taskdef name="deployc" classname="org.apache.catalina.ant.DeployTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="install" classname="org.apache.catalina.ant.InstallTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="list" classname="org.apache.catalina.ant.ListTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="remove" classname="org.apache.catalina.ant.RemoveTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="resources" classname="org.apache.catalina.ant.ResourcesTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="roles" classname="org.apache.catalina.ant.RolesTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="start" classname="org.apache.catalina.ant.StartTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
 
  <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask">
    <classpath>
      <pathelement path="${tomcat.home}/server/lib/catalina-ant.jar"/>
    </classpath>
  </taskdef>
  -->
  <!--
  <taskdef name="junit" classname="org.apache.tools.ant.taskdefs.optional.junit.JUnitTask">
    <classpath>
      <pathelement path="${ant.home}/lib/ant-junit.jar"/>
    </classpath>
  </taskdef>-->
     
  <!-- 设置类路径 -->
  <path id="classpath">
    <fileset dir="${lib.dir}">
      <include name="*.jar"/>
    </fileset>
 
    <fileset dir="${tomcat.home}/common/lib">
      <include name="*.jar"/>
    </fileset>
   
    <fileset dir="${ant.home}/lib">
      <include name="*.jar"/>
    </fileset>
   
    <pathelement path="${classes.dir}"/>
  </path>

  <!-- "help"任务用于显示帮助信息 -->
  <target name="help">
    <echo message=""/>
    <echo message="${webapp.name} build file"/>
    <echo message="----------------------------------------------------------"/>
    <echo message=""/>
    <echo message="Available targets are:"/>
    <echo message=""/>
    <echo message="help    --> Print this screen."/>
    <echo message="start   --> Start Tomcat application."/>
    <echo message="stop    --> Stop Tomcat application."/>
    <echo message="list    --> List Tomcat applications."/>
    <echo message="compile --> Compile all Java files."/>
    <echo message="deploy  --> Deploy the project to the tomcat."/>
    <echo message="clean   --> Clean output directories."/>
    <echo message="new     --> Creates a new project with the specified name."/>
    <echo message="test    --> Run junit test."/>
  </target>

  <!-- "start"任务用于启动Tomcat -->
  <target name="start" description="Start Tomcat application">
    <start url="${tomcat.manager.url}"
           username="${tomcat.username}"
           password="${tomcat.password}"
           path="/${webapp.name}"/>
  </target>

  <!-- "stop"任务用于停止Tomcat -->
  <target name="stop" description="Stop Tomcat application">
    <stop url="${tomcat.manager.url}"
          username="${tomcat.username}"
          password="${tomcat.password}"
          path="/${webapp.name}"/>
  </target>

  <!-- "list"任务用于显示Tomcat中应用名 -->
  <target name="list" description="List Tomcat applications">
    <list url="${tomcat.manager.url}"
          username="${tomcat.username}"
          password="${tomcat.password}"/>
  </target> 
 
  <!-- "clean"任务用于清除所有文件及文件夹 -->
  <target name="clean" description="Clean output directories">
    <delete dir="${classes.dir}"/>
    <delete dir="${tomcat.home}/webapps/${webapp.name}"/>
  </target>

  <!-- "compile"任务用于编译源代码和复制Hibernate配置文件到指定类文件夹 -->
  <target name="compile" description="compile the java source and copy the configuration file">
    <mkdir dir="${classes.dir}"/>
    <javac destdir="${classes.dir}"
           target="1.5" source="1.5" debug="true"
           deprecation="true" optimize="false" failonerror="true">
      <src path="${src.dir}"/>
      <classpath refid="classpath"/>
    </javac>
 
    <!-- 复制hibernate映射文件到项目下的classes文件中 -->
    <copy todir="${classes.dir}">
      <fileset dir="${src.dir}" includes="**/*.xml"/>
      <fileset dir="${src.dir}" includes="**/*.properties"/>
    </copy>
 
    <!-- 复制配置文件到项目下的classes文件中 -->
    <copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF/classes">
      <fileset dir="${classes.dir}" includes="**/*.xml"/>
      <fileset dir="${classes.dir}" includes="**/*.properties">
        <exclude name="**/*_zh.properties"/>
      </fileset> 
    </copy>
 
    <!-- 对中文配置文件进行编码编译 -->
    <native2ascii dest="${tomcat.home}/webapps/${webapp.name}/WEB-INF/classes"
                  encoding="UTF-8"
                  src="${classes.dir}" includes="**/*_zh.properties"/>
  </target>

  <!-- "deploy"任务用于部署项目和复制配置文件到tomcat容器 -->
  <target name="deploy" depends="compile" description="deploy the project to the tomcat!">
    <!-- 复制web网页文件 -->
    <copy todir="${tomcat.home}/webapps/${webapp.name}" preservelastmodified="true">
      <fileset dir="${webpage.dir}">
        <include name="**/*.*"/>
        <exclude name="**/readme.txt"/>
      </fileset>
    </copy>
 
    <!-- 复制类文件 -->
    <copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF/classes"
          preservelastmodified="true">
      <fileset dir="${classes.dir}"/>
    </copy>
   
    <!-- 复制库文件 -->
    <copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF/lib"
          preservelastmodified="true">
      <fileset dir="${lib.dir}">
        <exclude name="**/readme.txt"/>
      </fileset>
    </copy>
 
    <!-- 复制配置文件 -->
    <copy todir="${tomcat.home}/webapps/${webapp.name}/WEB-INF"
          preservelastmodified="true">
      <fileset dir="${webinf.dir}">
        <exclude name="**/readme.txt"/>
      </fileset>
      <fileset dir="${build.dir}">
        <include name="*.xml"/>
        <include name="*..properties"/>
      </fileset>
    </copy>
  </target>

  <!-- "new"任务用于创建一个新的项目,复制原项目,并改为新项目名 -->
  <target name="new" description="creates a new project with the specified name">
    <echo level="info">
      +----------------------------------------------------------+
      |    -- Welcome to the AppDemo New Application Wizard! --  |
      |                                                          |
      | To create a new application, please answer the following |
      | questions.                                               |
      +----------------------------------------------------------+
    </echo>
    <echo/>
 
    <!-- 提示用户输入新建的项目名 -->
    <input message="What would you like to name your application [myapp]?"
           addproperty="app.name" defaultvalue="myapp"/>
 
    <echo level="info">
      Creating new application named '${app.name}'...
    </echo>
    <copy todir="../${app.name}">
      <fileset dir="${basedir}">
        <exclude name="${lib.dir}/*.txt"/>
      </fileset>
    </copy>
 
    <!-- 替换应用名 -->
    <replaceregexp flags="g">
      <regexp pattern="message"/>
      <substitution expression="${app.name}"/>
      <fileset dir="../${app.name}">
        <include name="**"/>
        <exclude name="**/*.jar"/>
      </fileset>
    </replaceregexp>
  </target>
</project>

        3、webapp.properties文件
src.dir=src
build.dir=WebRoot
webpage.dir=${build.dir}/webpage
webinf.dir=${build.dir}/webinf
lib.dir=${webinf.dir}/lib
classes.dir=${webinf.dir}/classes

        4、tomcatTasks.properties文件
deploy=org.apache.catalina.ant.DeployTask
install=org.apache.catalina.ant.InstallTask
list=org.apache.catalina.ant.ListTask
reload=org.apache.catalina.ant.ReloadTask
remove=org.apache.catalina.ant.RemoveTask
resources=org.apache.catalina.ant.ResourcesTask
roles=org.apache.catalina.ant.RolesTask
start=org.apache.catalina.ant.StartTask
stop=org.apache.catalina.ant.StopTask
undeploy=org.apache.catalina.ant.UndeployTask

        5、tomcat.properties文件
tomcat.server=localhost
tomcat.manager.url=http://${tomcat.server}/manager
tomcat.username=admin
tomcat.password=

posted on 2006-08-05 10:18 水秀清灵 阅读(837) 评论(1)  编辑  收藏 所属分类: 学习笔记

FeedBack:
# re: 在开发中使用ant
2006-08-10 06:45 | 枫中玎玲
小巫不错哈,ant都看的差不多了,呵呵
过段时间我也要学ant老,到时少不了问你问题哦
^_^  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: