ant的build.xml文件分析

build.xml

1. ant中变量的传递有两种方法:

a) setenv,然后在build.xml中获取系统环境变量,e.g. setenv ANT_HOME xxxx

b) ant -D变量名=变量值 target_name,则在build.xml中可以直接使用该变量, e.g. ant -Dlsf.version=6.2 all (build.xml见下)

 ########################################<?xml version="1.0" encoding="UTF-8"?>

<!-- 定义default target,以及当前目录 -->

<project name="xxx" default="all" basedir=".">

<!-- 定义一些变量,直接指定其值 -->

<property name="perf.version" value="1.2"></property>

<!-- 定义一些变量,其值从环境变量获取 -->

<property environment="env"></property>

<property name="bname" value="${env.ANT_HOME}"></property>

<!-- 定义一些变量,其值是基于其他的变量值 -->

<property name="perf.home" value="${basedir}/../.."></property>

<property name="bname" value="${env.BNAME}"></property>

<property name="ant.home" value="${env.ANT_HOME}"></property>

<!-- 通过 Condition 定义一些变量,其值根据不同的条件设定不同的值 -->

<condition property="cmd.ant" value="${ant.home}\bin\ant.bat">

<!-- ant 定义并自动获取的os type -->

  <os family="windows"></os>

</condition>

<condition property="cmd.ant" value="${ant.home}/bin/ant">

     <not>

           <os family="windows"></os>

     </not>

</condition>

<condition property="node.lsflib.dir" value="linux-x86">

         <equals arg1="${bname}" arg2="linux-x86"></equals>

</condition>

<condition property="node.lsflib.dir" value="linux-x86_64">

       <equals arg1="${bname}" arg2="linux-x86_64"></equals>

</condition>

......

<condition property="build" value="true">

      <or>

<equals arg1="${bname}" arg2="linux-x86"></equals>

<equals arg1="${bname}" arg2="linux-x86_64"></equals> ...... </or>

</condition>

<!-- 这里lsf.version 就是通过 ant -Dlsf.version=xxx 传进来的-->

<condition property="lsf.shared.dir" value="LSF6.2">

       <contains string="${lsf.version}" substring="6.2">

       </contains>

</condition>

<condition property="lsf.shared.dir" value="LSF7_0_4_Gold_Patch">

          <contains string="${lsf.version}" substring="7.0"></contains>

</condition> 

 ......

<!-- Get build date -->

 <tstamp>

     <format property="build.date" pattern="EEE MMM dd HH:mm:ss yyyy">

      </format>

</tstamp>

<path id="compile.classpath">

       <pathelement location="${classes.dir}"></pathelement>

       <fileset dir="${perf.home}/xxx/lib">

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

       </fileset>

  </path>

 <!--可以用 if 或者 unless 设定在什么条件下执行该target-->

 <target name="all" if="build">

          <antcall target="xxx"></antcall>

 <antcall target="xxx"></antcall>

 ......

</target>

<target name="xxx">

 <!--到某个目录下执行ant命令或其他-->

       <exec dir="${perf.home}/xxx" executable="${cmd.ant}" failonerror="true">

<arg line="clean"> </arg>

<arg line="build"></arg>

</exec>

<!--到某个目录下执行带参数的gmake命令 "gmake all variable_name=variable_value"--><exec dir="${basedir}/node/c" executable="gmake" failonerror="true"><arg line="clean"></arg><arg line="all LSF_SHARED_DIR=${lsf.shared.dir}"></arg></exec><mkdir dir="${build.home}/classes"></mkdir><!-- Compile java,指定-classpath, 这里compile.class是通过<path id="compile.classpath">...</path>定义的 --><javac srcdir="${basedir}/xxx/java" destdir="${build.home}/classes" debug="${DEBUG}" deprecation="${compile.deprecation}" optimize="${compile.optimize}"><classpath refid="compile.classpath"></classpath></javac><!-- Copy files--><copy todir="${build.home}/classes" filtering="on"><fileset dir="${basedir}/xxx/java" includes="**/*.properties"></fileset><fileset dir="${basedir}/xxx/c" excludes="**/*.txt"></fileset></copy><!-- jar --><jar jarfile="${build.home}/xxx/xxx.jar"><!-- 指定要打包的文件目录 --><fileset dir="${build.home}/classes" includes="**/*"></fileset><!--定义写入manifest的内容--><manifest><section name="${sp.name}.jar"><attribute name="Created-By" value="xxxx"></attribute><attribute name="Version" value="${VERSION}"></attribute><attribute name="Build-Date" value="${build.date}"></attribute> ...... </section></manifest></jar><!-- 删除目录 --><delete dir="${build.home}/classes"></delete><!--打tar包--><tar destfile="${dist.home}/"><tarfileset dir="${basedir}/DBschema/xxx" includes="**/*.sql" prefix="${schema.dir.name}"></tarfileset></tar> ...... </target></project>

猜你喜欢

转载自gsg2009.iteye.com/blog/599579