`

ANT学习笔记

阅读更多

Apache Ant™

Apache Ant is a Java library and command-line tool whose mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications. Ant can also be used effectively to build non Java applications, for instance C or C++ applications. More generally, Ant can be used to pilot any type of process which can be described in terms of targets and tasks.

The latest version Ant1.8.2.

http://ant.apache.org/

1.       Ant安装(Windows)

  • 下载ant最新版本并解压到文件目录
  • 设置ANT_HOME环境变量
  • JAVA_HOME目录
  • 添加%ANT_HOME%\bin path
  • 检查是否设置成功:ant -version

 

2.       Ant构建文件(build.xml)

Build.xmlxml编写, ant构建文件对大小写敏感

 

3.       运行ant

ant命令:

ant [options] [target [target2 [target3] ...]]
 

 Options:

  -help, -h              print this message

  -projecthelp, -p       print project help information

  -version               print the version information and exit

  -diagnostics           print information that might be helpful to

                         diagnose or report problems.

  -quiet, -q             be extra quiet

  -verbose, -v           be extra verbose

  -debug, -d             print debugging information

  -emacs, -e             produce logging information without adornments

  -lib <path>            specifies a path to search for jars and classes

  -logfile <file>        use given file for log

    -l     <file>                ''

  -logger <classname>    the class which is to perform logging

  -listener <classname>  add an instance of class as a project listener

  -noinput               do not allow interactive input

  -buildfile <file>      use given buildfile

    -file    <file>              ''

    -f       <file>              ''

  -D<property>=<value>   use value for given property

  -keep-going, -k        execute all targets that do not depend

                         on failed target(s)

  -propertyfile <name>   load all properties from file with -D

                         properties taking precedence

  -inputhandler <class>  the class which will handle input requests

  -find <file>           (s)earch for buildfile towards the root of

    -s  <file>           the filesystem and use it

  -nice  number          A niceness value for the main thread:

                         1 (lowest) to 10 (highest); 5 is the default

  -nouserlib             Run ant without using the jar files from ${user.home}/.ant/lib

  -noclasspath           Run ant without using CLASSPATH

  -autoproxy             Java 1.5+ : use the OS proxies

  -main <class>          override Ant's normal entry point

Examples

ant

Ant将打开默认的构建文件,并执行默认的目标compile. 默认情况下,ant命令会在当前目录下寻找build.xml

ant –buildfile test.xml

runs Ant using the test.xml file in the current directory, on the default target.

ant -buildfile test.xml dist

runs Ant using the test.xml file in the current directory, on the target called dist.

ant -buildfile test.xml -Dbuild=build/classes dist

runs Ant using the test.xml file in the current directory, on the target called dist, setting the build property to the value build/classes.

ant -lib /home/ant/extras

runs Ant picking up additional task and support jars from the /home/ant/extras location

ant -lib one.jar;another.jar

ant -lib one.jar -lib another.jar

adds two jars to Ants classpath.

 

4.       Using Apache Ant

Structure of Buildfile

 

 

 

Task1

Task2

Property

Path

Taskn

taskref

Projects

A project has three attributes:

Attribute

Description

Required

name

the name of the project.

No

default

the default target to use when no target is supplied.

No; however, since Ant 1.6.0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project's initialization, even when Ant is run with the -projecthelp option.

basedir

the base directory from which all path calculations are done. This attribute might be overridden by setting the "basedir" property beforehand. When this is done, it must be omitted in the project tag. If neither the attribute nor the property have been set, the parent directory of the buildfile will be used.

 

e.g:

<project name="CopsTestComp" default="show property" basedir=”.”>

Targets

A target can depend on other targets. You might have a target for compiling, for example, and a target for creating a distributable. You can only build a distributable when you have compiled first, so the distribute target depends on the compile target. Ant resolves these dependencies.

For example:

<target name="jar" depends="clean,build">

      ……

</target>

Tasks

A task is a piece of code that can be executed.

<target name="build">

<mkdir dir="${path.build}" />       //task1

<javac classpath="${path.build}" debug="true" srcdir="src" destdir="${path.build}">       //task2

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

<classpath refid= "compile.classpath "/>

</javac>

</target>

 

Properties

Properties are an important way to customize a build process or to just provide shortcuts for strings that are used repeatedly inside a build file.

  <property name="src" location="src"/>

  <property name="build" location="build"/>

  <property name="dist"  location="dist"/>

 

Use of external tasks

Ant supports a plugin mechanism for using third party tasks. For using them you have to do two steps:

  1. place their implementation somewhere where Ant can find them
  2. declare them.

Don't add anything to the CLASSPATH environment variable - this is often the reason for very obscure errors. Use Ant's own mechanisms for adding libraries:

  • via command line argument -lib
  • adding to ${user.home}/.ant/lib
  • adding to ${ant.home}/lib

For the declaration there are several ways:

  • declare a single task per using instruction using

<taskdef name="jag_connect"  classname="com.sybase.jaguar.management.jagtool.ant.ConnectTask" />

  • declare a bundle of tasks using a properties-file holding these taskname-ImplementationClass-pairs and <taskdef>
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" /> <for ... />
  • declare a bundle of tasks using a xml-file holding these taskname-ImplementationClass-pairs and <taskdef>
    <taskdef resource="net/sf/antcontrib/antlib.xml" /> <for ... />
  • declare a bundle of tasks using a xml-file named antlib.xml, XML-namespace and antlib: protocoll handler
    <project xmlns:ac="antlib:net.sf.antconrib"/> <ac:for ... />

Example Buildfile

<?xml version="1.0"?>

<project name="Test" default="show property" basedir=”.”>

 

  <property name="jar.file"        value="${ant.project.name}.jar" />

  <property name="entity.name"     value="Package:${ant.project.name}" />

  <property name="list.type"       value="Package" />

  <property name="path.build"      value="${basedir}/bin" />

  <property name="path.jar"        value="D:/jar/jar_hksg/" />

  <property name="webs.jar"        value="D:/jar/jar_hksg/" />

     

      <!-- EAServer properties -->

  <property name="jaguar.host"     value="192.168.0.11" />

  <property name="jaguar.port"     value="9091" />

  <property name="jaguar.user"     value="addddd" />

  <property name="jaguar.password" value="123456" />

 

      <!-- import EAServer task -->

  <taskdef name="jag_connect"  classname="com.sybase.jaguar.management.jagtool.ant.ConnectTask" />

  <taskdef name="jag_deploy"   classname="com.sybase.jaguar.management.jagtool.ant.DeployTask" />

  <taskdef name="jag_delete"   classname="com.sybase.jaguar.management.jagtool.ant.DeleteTask" />

  <taskdef name="jag_refresh"  classname="com.sybase.jaguar.management.jagtool.ant.RefreshTask" />

  <taskdef name="jag_restart"  classname="com.sybase.jaguar.management.jagtool.ant.RestartTask" />

  <taskdef name="jag_shutdown" classname="com.sybase.jaguar.management.jagtool.ant.ShutdownTask" />

  <taskdef name="jag_list"     classname="com.sybase.jaguar.management.jagtool.ant.ListTask" />

 

  <target name="jar" depends="clean,build">

      <delete file="${jar.file}"/>

      <jar destfile="${jar.file}">

        <fileset dir="${path.build}" includes="**/*.*" />

      <fileset dir="${basedir}"    includes="META-INF/**" />

      </jar>

  </target>

 

  <target name="connect">

    <jag_connect host="${jaguar.host}" port="${jaguar.port}" user="${jaguar.user}" password="${jaguar.password}" />

  </target>

     

  <target name="restart" depends="connect">

    <jag_restart />

    <waitfor checkevery="1" checkeveryunit="second">

      <socket server="${jaguar.host}" port="${jaguar.port}" />

    </waitfor>

  </target>

     

  <target name="shutdown" depends="connect">

    <jag_shutdown />

  </target>

 

  <target name="deploy" depends="jar,connect">

    <jag_deploy type="ejbjar" install="true" strategy="full" file="${basedir}/${jar.file}" verbose="true" />

    <jag_refresh entity="${entity.name}" />

  </target>

 

  <target name="undeploy" depends="connect">

    <jag_delete entity="${entity.name}" />

  </target>

           

  <target name="list" depends="connect">

    <jag_list type="${list.type}" />

  </target>

     

  <target name="refresh" depends="connect">

    <jag_refresh entity="${entity.name}" />

  </target>

 

  <target name="clean">

       <delete includeEmptyDirs="true">

            <fileset dir="${path.build}" />

       </delete>

  </target>

 

  <target name="build">

       <mkdir dir="${path.build}" />

       <javac classpath="${path.build}" debug="true" srcdir="src" destdir="${path.build}">

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

            <classpath refid= "compile.classpath "/>

       </javac>

  </target>

 

     

  <path id= "compile.classpath ">

      <pathelement path = "${path.jar}easj2ee.jar"/>       

      <pathelement path = "${path.jar}log4j-1.2.15.jar "/>

      <pathelement path = "${path.jar}easserver.jar"/>

      <pathelement path = "${path.jar}easclient.jar"/>

      <pathelement path = "${path.jar}hksps.jar"/>

      <pathelement path = "${path.jar}common.jar"/>

      <pathelement path = "${path.jar}mbclnt50.jar"/>

  </path>

 

  <target name="show property">

    <echo message="Host:${jaguar.host}"/>

    <echo message="Port:${jaguar.port}"/>

  </target>

</project>

The structure of the build.xml:

 

structure of build

Stucture of build.xml

 

参考文档

Apache Ant™ 1.8.2 Manual(http://ant.apache.org/manual/index.html)

  • 大小: 42.5 KB
分享到:
评论

相关推荐

    ant学习笔记之(ant执行命令的详细参数和Ant自带的系统属性)

    《Ant学习笔记:详解Ant执行命令参数与系统属性》 Ant,作为一个基于Java的构建工具,因其跨平台性以及XML格式的构建脚本而被广泛应用于自动化构建过程,如编译、打包、测试等。本篇文章将深入探讨Ant执行命令的...

    ant 学习 笔记 一个简单的java 编译部署实例

    根据提供的文件信息,这里将深入解析“ant学习笔记:一个简单的java编译部署实例”,涵盖标题、描述、标签以及部分内容中提及的关键知识点。 ### Apache Ant简介 Apache Ant是一款开源的Java环境下的自动化构建...

    Ant学习笔记

    **Ant学习笔记** Ant是一个基于Java的构建工具,它的全称是Another Neat Tool,主要应用于Java项目的构建。Ant的设计理念是通过XML配置文件来定义构建过程,而非依赖于特定平台的shell命令。每个构建任务是由实现了...

    Ant 学习笔记

    **Ant学习笔记** Apache Ant,一个Java库和命令行工具,其任务是驱动构建过程。它是Java世界中广泛使用的构建工具,类似于Unix世界的Make。Ant以其XML格式的构建文件(通常命名为`build.xml`)而闻名,这个文件包含...

    ant_学习笔记

    ### ant学习笔记:深入了解ant构建工具 #### 引言 `ant`,作为一款源自动网络的学习笔记,主要探讨了Apache Ant这一强大的构建工具。Apache Ant是一款开源的、跨平台的构建工具,专为Java应用程序设计,旨在简化并...

    ant 学习笔记

    NULL 博文链接:https://worktianqi.iteye.com/blog/2162932

    Ant 1.9.1 学习笔记

    Ant 1.9.1是Ant的一个版本,学习笔记通常记录了使用该工具的基本操作和配置方法。 ### Ant的下载和安装 要使用Ant,首先需要下载并安装。在Windows系统中,通常需要配置环境变量以便于命令行中使用Ant命令。ANT_...

    ant个人学习笔记和简单示例

    总的来说,这个“ant个人学习笔记和简单示例”应该能帮助你掌握Ant的基本用法,理解构建过程的自动化,以及如何编写和维护自己的构建文件。通过学习和实践其中的示例,你将能够熟练地运用Ant来构建和管理Java项目,...

    Java/JavaEE 学习笔记

    Java/JavaEE 学习笔记 作者在杰普学习时的学习笔记,是J2ee初学者必备手册,是大家学习J2EE开发的很好的参考笔记。 Java/JavaEE 学习笔记 内容...ant学习笔记...................387 Web Service学习笔记.....388

    Ant构建工具学习笔记

    《Ant构建工具学习指南》 Ant,作为Java领域的一个强大构建工具,它的主要作用在于将复杂的项目构建过程规范化、自动化,使得开发者能够更专注于代码的编写而非构建流程。本文将深入探讨Ant的基本概念、安装配置、...

    J2EE学习笔记(J2ee初学者必备手册)

    内容目录 .......................1 Java/JavaEE.....1 2008年11月1日...............1 Unix 学习笔记7 一、Unix前言............7 ...ant学习笔记...................387 Web Service学习笔记.....388

    ANT學習筆記(一)——ANT 結合JUNIT4學習DEMO

    **ANT学习笔记(一)——ANT结合JUNIT4学习DEMO** ANT,全称为Apache Ant,是一个基于Java的构建工具,用于自动化Java项目的构建、编译、测试和部署过程。它通过XML配置文件来定义任务,使得项目构建过程可配置且可...

    ANT學習筆記(二)—— 應用於WEB的ANT測試DEMO

    在本篇ANT学习笔记中,我们将探讨如何将ANT应用于WEB项目的测试DEMO。ANT是一个流行的Java构建工具,它允许开发者自动化构建、测试和部署软件。本文档将介绍ANT的基本概念,以及如何配置和运行一个针对WEB应用的构建...

    ant的学习笔记.doc

    ### ant的学习笔记知识点详解 #### 一、Ant的作用与特性 Ant是一款强大的自动化构建工具,主要应用于Java项目,能够高效地处理项目的编译、打包、测试等任务。它采用XML格式编写构建脚本,这使得Ant具有良好的跨...

    Ant打包编译部署工具学习笔记2(附件中含有测试项目)

    在"Ant打包编译部署工具学习笔记2"中,我们可以预期博主分享了关于如何利用Ant进行更复杂的构建操作,例如集成测试、优化和打包。这可能包括如何配置build.xml文件,添加自定义任务,以及如何处理依赖关系。由于没有...

    J2EE 资源集合

    3. **Ant学习笔记.doc与Ant权威指南.pdf** Ant是Java世界里常用的构建工具,能够自动化执行编译、打包、测试等任务。文档和PDF提供了Ant的基本使用、任务定义、宏定义等,对于项目构建流程的管理至关重要。 4. **...

Global site tag (gtag.js) - Google Analytics