51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 7726|回复: 1
打印 上一主题 下一主题

Ant使用

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2018-6-11 17:19:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
Ant是一个用于简单或复杂Java工程的自动化构建、部署工具,它对于那些具有分布式开发团队或者相信通过频繁的构建来进行不间断集成的公司尤其有用。对于那些建立传统全Java应用程序以及那些使用HTML、JSP和Java servlets创建Web应用程序的公司来说,Ant极具价值。无论你的Java开发者使用什么操作系统、集成开发环境或者构建环境,Ant都可以将你的工程集合在一起,用于那些重要的构建。Ant也能够自动化并且同步文档部署,这通常发生在软件开发过程中的没有正式文档和文档比较混乱的部分。

     在构建和部署Java应用程序的时候,Ant处理着大量有用的任务。最基本的任务包括添加和移除目录、使用FTP拷贝和下载文件、创建JAR和ZIP文件以及创建文档。更高级的特性包括用源代码控制系统诸如CVS或者SourceSafe来检查源代码、执行SQL查询或脚本、将XML文件转换为人能识别的HTML,以及为远程方法调用生成stub(存根)文件。

     Ant和Make(非常著名的构建工具,很多C语言开发人员都使用它)之间有什么不同?Ant是为Java而创建,带有属于其自身的、独特的范例,具有可移植性。而Make依赖于固定的操作系统命令(因此一个运行在微软Windows下的Make文件对于使用UNIX的开发者来说毫无用处),利用Ant构建的纯Java工程是可移植的,因为Ant本身就是用Java编写的,并且Ant bulidfiles使用XML语法。

1.下载及安装

Apache Ant是一个基于Java的构建工具,满足跨平台使用。

下载及安装:

http://ant.apache.org/

解压下载的Ant拷贝到任意目录以D:\Program Files\apache-ant-1.8.4为例

将D:\Program Files\apache-ant-1.8.4配置为系统变量ANT_HOME

在PATH中加入%ANT_HOME%\bin

命令提示符中输入ant相关命令可以查看是否配置正确。





2.简单实例

实例:编译运行一个简单的类。

编写一个简单的HelloWorld

public class HelloWorld{
    public static void main(String[] args){
        System.out.println("Hello, ANT!!!");
    }
}
建立build.xml(名称可改)

HelloWorld.java和build.xml所在目录示意:

├src

│ └HelloWorld.java

└build.xml

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project name="antStudyProj" default="run"  basedir=".">
    <property name="classdir" value="build/classes"/>
    <echo message="Learn Ant!!!"/>
    <target name="clean">
        <delete dir="${classdir}"/>
    </target>

    <target name="compile" depends="clean">
        <mkdir dir="${classdir}" />
        <javac srcdir="src" destdir="${classdir}" includeantruntime="false"/>
    </target>
    <target name="run" depends="compile">
        <java classname="HelloWorld">
            <classpath path="${classdir}"/>
        </java>
    </target>
</project>
复制代码
运行Ant,查看结果(可以通过ant -help查看ant命令的使用)

    这里使用ant -buildfile build.xml文件的绝对路径来运行,结果如下:



    注意:可以通过其他方式也可运行ant,ant的入口为org.apache.tools.ant.launch.Launcher,里面包含mian方法,可指定执行该类并传入参数。

3.buildfile介绍

这是官网的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>
复制代码
buildfile使用XML编写。每个buildfile包含一个project元素,而project元素至少有一个target。

project有三个属性name、default、basedir,都不是必须的。

name:项目名称

default:指定默认执行的target

basedir:故名思意就是工作的根目录 .代表当前目录

project中可以定义多个target。Target是要执行的tasks的集合。

target有以下属性:name,depends,if,unless,description,extensionOf,onMissingExtensionPoint。

name:target的名字,必须

depends:此target依赖的targets,用逗号隔开,非必须

if:某个属性存在或表达式的计算结果为true,非必须

unless:和if对应,非必须

description:对target的描述,非必须

task能被执行的一段代码,结构如下

<name attribute1="value1" attribute2="value2" ... />

Property属性可以通过name及value定义属性,或通过file指定载入的属性文件等。

当定义了一个name为example的属性后,可通过${example}进行引用。

<property name="foo.dist" value="dist"/>

将属性foo.dist的值设置为dist

<property file="foo.properties"/>



