Ant 简明教程

Ant - Listeners & Loggers

Ant 允许使用侦听器和记录器监控构建过程。

Ant allows the build process to be monitored using listeners and loggers.

Listeners

Ant 提供以下事件,可以使用侦听器捕获这些事件。

Ant provides following events to be captured using listeners.

  1. build started

  2. build finished

  3. target started

  4. target finished

  5. task started

  6. task finished

  7. message logged

可以使用 -listener 参数在命令行上注册自定义侦听器。

Custom listeners can be registered on command line using -listener argument.

Loggers

记录器扩展了侦听器的功能,并添加了以下特性

Loggers extends listeners capabilities and add the following features

  1. Can log information to console or file using -logfile argument

  2. Can log using logging levels like -quiet, -verbose, -debug

  3. Are emacs-mode aware

Built-in Listeners/loggers

  1. org.apache.tools.ant.DefaultLogger − The logger used implicitly unless overridden with the -logger command-line switch.

  2. org.apache.tools.ant.NoBannerLogger − This logger omits output of empty target output.

  3. org.apache.tools.ant.listener.MailLogger − Extends DefaultLogger such that output is still generated the same, and when the build is finished an e-mail can be sent.

  4. org.apache.tools.ant.listener.AnsiColorLogger − Colorifies the build output.

  5. org.apache.tools.ant.listener.Log4jListener − Passes events to Apache Log4j for highly customizable logging.

  6. org.apache.tools.ant.XmlLogger − Writes the build information to an XML file.

  7. org.apache.tools.ant.TimestampedLogger − Prints the time that a build finished

  8. org.apache.tools.ant.listener.BigProjectLogger − Prints the project name every target

  9. org.apache.tools.ant.listener.SimpleBigProjectLogger − Prints the project name for subprojects only, otherwise like NoBannerLogger Since Ant 1.8.1

  10. org.apache.tools.ant.listener.ProfileLogger − The default logger, with start times, end times and durations added for each task and target.

Example

使用以下内容创建 build.xml:

Create build.xml with the following content:

<?xml version="1.0"?>
<project name="sample" basedir="." default="copy">
   <target name="copy">
     <echo>File Copied</echo>
   </target>
</project>

Output

在上述构建文件上运行 Ant 会生成以下输出:

Running Ant on the above build file produces the following output −

F:\tutorialspoint\ant>ant -logger org.apache.tools.ant.listener.TimestampedLogger
Buildfile: F:\tutorialspoint\ant\build.xml

copy:
   [echo] File Copied

BUILD SUCCESSFUL - at 03/12/21, 11:24 AM
Total time: 0 seconds

F:\tutorialspoint\ant>ant -logger org.apache.tools.ant.XmlLogger -verbose -logfile build_log.xml
Apache Ant(TM) version 1.10.12 compiled on October 13 2021
Trying the default build file: build.xml
Buildfile: F:\tutorialspoint\ant\build.xml

现在,你可以检查 build_log.xml 文件是否已使用相关日志创建。

Now you can check build_log.xml file is created with relevant logs.