ant入门

ant是一个基于java的自动化脚本引擎,脚本的格式为xml.

ant脚本的编译顺序:

Ant tries to execute the targets in the depends attribute in the order they appear (from left to right). Keep in mind that it is possible that a target can get executed earlier when an earlier target depends on it:

<target name="A"/>
<target name="B" depends="A"/>
<target name="C" depends="B"/>
<target name="D" depends="C,B,A"/>

Suppose we want to execute target D. From its depends attribute, you might think that first target C, then B and then A is executed. Wrong! C depends on B, and B depends on A, so first A is executed, then B, then C, and finally D.

 

Properties

A project can have a set of properties. These might be set in the buildfile by the property task, or might be set outside Ant. A property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes. This is done by placing the property name between "${" and "}" in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes. This is resolved at run-time as build/classes.

In the event you should need to include this construct literally (i.e. without property substitutions), simply "escape" the '$' character by doubling it. To continue the previous example:

  <echo>$${builddir}=${builddir}</echo>
will echo this message:
  ${builddir}=build/classes

 

In order to maintain backward compatibility with older Ant releases, a single '$' character encountered apart from a property-like construct (including a matched pair of french braces) will be interpreted literally; that is, as '$'. The "correct" way to specify this literal character, however, is by using the escaping mechanism unconditionally, so that "$$" is obtained by specifying "$$$$". Mixing the two approaches yields unpredictable results, as "$$$" results in "$$".

Built-in Properties

Ant provides access to all system properties as if they had been defined using a <property> task. For example, ${os.name} expands to the name of the operating system.

For a list of system properties see the Javadoc of System.getProperties.

In addition, Ant has some built-in properties:

basedir             the absolute path of the project's basedir (as set
                    with the basedir attribute of <project>).
ant.file            the absolute path of the buildfile.
ant.version         the version of Ant
ant.project.name    the name of the project that is currently executing;
                    it is set in the name attribute of <project>.
ant.java.version    the JVM version Ant detected; currently it can hold
                    the values "1.2", "1.3", "1.4" and "1.5".

There is also another property, but this is set by the launcher script and therefore maybe not set inside IDEs:

ant.home            home directory of Ant

示例:

<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>

 

   =======================================================================

 

 

<project basedir="." default="war" name="ucdcenter">  <property name="dist.war" value="../dist/ucdcenter.war" />  <property name="dist.webroot" value="../dist/WebRoot" />  <property name="dist.webinf.dir" value="${dist.webroot}/WEB-INF" />

 <property name="lib.dir" value="../lib"/>  <property name="web.class.dir" value="WebRoot/WEB-INF/classes" />  <property name="j2eelib.dir" value="../j2ee1.4lib" />

 <property environment="env" />     <property name="business.location" value="../business"/>  <property name="debuglevel" value="source,lines,vars" />  <property name="target" value="1.5" />  <property name="source" value="1.5" />  <property name="compiler.args" value="-encoding UTF-8" />

 <path id="j2ee.libraryclasspath">   <fileset dir="${j2eelib.dir}" includes="**/*.jar" />  </path>  <path id="ucdcenter.classpath">   <pathelement location="${web.class.dir}" />   <path refid="j2ee.libraryclasspath" />   <fileset dir="${lib.dir}" includes="**/*.jar" />  </path>

 <target name="init">   <delete dir="../dist" />   <mkdir dir="../dist" />   <copy includeemptydirs="false" todir="${dist.webroot}">    <fileset dir="WebRoot" excludes="**/*.launch, **/*.java" />   </copy>   <copy includeemptydirs="false" todir="${dist.webinf.dir}/classes">    <fileset dir="src" excludes="**/*.launch, **/*.java" />    <fileset dir="config" excludes="**/*.launch, **/*.java" />   </copy>      <copy includeemptydirs="false" todir="${dist.webinf.dir}/lib/">       <fileset dir="${lib.dir}" includes="*.jar"/>       <fileset dir="${lib.dir}/j2ee" includes="**/*.jar"/>       <fileset dir="${lib.dir}/xfire" includes="**/*.jar"/>       <fileset dir="${lib.dir}/pdf" includes="**/*.jar"/>       <fileset dir="${lib.dir}/activemq" includes="**/*.jar"/>       <fileset dir="${lib.dir}/springmodules" includes="**/*.jar"/>   </copy>      </target>

 <target name="build-subprojects">      <ant antfile="${business.location}/build.xml" inheritAll="false" target="build-project"/>  </target>    <target depends="init,build-subprojects" name="build-project">   <echo message="${ant.project.name}: ${ant.file}" />   <javac debug="true" debuglevel="${debuglevel}" destdir="${dist.webinf.dir}/classes" source="${source}" target="${target}">    <compilerarg line="${compiler.args}" />    <src path="." />    <classpath refid="ucdcenter.classpath" />   </javac>  </target>

 <target name="war" depends="build-project" description="war ucdcenter">   <tstamp>    <format property="datestamp" pattern="yyyy-MM-dd" />   </tstamp>   <war destfile="${dist.war}" webxml="${dist.webinf.dir}/web.xml" basedir="${dist.webroot}" excludes="**/web.xml">   </war>  </target> </project> 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值