从foo.properties文件中读取属性

<property url="http://www.mysite.com/bla/props/foo.properties"/>

从url指定的properties中读取所有属性

Classpath有两个属性path和location,path指定jar包,location指定包含jar包的路径。可以通过fileset和dirset简化配置。

复制代码
<classpath>
      <pathelement path="${classpath}"/>
      <fileset dir="lib">
        <include name="**/*.jar"/>
      </fileset>
      <pathelement location="classes"/>
      <dirset dir="${build.dir}">
        <include name="apps/**/classes"/>
        <exclude name="apps/**/*Test*"/>
      </dirset>
      <filelist refid="third-party_jars"/>
</classpath>
复制代码
4.Ant流程控制 if elseif else

    使用循环,判断等控制时需添加ant-contrib包。

    taskdef定义第三方的任务,主要包含ant的处理流程逻辑

    if elseif else格式如下

复制代码
<property name="hello" value="false"/>
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpath="D:\Program Files\apache-ant-1.8.4\lib\ant-contrib-1.0b3.jar" />  
    <target name="run" depends="compile">
        <java classname="HelloWorld">
            <classpath path="${classdir}"/>
        </java>
        <if>
            <equals arg1="${hello}" arg2="true"/>
            <then>
                <echo>${hello} is true</echo>
            </then>
            <elseif>
                <equals arg1="${hello}" arg2="false"/>
                <then>
                    <echo>${hello} is false</echo>
                </then>
            </elseif>
            <else>
                <echo>${hello}</echo>
            </else>
        </if>
   </target>
复制代码
5.常用task

使用classpath

<target>
    <javac>
        <classpath refid="project.class.path"/>
    </javac>
</target>
设置classpath

复制代码
<classpath id="project.class.path">
    <pathelement path="${classpath}"/>
    <fileset dir="lib">
        <include name="**/*.jar"/>
    </fileset>
    <pathelement location="classes"/>
    <dirset dir="build">
        <include name="apps/**/classes"/>
        <exclude name="apps/**/*Test*"/>
    </dirset>
    <filelist refid="third-party_jars"/>
</classpath>
复制代码
输出信息

<echo message="XXX"/>
<echo>XXX</echo>
引入一个XML文件

<import file="../common-targets.xml"/>
拷贝文件

复制代码
<copy file="myfile.txt" tofile="mycopy.txt"/>
<copy file="myfile.txt" todir="../otherdir"/>
<copy todir="..//newdir">
    <fileset dir="src_dir"/>
</copy>
<copy todir="../destdir">
    <fileset dir="src_dir">
        <exclude name="**/*.java"/>
    </fileset>
</copy>
<copy todir="../destdir">
    <fileset dir="src_dir" excludes="**/*.java"/>
</copy>
<!--拷贝一个文件集合到一个目录,同时建立备份文件-->
<copy todir="../backup/dir">
    <fileset dir="src_dir"/>
    <globmapper from="*" to="*bak"/>
</copy>
<!--拷贝一个集合的文件到指定目录,并替换掉TITLE-->
<copy todir="../backup/dir">
    <fileset dir="src_dir"/>
    <filterset>
        <filter token="TITLE" value="Foo Bar"/>
    </filterset>
