http://ant.apache.org/manual/Tasks/exec.html

Exec

Description

Executes a system command. When the os attribute is specified, then the command is only executed when Apache Ant is run on one of the specified operating systems.

Note that you cannot interact with the forked program, the only way to send input to it is via the input and inputstring attributes. Also note that since Ant 1.6, any attempt to read input in the forked program will receive an EOF (-1). This is a change from Ant 1.5, where such an attempt would block.

If you want to execute an executable using a path relative to the project's basedir, you may need to use vmlauncher="false" on some operating systems - but even this may fail (Solaris 8/9 has been reported as problematic). The resolveexecutable attribute should be more reliable, as would be something like

  <property name="executable-full-path"
location="../relative/path/to/executable"/>
<exec executable="${executable-full-path}" ...

Windows Users

The <exec> task delegates to Runtime.exec which in turn apparently calls ::CreateProcess. It is the latter Win32 function that defines the exact semantics of the call. In particular, if you do not put a file extension on the executable, only ".EXE" files are looked for, not ".COM", ".CMD" or other file types listed in the environment variable PATHEXT. That is only used by the shell.

Note that .bat files cannot in general by executed directly. One normally needs to execute the command shell executable cmdusing the /c switch.

<target name="help">
<exec executable="cmd">
<arg value="/c"/>
<arg value="ant.bat"/>
<arg value="-p"/>
</exec>
</target>

A common problem is not having the executable on the PATH. In case you get an error message Cannot run program "...":CreateProcess error=2. The system cannot find the path specified. have a look at your PATH variable. Just type the command directly on the command line and if Windows finds it, Ant should do it too. (Otherwise ask on the user mailinglist for help.) If Windows can not execute the program add the directory of the program to the PATH (set PATH=%PATH%;dirOfProgram) or specify the absolute path in the executable attribute in your buildfile.

Cygwin Users

