Ant 参考

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 cmd    using 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 logical JAVA$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 run JAVA.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 the vmlauncher attribute.  If       vmlauncher 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 unless failifexecutionfails 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.

时间: 2024-10-08 09:45:30

Ant 参考的相关文章

使用Ant打包Android应用详解——Ant使用解析

上篇<使用Ant打包Android应用详解>描述了使用Ant打包的流程,但很多步骤并没有说明如此做的原因,本篇将从Ant方面来理解,下一篇从APK生成的流程来说明. APK包的生成是一系列操作的结果,而Ant则是将这一系列操作流程化,提供出定制化的接口,以及可配置的参数供修改,而这些都是通过指定的构建文件来实现.我们就从Ant的打包流程来理解Ant的一些基本用法.当在命令行中执行ant,默认会去解析当前目录的build.xml作为构建文件.下面是个删除部分注视的版本: <project

使用Ant打包Android应用详解

计划写个完整的使用Ant打包Android应用的系列文章,三篇文章.首篇详细介绍采用Ant打包Android应用的流程,列出部分定制问题及其解决方法,第二篇介绍我理解的Ant打包的思路与基本的概念和使用,最后一篇描述apk包的生成过程. 实现的环境: 操作系统:ubuntu 64bit 14.04 Android SDK Build Tools:22.0.1 Android SDK Tools:24.3.3 Ant:1.9.4 ProGuard:5.2.1 JDK:1.7 一.简单应用出包流程

ant完成文件上传和启动服务

首先,请先下载JAR包.Ant上传文件到Linux服务器使用scp,需要下载jar包jsch.jar,将jar包放入ant的lib文件夹下. <?xml version="1.0" ?> <project name="${project.name}" default="start-server" basedir="."> <property name="password" va

Vue常用经典开源项目汇总参考-海量

Vue常用经典开源项目汇总参考-海量 Vue是什么? Vue.js(读音 /vju/, 类似于 view) 是一套构建用户界面的 渐进式框架.与其他重量级框架不同的是,Vue 采用自底向上增量开发的设计.Vue 的核心库只关注视图层,并且非常容易学习,非常容易与其它库或已有项目整合.另一方面,Vue 完全有能力驱动采用单文件组件和 Vue 生态系统支持的库开发的复杂单页应用. Vue.js 的目标是通过尽可能简单的 API 实现响应的数据绑定和组合的视图组件. 易用(已经会了HTML,CSS,J

混合式app ionic2.x 手动搭建开发环境教程分享(nodejs,jdk,ant,androidsdk)

1.ionic简介 为什么选用ionic: 彻底开源且免费 性能优异 基于红的发紫的AngularJs 漂亮的UI 强大的命令行(基于更热门的nodejs) 开发团队非常活跃 ngCordova,将主流的Cordova API或者Cordova插件封装为AngularJS扩展,使用非常方便 开源免费的webfont icon库ionicons,基本满足你icon需求 2.相关下载: Node.js(npm安装工具) :  https://nodejs.org/en/ jdk (android编译

ANT+ 之【心率传感器设计】【下】

好久不见!!最近的几个月发生了不少事情,导致精力极度分散,自己todolist上各个项目的优先级不停地被调整,博客的事情就被暂时搁置了下来,特别是__AW君__的出现,让自己突然有了一种说不出来的感觉……唔,果然love is blind没错,所幸自己毕竟是一个天生乐观的人,接下来的剧情我会用心去体验的~ 让我们永远相信美好的事情即将发生吧! /****************************************************************************

Ant学习-001-ant 基础知识及windows环境配置

一.Ant 概要基础知识 Apache Ant 是一个将软件编译.测试.部署等步骤联系在一起加以自动化的一个工具,大多用于Java环境中的软件开发,用以构建应用,或结合其他开源测试工具例如 git.TestNG等搭建持续集成测试开发环境,从而高效.高质量的构建产品. Ant 有以下一些优点: 跨平台,可移植.Ant 是 Java 语言编写的,所以有很好的跨平台性和可移植性,无论是在 windows.Linux,还是 mac. 操作简单. Ant 是由一个内置任务和可选任务组成的,运行时需要一个构

Linux下把ANT添加进环境变量(PATH)

在终端输入如下命令: source /etc/profile sudo gedit /etc/profile #sudo是为了取得编辑的权限 在profile文件下添加下面两行 ANT_HOME=/home/jij/workspace/apache-ant-1.9.4/binPATH=$ANT_HOME/bin:$PATH 保存关闭文件,输入如下命令测试是否成功,返回版本号的话,说明PATH设置成功 ant -versionApache Ant(TM) version 1.9.3 compile

ANT教程经典

Ant是一个Apache基金会下的跨平台的构件工具,它可以实现项目的自动构建和部署等功能.在本文中,主要让读者熟悉怎样将Ant应用到Java项目中,让它简化构建和部署操作. 一.             安装与配置 下载地址:http://ant.apache.org/,在本文中下载的是1.7.0版本.解压到某个目录(例如E:"apache-ant-1.7.0),即可使用. 添加系统环境变量:ANT_HOME,该变量指向Ant解压后的根目录,在此为E:"apache-ant-1.7.0