</copy>
<copydir src="${src}/resources" dest="${dest}" includes="**/*.java" excludes="**/Test.java"/>
<copyfile src="test.java" dest="subdir/test.java"/>
复制代码
删除文件、目录



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
 楼主| 发表于 2018-6-11 17:20:15 | 只看该作者
  1. <p>复制代码</p><p><delete file="/lib/ant.jar"/></p><p><delete dir="/lib"/></p><p><delete></p><p>    <fileset dir="." includes="**/*.bak"/></p><p></delete></p><p><!--删除当前目录下所有的文件和目录,包括当前目录--></p><p><delete includeEmptyDirs="true"></p><p>    <fileset dir="build"/></p><p></delete></p><p><!--删除当前目录下所有的文件和目录,不包括当前目录--></p><p><delete includeEmptyDirs="true"></p><p>    <fileset dir="build" includes="**/*"/></p><p></delete></p><p><!--删除当前目录下所有的svn相关文件(因为SVN文件默认是excludes的,所以这里说明一下)--></p><p><delete defaultExcludes="false"></p><p>    <fileset dir="${src}" includes="**/*.svn"/></p><p></delete></p><p><!--删除文件目录树--></p><p><deltree dir="dist"/></p><p>复制代码</p><p>剪切文件</p><p>
  2. </p><p><move todir="some/new/dir"></p><p>    <fileset dir="my/src/dir"></p><p>        <include name="**/*.jar"/></p><p>        <exclude name="**/ant.jar"/></p><p>    </fileset></p><p></move></p><p>重命名</p><p>
  3. </p><p><rename src="foo.jar" dest="ant-${version}.jar"/></p><p>建立临时文件</p><p>
  4. </p><p><!--在目录build下,建立文件名为temp.file,后缀名为xml的文件--></p><p><tempfile property="temp.file" destDir="build" suffix=".xml"/></p><p>Touch使用</p><p>
  5. </p><p>复制代码</p><p><!--如果文件不存在,创建文件,如果存在,更改最后访问时间为当前系统时间--></p><p><touch file="myfile"/></p><p><!--如果文件不存在,创建文件,更改最后访问时间--></p><p><touch file="myfile" datetime="06/28/2000 2:02 pm"/></p><p><!--更改目录下所有文件的最后访问时间--></p><p><touch datetime="09/10/1974 4:30 pm"></p><p>    <fileset dir="src_dir"/></p><p></touch></p><p>复制代码</p><p>Condition的使用 <and> <or> <not>等tag</p><p>
  6. </p><p>peoperty: The name of the property to set</p><p>
  7. </p><p>value: The value to set the property to.</p><p>
  8. </p><p>else: The value to set the property to if the codition evaluates to false.</p><p>
  9. </p><p>复制代码</p><p><condition property="isMacOsButNotMacOsX"></p><p>    <and></p><p>        <os family="mac"></p><p>        <not></p><p>            <os family="unix"></p><p>        </not></p><p>    </and></p><p></condition></p><p>复制代码</p><p>替换</p><p>
  10. </p><p>复制代码</p><p><replace </p><p>    file="configure.sh"</p><p>    value="defaultvalue"</p><p>    propertyFile="src/name.properties"></p><p>  <replacefilter </p><p>    token="@token1@"/></p><p>  <replacefilter </p><p>    token="@token2@" </p><p>    value="value2"/></p><p>  <replacefilter </p><p>    token="@token3@" </p><p>    property="property.key"/></p><p>  <replacefilter></p><p>    <replacetoken>@token4@</replacetoken> </p><p>    <replacevalue>value4</replacevalue></p><p>  </replacefilter></p><p></replace></p><p>复制代码</p><p> 调用chmod</p><p>
  11. </p><p>复制代码</p><p><chmod perm="go-rwx" type="file"></p><p>    <fileset dir="/web"></p><p>        <include name="**/*.cgi"/></p><p>        <include name="**/*.old"/></p><p>    </fileset></p><p>    <dirset dir="/web"></p><p>        <include name="**/private_*"/></p><p>    </dirset></p><p></chmod></p><p>复制代码</p><p>设置property</p><p>
  12. </p><p>复制代码</p><p><!--设置属性name-value--></p><p><property name="foo.dist" value="dist"/></p><p><!--读取属性文件中的属性配置--></p><p><property file="foo.properties"/></p><p><!--读取网络中的property-set--></p><p><property url="http://www.mysite/props/foo.properties"/></p><p><!--读取文件中的属性配置--></p><p><property resource="foo.properties"/></p><p><!--读取环境变量--></p><p><property environment="env"/></p><p>复制代码</p><p> 显示错误信息</p><p>
  13. </p><p>Fail task 退出当前构建,抛出BuildException,打印错误信息。</p><p>
  14. </p><p>message:A message giving further information on why the build exited</p><p>
  15. </p><p>if:Only fail if a property of the given name exists in the current project</p><p>
  16. </p><p>unless:Only fail if a property of the given name doesn't exist in the current project</p><p>
  17. </p><p>status:Exit using the specified status code; assuming the generated Exception is not caught, the JVM will exit with this status.Since Apache Ant 1.6.2</p><p>
  18. </p><p>复制代码</p><p><fail>Something wrong here.</fail></p><p><fail message="${属性}"/></p><p><!--如果这个属性不存在,显示错误--></p><p><fail unless="failCondition" message="unless Condition"/></p><p><!--如果这个属性存在,显示错误--></p><p><fail if="failCondition" message="if Condition"/></p><p><!--如果符合条件,显示错误--></p><p><fial message="tag condition"></p><p>    <condition></p><p>        <not></p><p>            <isset property="failCondition"/></p><p>        </not></p><p>    </condition></p><p></fail></p><p>复制代码</p><p>创建目录</p><p>
  19. </p><p><mkdir dir="${dist}/lib"/></p><p>打jar包</p><p>
  20. </p><p><jar destfile="${dist}/lib/app.jar" basedir="${build}/classes"/></p><p><jar destfile="${build}/lib/app.jar" basedir="${build}/classes" includes="mypackage/test/**" excludes="**/Test.class"/></p><p>打ear包</p><p>
  21. </p><p><ear destfile="build/myapp.ear" appxml="src/metadata/application.xml"></p><p>    <fileset dir="build" includes="*.jar,*war"/></p><p></ear></p><p>执行程序</p><p>
  22. </p><p>复制代码</p><p><target name="help"></p><p>    <exec executable="cmd"></p><p>        <arg value="/c"></p><p>        <arg value="ant.bat"/></p><p>        <arg value="-p"/></p><p>    </exec></p><p></target></p><p>复制代码</p><p>运行jar包</p><p>
  23. </p><p>复制代码</p><p><java classname="test.Main" dir="${exec.dir}" jar="${exec.dir}/dist/test.jar" fork="true" failonerror="true" maxmemory="128m"></p><p>    <arg value="-h"/></p><p>    <classpath></p><p>        <pathelement location="dist/test.jar"/></p><p>        <pathelement path="/Users/antoine/dev/asf/ant-core/bootstrap/lib/ant-launcher.jar"/></p><p>    </classpath></p><p></java></p><p>复制代码</p><p>  编译程序</p><p>
  24. </p><p><javac srcdir="${src}" destdit="${build}" classpath="xyz.jar" debug="on" source="1.4"/></p><p>Length使用</p><p>
  25. </p><p><!--把字符串foo的长度保存到属性length.foo中--></p><p><length string="foo" property="length.foo"/></p><p><!--把文件bar的长度保存到属性length.bar--></p><p><length file="bar" property="length.bar"/></p><p>输入input</p><p>
  26. </p><p>输入值保存在db.user中</p><p>
  27. </p><p><input message="Please enter db-username:" addproperty="db.user" defaultvalue="Scott-Tiger"/></p><p>压缩、解压缩</p><p>
  28. </p><p>复制代码</p><p><!--解压缩zip文件--></p><p><unzip src="apache-ant-bin.zip" dest="${tools.home}"></p><p>    <patternset>    </p><p>        <include name="apache-ant/lib/ant.jar"/></p><p>    </patternset></p><p>    <mapper type="flatten"/></p><p></unzip></p><p>
  29. </p><p><!--压缩zip文件--></p><p><zip destfile="${dist}/manual.zip" basedir="htdoc/manual" includes="api/**/*.html" excludes="**/todo.html"/></p><p>
  30. </p><p><!--打tar包--></p><p><tar destfile="/Users/antoine/dev/asf/ant-core/docs.tar"></p><p>    <tarfileset dir="${dir.src}/doc" fullpath="/usr/doc/ant/README" preserveLeadingSlashes="true"></p><p>        <include name="readme.txt"/></p><p>    <tarfileset></p><p>    <tarfileset dir="${dir.src}/docs" prefix="/usr/doc/ant" preserveLeadingSlashes="true"></p><p>        <include name="*.html"/></p><p>    </tarfileset></p><p></tar></p><p>
  31. </p><p><!--解压tar包--></p><p><untar src="tools.tar" dest="${tools.home}"/></p><p>复制代码</p><p>打war包</p><p>
  32. </p><p>复制代码</p><p><war destfile="myapp.war" webxml="src/metadata/myapp.xml"></p><p>    <fileset dir="src/html/myapp"/></p><p>    <fileset dir="src/jsp/myapp"/></p><p>    <lib dir="thirdparty/libs"></p><p>        <exclude name="jdbc1.jar"></p><p>    </lib></p><p>    <classes dir="build/main"/></p><p>    <zipfileset dir="src/graphics/images/gifs" prefix="images"/></p><p></war></p>
复制代码


回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-26 09:26 , Processed in 0.073514 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表