The <exec> task will not understand paths such as /bin/sh for the executable parameter. This is because the Java VM in which Ant is running is a standard Windows executable and is not aware of the Cygwin environment (i.e., doesn't load cygwin1.dll). The only work-around for this is to compile a JVM under Cygwin (at your own risk). See for instance OpenJDK build instructions for cygwin.

OpenVMS Users

The command specified using executable and <arg> elements is executed exactly as specified inside a temporary DCL script. This has some implications:

  • paths have to be written in VMS style
  • if your executable points to a DCL script remember to prefix it with an @-sign (e.g. executable="@[FOO]BAR.COM"), just as you would in a DCL script

For <exec> to work in an environment with a Java VM older than version 1.4.1-2 it is also required that the logicalJAVA$FORK_SUPPORT_CHDIR is set to TRUE in the job table (see the JDK Release Notes).

Please note that the Java VM provided by HP doesn't follow OpenVMS' conventions of exit codes. If you run a Java VM with this task, the task may falsely claim that an error occurred (or silently ignore an error). Don't use this task to runJAVA.EXE, use a <java> task with the fork attribute set to true instead as this task will follow the VM's interpretation of exit codes.

RedHat S/390 Users

It has been reported on the VMESA-LISTSERV that shell scripts invoked via the Ant Exec task must have their interpreter specified, i.e., the scripts must start with something like:

#!/bin/bash

or the task will fail as follows:

[exec] Warning: UNIXProcess.forkAndExec native error: Exec format error
[exec] Result: 255

Running Ant as a background process on Unix(-like) systems

If you run Ant as a background process (like ant &) and use the <exec> task with spawn set to false, you must provide explicit input to the forked process or Ant will be suspended because it tries to read from the standard input.

Parameters

Attribute Description Required
command the command to execute with all command line arguments. deprecated, use executable and nested <arg> elements instead. Exactly one of the two.
executable the command to execute without any command line arguments.
dir the directory in which the command should be executed. No.
Note: the default used when dir has not been specified depends on thevmlauncher attribute. Ifvmlauncher is true the task will use the current working directory, otherwise it uses the project's basedir.
os list of Operating Systems on which the command may be executed. If the current OS's name is contained in this list, the command will be executed. The OS's name is determined by the Java Virtual machine and is set in the "os.name" system property. No
osfamily OS family as used in the <os> condition. since Ant 1.7 No
spawn whether or not you want the command to be spawned
Default is false.
If you spawn a command, its output will not be logged by ant.
The input, output, error, and result property settings are not active when spawning a process.
since Ant 1.6
No
output Name of a file to which to write the output. If the error stream is not also redirected to a file or property, it will appear in this output. No
error The file to which the standard error of the command should be redirected.since Ant 1.6 No
logError This attribute is used when you wish to see error output in Ant's log and you are redirecting output to a file/property. The error output will not be included in the output file/property. If you redirect error with the "error" or "errorProperty" attributes, this will have no effect. since Ant 1.6 No
append Whether output and error files should be appended to or overwritten. Defaults to false. No
outputproperty The name of a property in which the output of the command should be stored. Unless the error stream is redirected to a separate file or stream, this property will include the error output. No
errorproperty The name of a property in which the standard error of the command should be stored. since Ant 1.6 No
input A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring attribute. since Ant 1.6 No
inputstring A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input attribute. since Ant 1.6 No
resultproperty the name of a property in which the return code of the command should be stored. Only of interest if failonerror=false. No
timeout Stop the command if it doesn't finish within the specified time (given in milliseconds). No
failonerror Stop the buildprocess if the command exits with a return code signaling failure. Defaults to false. No
failifexecutionfails Stop the build if we can't start the program. Defaults to true. No
newenvironment Do not propagate old environment when new environment variables are specified. No, default is false
vmlauncher Run command using the Java VM's execution facilities where available. If set to false the underlying OS's shell, either directly or through the antRun scripts, will be used. Under some operating systems, this gives access to facilities not normally available through the VM including, under Windows, being able to execute scripts, rather than their associated interpreter. If you want to specify the name of the executable as a relative path to the directory given by the dir attribute, it may become necessary to set vmlauncher to false as well. No, default is true
resolveexecutable When this attribute is true, the name of the executable is resolved firstly against the project basedir and if that does not exist, against the execution directory if specified. On Unix systems, if you only want to allow execution of commands in the user's path, set this to false. since Ant 1.6 No, default is false
searchpath When this attribute is true, then system path environment variables will be searched when resolving the location of the executable. since Ant 1.6.3 No, default is false

Examples

<exec dir="${src}" executable="cmd.exe" os="Windows 2000" output="dir.txt">
<arg line="/c dir"/>
</exec>

Parameters specified as nested elements

arg

Command line arguments should be specified as nested <arg> elements. See Command line arguments.

env

It is possible to specify environment variables to pass to the system command via nested <env> elements.

Attribute Description Required
key The name of the environment variable. 
Note: (Since Ant 1.7) For windows, the name is case-insensitive.
Yes
value The literal value for the environment variable. Exactly one of these.
path The value for a PATH like environment variable. You can use ; or : as path separators and Ant will convert it to the platform's local conventions.
file The value for the environment variable. Will be replaced by the absolute filename of the file by Ant.

redirector

Since Ant 1.6.2

A nested I/O Redirector can be specified. In general, the attributes of the redirector behave as the corresponding attributes available at the task level. The most notable peculiarity stems from the retention of the <exec> attributes for backwards compatibility. Any file mapping is done using a null sourcefile; therefore not all Mapper types will return results. When no results are returned, redirection specifications will fall back to the task level attributes. In practice this means that defaults can be specified for input, output, and error output files.

Errors and return codes

By default the return code of a <exec> is ignored; when you set failonerror="true" then any return code signaling failure (OS specific) causes the build to fail. Alternatively, you can set resultproperty to the name of a property and have it assigned to the result code (barring immutability, of course).

If the attempt to start the program fails with an OS dependent error code, then <exec> halts the build unlessfailifexecutionfails is set to false. You can use that to run a program if it exists, but otherwise do nothing.

What do those error codes mean? Well, they are OS dependent. On Windows boxes you have to look at the documentation; error code 2 means 'no such program', which usually means it is not on the path. Any time you see such an error from any Ant task, it is usually not an Ant bug, but some configuration problem on your machine.

Examples

<exec executable="emacs">
<env key="DISPLAY" value=":1.0"/>
</exec>

starts emacs on display 1 of the X Window System.

<property environment="env"/>
<exec ... >
<env key="PATH" path="${env.PATH}:${basedir}/bin"/>
</exec>

adds ${basedir}/bin to the PATH of the system command.

<property name="browser" location="C:/Program Files/Internet Explorer/iexplore.exe"/>
<property name="file" location="ant/docs/manual/index.html"/> <exec executable="${browser}" spawn="true">
<arg value="${file}"/>
</exec>

Starts the ${browser} with the specified ${file} and end the Ant process. The browser will remain.

<exec executable="cat">
<redirector outputproperty="redirector.out"
errorproperty="redirector.err"
inputstring="blah before blah">
<inputfilterchain>
<replacestring from="before" to="after"/>
</inputfilterchain>
<outputmapper type="merge" to="redirector.out"/>
<errormapper type="merge" to="redirector.err"/>
</redirector>
</exec>

Sends the string "blah before blah" to the "cat" executable, using an <inputfilterchain> to replace "before" with "after" on the way in. Output is sent to the file "redirector.out" and stored in a property of the same name. Similarly, error output is sent to a file and a property, both named "redirector.err".

Note: do not try to specify arguments using a simple arg-element and separate them by spaces. This results in only a single argument containing the entire string.

Timeouts: If a timeout is specified, when it is reached the sub process is killed and a message printed to the log. The return value of the execution will be "-1", which will halt the build if failonerror=true, but be ignored otherwise.

ant exec的更多相关文章

  1. Ant 参考

    http://ant.apache.org/manual/Tasks/exec.html Exec Description Executes a system command. When the os ...

  2. JavaEE开发环境配置

    JavaEE开发环境配置 Tomcat的下载和安装 1.登录Tomcat 站点,下载Tomcat最新版本http://tomcat.apache.org/Windows平台下载ZIP包,LInux平台 ...

  3. Ant 执行 exec cmd.exe 时路径包含空格的问题

    需求描述 通过Ant脚本调用bat脚本 问题描述 bat脚本所在目录名称包含空格(space),cmd.exe调用时候报错The system cannot find the path specifi ...

  4. Eclipce结合Ant进行编译、打包、传输、运行

    注意: 用Ant构建时,build path只能是单级的,如默认的src,如果是类似basePath/jsr253这样的话,运行Ant build时会报错,说找不到jsr253. (此文讲述的是以an ...

  5. ant 使用指南

    一.概述 ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发.在实际软件开发中,有很多地方可以用到ant. 开发环境: System:Windo ...

  6. windows+ant+git+tomcat中ant直接获取git项目部署注意点

    最近项目搬迁到公司的"GitHub"上面原来的SVN的ant发布脚本要改下,于是百度ant获取git的方法太少了,windows平台上更是没有所以搞了两天,今天终于有点成果分享给大 ...

  7. Ant搭建 一键生成APP技术 平台

    1.博客概要 本文详细介绍了当今流行的一键生成APP技术.介绍了这种设计思想的来源,介绍了国内外的研究背景,并介绍了这个技术体系中的一些实现细节,欢迎各路大神们多提意见.一键生成技术,说的通俗点就是, ...

  8. Ant 常用语法及选项

    project 项目定义,一个ant文件就是一个 project,定义了项目名称,起始位置以及默认执行的 target. <project name="Easily" bas ...

  9. ant 介绍 http://blog.csdn.net/sunjavaduke/archive/2007/03/08/1523819.aspx

    转自: 本内容包含了Ant的历史简要介绍,Ant的功能以及Ant框架的介绍,并对下载安装使用Ant进行了示例介绍,同时通过一个Java程序讲解了Ant的基本使用方法. 1.       Ant简介:这 ...

随机推荐

  1. (传输层)UDP协议

    目录 数据单位特点具体实现要求UDP首部格式发送UDP请求的客户端图释 数据单位 UDP 传送的数据单位协议是 UDP 报文或用户数据报 特点 UDP 是无连接的,即发送数据之前不需要建立连接 UDP ...

  2. WebApi 使用PUT和DELETE时报405的问题

    最近两天写了个项目,里面有一个接口是用谓词delete接收请求. 本地完全没问题,但是当发布到服务器上之后(IIS7.5),就报出 405.0 - Method Not Allowed 很明显是配置问 ...

  3. java使用this关键字调用本类重载构造器

    在构造器中可以调用本类的其他重载构造器,不能使用构造器名称来调用另一个构造器,而是应该使用Java特定的this(-.)来调用. this(-.)方法必须出现在构造器中的第一行,用来调用其他重载构造器 ...

  4. CBarChart柱形图类

    在用VC做有关图表的时候,感觉不是那么方便,在codeproject找到一个柱形图的实用类,原文地址为:http://www.codeproject.com/KB/miscctrl/CBarChart ...

  5. MyCat 学习笔记 第七篇.数据分片 之 按数据范围分片

    1 应用场景 Mycat 其实自带了2个数据范围分片的方案,一个是纯数据范围的分片,比如 1至 10000 号的数据放到分片1 ,10001 至 20000号数据放到分片2里. 另一个是数据常量形式的 ...

  6. QT添加程序图标及窗口图标

    程序图标 材料准备 图标文件:*.ico文件,存放在源文件同一目录下,如"myapp.ico" 写入图标 向*.pro文件中,独立一行写入"RC_ICONS = *.ic ...

  7. 烂泥:LVM学习之逻辑卷、卷组及物理卷删除

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上篇文章,我们介绍了有关LVM的逻辑卷及卷组的空间缩小.这次我们来介绍下如何删除一个逻辑卷及卷组. 删除逻辑卷需要以下几个步骤: 1. 卸载已经挂载的逻 ...

  8. 关于google电子地图跟卫星地图位置不重合

    再做项目时,用到了google地图的显示位置,就是在网页上插入事物在地图上的位置,点击卫星地图跟电子地图时发现不是重合,网上GOOGLE了下,说是加密的问题给偏移了500米左右,用google测量工具 ...

  9. Linux 环境下如何使 Chrome 浏览器字体更漂亮

    Windows 就免谈了,本身字体渲染技术 Cleartype 以及 DirectWrite 就稀烂得一塌糊涂.Mac 下面本来字体渲染就很好,所以关键就是在 Linux 下如何使 Chrome 的字 ...

  10. CORDIC原理与FPGA实现(2)

    CORDIC算法实现极坐标(polar)到直角坐标系(Cartesian)的变换. 1: function [horizonal,vertical]=polar2car(mag, pha); 2: x ...