http://blog.csdn.net/sunjavaduke/archive/2007/03/15/1530069.aspx
<target name="sh" depends="compile">
<exec command="sh ./runsvr.sh" failonerror="false"/>
</target>
附录:
<javac>任务介绍:
Javac
Description
Compiles a Java source tree.
The source and destination directory will be recursively scanned for Java source files to compile. Only Java files that have no corresponding .class
file or where the class file is older than the .java
file will be compiled.
Note: Ant uses only the names of the source and class files to find the classes that need a rebuild. It will not scan the source and therefore will have no knowledge about nested classes, classes that are named different from the source file, and so on. See the
<depend>
task for dependency checking based on other than just existence/modification times.
When the source files are part of a package, the directory structure of the source tree should follow the package hierarchy.
It is possible to refine the set of files that are being compiled. This can be done with the
includes
,
includesfile
,
excludes
, and
excludesfile
attributes. With the
includes
or
includesfile
attribute, you specify the files you want to have included. The
exclude
or
excludesfile
attribute is used to specify the files you want to have excluded. In both cases, the list of files can be specified by either the filename, relative to the directory(s) specified in the
srcdir
attribute or nested
<src>
element(s), or by using wildcard patterns. See the section on
directory-based tasks, for information on how the inclusion/exclusion of files works, and how to write wildcard patterns.
It is possible to use different compilers. This can be specified by either setting the global build.compiler
property, which will affect all <javac>
tasks throughout the build, or by setting the compiler
attribute, specific to the current <javac>
task. Valid values for either the build.compiler
property or the compiler
attribute are:
classic
(the standard compiler of JDK 1.1/1.2) – javac1.1
and javac1.2
can be used as aliases.
modern
(the standard compiler of JDK 1.3/1.4/1.5) – javac1.3
and javac1.4
and javac1.5
can be used as aliases.
jikes
(the Jikes compiler).
jvc
(the Command-Line Compiler from Microsoft's SDK for Java / Visual J++) – microsoft
can be used as an alias.
kjc
(the kopi compiler).
gcj
(the gcj compiler from gcc).
sj
(Symantec java compiler) – symantec
can be used as an alias.
extJavac
(run either modern or classic in a JVM of its own).
The default is javac1.x
with x
depending on the JDK version you use while you are running Ant. If you wish to use a different compiler interface than those supplied, you can write a class that implements the CompilerAdapter interface (package org.apache.tools.ant.taskdefs.compilers
). Supply the full classname in the build.compiler
property or the compiler
attribute.
The fork attribute overrides the build.compiler
property or compiler
attribute setting and expects a JDK1.1 or higher to be set in JAVA_HOME
.
You can also use the compiler
attribute to tell Ant which JDK version it shall assume when it puts together the command line switches - even if you set fork="true"
. This is useful if you want to run the compiler of JDK 1.1 while you current JDK is 1.2+. If you use compiler="javac1.1"
and (for example) depend="true"
Ant will use the command line switch -depend
instead of -Xdepend
.
This task will drop all entries that point to non-existent files/directories from the classpath it passes to the compiler.
Windows Note:When the modern compiler is used in unforked mode on Windows, it locks up the files present in the classpath of the <javac>
task, and does not release them. The side effect of this is that you will not be able to delete or move those files later on in the build. The workaround is to fork when invoking the compiler.
Parameters
Attribute | Description | Required |
srcdir | Location of the java files. (See the note below.) | Yes, unless nested <src> elements are present. |
destdir | Location to store the class files. | No |
includes | Comma- or space-separated list of files (may be specified using wildcard patterns) that must be included; all .java files are included when omitted. | No |
includesfile | The name of a file that contains a list of files to include (may be specified using wildcard patterns). | No |
excludes | Comma- or space-separated list of files (may be specified using wildcard patterns) that must be excluded; no files (except default excludes) are excluded when omitted. | No |
excludesfile | The name of a file that contains a list of files to exclude (may be specified using wildcard patterns). | No |
classpath | The classpath to use. | No |
sourcepath | The sourcepath to use; defaults to the value of the srcdir attribute (or nested <src> elements). To suppress the sourcepath switch, use sourcepath="" . | No |
bootclasspath | Location of bootstrap class files. | No |
classpathref | The classpath to use, given as a reference to a path defined elsewhere. | No |
sourcepathref | The sourcepath to use, given as a reference to a path defined elsewhere. | No |
bootclasspathref | Location of bootstrap class files, given as a reference to a path defined elsewhere. | No |
extdirs | Location of installed extensions. | No |
encoding | Encoding of source files. (Note: gcj doesn't support this option yet.) | No |
nowarn | Indicates whether the -nowarn switch should be passed to the compiler; defaults to off . | No |
debug | Indicates whether source should be compiled with debug information; defaults to off . If set to off , -g:none will be passed on the command line for compilers that support it (for other compilers, no command line argument will be used). If set to true , the value of the debuglevel attribute determines the command line argument. | No |
debuglevel | Keyword list to be appended to the -g command-line switch. This will be ignored by all implementations except modern , classic(ver >= 1.2) and jikes . Legal values are none or a comma-separated list of the following keywords: lines , vars , and source . If debuglevel is not specified, by default, nothing will be appended to -g . If debug is not turned on, this attribute will be ignored. | No |
optimize | Indicates whether source should be compiled with optimization; defaults to off . | No |
deprecation | Indicates whether source should be compiled with deprecation information; defaults to off . | No |
target | Generate class files for specific VM version (e.g., 1.1 or 1.2 ). Note that the default value depends on the JVM that is running Ant. In particular, if you use JDK 1.4+ the generated classes will not be usable for a 1.1 Java VM unless you explicitly set this attribute to the value 1.1 (which is the default value for JDK 1.1 to 1.3). We highly recommend to always specify this attribute. | No |
verbose | Asks the compiler for verbose output; defaults to no . | No |
depend | Enables dependency-tracking for compilers that support this (jikes and classic ). | No |
includeAntRuntime | Whether to include the Ant run-time libraries in the classpath; defaults to yes . | No |
includeJavaRuntime | Whether to include the default run-time libraries from the executing VM in the classpath; defaults to no . | No |
fork | Whether to execute javac using the JDK compiler externally; defaults to no . | No |
executable | Complete path to the javac executable to use in case of fork="yes" . Defaults to the compiler of the Java version that is currently running Ant. Ignored if fork="no" . Since Ant 1.6 this attribute can also be used to specify the path to the executable when using jikes, jvc, gcj or sj. | No |
memoryInitialSize | The initial size of the memory for the underlying VM, if javac is run externally; ignored otherwise. Defaults to the standard VM memory setting. (Examples: 83886080 , 81920k , or 80m ) | No |
memoryMaximumSize | The maximum size of the memory for the underlying VM, if javac is run externally; ignored otherwise. Defaults to the standard VM memory setting. (Examples: 83886080 , 81920k , or 80m ) | No |
failonerror | Indicates whether the build will continue even if there are compilation errors; defaults to true . | No |
source | Value of the -source command-line switch; will be ignored by all implementations prior to javac1.4 (or modern when Ant is not running in a 1.3 VM) and jikes . If you use this attribute together with jikes , you must make sure that your version of jikes supports the -source switch. By default, no -source argument will be used at all. Note that the default value depends on the JVM that is running Ant. We highly recommend to always specify this attribute. | No |
compiler | The compiler implementation to use. If this attribute is not set, the value of the build.compiler property, if set, will be used. Otherwise, the default compiler for the current VM will be used. (See the above list of valid compilers.) | No |
listfiles | Indicates whether the source files to be compiled will be listed; defaults to no . | No |
tempdir | Where Ant should place temporary files. This is only used if the task is forked and the command line args length exceeds 4k. Since Ant 1.6. | No; default is java.io.tmpdir. |
Parameters specified as nested elements
This task forms an implicit
FileSet and supports all attributes of
<fileset>
(
dir
becomes
srcdir
) as well as the nested
<include>
,
<exclude>
and
<patternset>
elements.
src
, classpath
, sourcepath
, bootclasspath
and extdirs
<javac>
's
srcdir
,
classpath
,
sourcepath
,
bootclasspath
, and
extdirs
attributes are
path-like structures and can also be set via nested
<src>
,
<classpath>
,
<sourcepath>
,
<bootclasspath>
and
<extdirs>
elements, respectively.
compilerarg
You can specify additional command line arguments for the compiler with nested
<compilerarg>
elements. These elements are specified like
Command-line Arguments but have an additional attribute that can be used to enable arguments only if a given compiler implementation will be used.
Attribute | Description | Required |
value | | Exactly one of these. |
line |
file |
path |
compiler | Only pass the specified argument if the chosen compiler implementation matches the value of this attribute. Legal values are the same as those in the above list of valid compilers.) | No |
Examples
<javac srcdir="${src}"
destdir="${build}"
classpath="xyz.jar"
debug="on"
source="1.4"
/>
compiles all .java
files under the ${src}
directory, and stores the .class
files in the ${build}
directory. The classpath used includes xyz.jar
, and compiling with debug information is on. The source level is 1.4, so you can use assert
statements.
<javac srcdir="${src}"
destdir="${build}"
fork="true"
source="1.2"
target="1.2"
/>
compiles all .java
files under the ${src}
directory, and stores the .class
files in the ${build}
directory. This will fork off the javac compiler using the default javac
executable. The source level is 1.2 (similar to 1.1 or 1.3) and the class files should be runnable under JDK 1.2+ as well.
<javac srcdir="${src}"
destdir="${build}"
fork="java$javac.exe"
source="1.5"
/>
compiles all .java
files under the ${src}
directory, and stores the .class
files in the ${build}
directory. This will fork off the javac compiler, using the executable named java$javac.exe
. Note that the $
sign needs to be escaped by a second one. The source level is 1.5, so you can use generics.
<javac srcdir="${src}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
compiles .java
files under the ${src}
directory, and stores the .class
files in the ${build}
directory. The classpath used includes xyz.jar
, and debug information is on. Only files under mypackage/p1
and mypackage/p2
are used. All files in and below the mypackage/p1/testpackage
directory are excluded from compilation. You didn't specify a source or target level, so the actual values used will depend on which JDK you ran Ant with.
<javac srcdir="${src}:${src2}"
destdir="${build}"
includes="mypackage/p1/**,mypackage/p2/**"
excludes="mypackage/p1/testpackage/**"
classpath="xyz.jar"
debug="on"
/>
is the same as the previous example, with the addition of a second source path, defined by the property src2
. This can also be represented using nested <src>
elements as follows:
<javac destdir="${build}"
classpath="xyz.jar"
debug="on">
<src path="${src}"/>
<src path="${src2}"/>
<include name="mypackage/p1/**"/>
<include name="mypackage/p2/**"/>
<exclude name="mypackage/p1/testpackage/**"/>
</javac>
If you want to run the javac compiler of a different JDK, you should tell Ant, where to find the compiler and which version of JDK you will be using so it can choose the correct command line switches. The following example executes a JDK 1.1 javac in a new process and uses the correct command line switches even when Ant is running in a Java VM of a different version:
<javac srcdir="${src}"
destdir="${build}"
fork="yes"
executable="/opt/java/jdk1.1/bin/javac"
compiler="javac1.1"
/>
Note: If you wish to compile only source files located in certain packages below a common root, use the
include
/
exclude
attributes or
<include>
/
<exclude>
nested elements to filter for these packages. Do not include part of your package structure in the
srcdir
attribute (or nested
<src>
elements), or Ant will recompile your source files every time you run your compile target. See the
Ant FAQ for additional information.
If you wish to compile only files explicitly specified and disable javac's default searching mechanism then you can unset the sourcepath attribute:
<javac sourcepath="" srcdir="${src}"
destdir="${build}" >
<include name="**/*.java"/>
<exclude name="**/Example.java"/>
</javac>
That way the javac will compile all java source files under "${src}" directory but skip the examples. The compiler will even produce errors if some of the non-example files refers to them.
Note: If you are using Ant on Windows and a new DOS window pops up for every use of an external compiler, this may be a problem of the JDK you are using. This problem may occur with all JDKs < 1.2.
Jikes Notes
You need Jikes 1.15 or later.
Jikes supports some extra options, which can be set be defining the properties shown below prior to invoking the task. The setting for each property will be in affect for all <javac>
tasks throughout the build. The Ant developers are aware that this is ugly and inflexible – expect a better solution in the future. All the options are boolean, and must be set to true
or yes
to be interpreted as anything other than false. By default, build.compiler.warnings
is true
, while all others are false
.
Property | Description | Default |
build.compiler.emacs | Enable emacs-compatible error messages. | false
|
build.compiler.fulldepend | Enable full dependency checking; see the +F switch in the Jikes manual. | false
|
build.compiler.pedantic | Enable pedantic warnings. | false
|
build.compiler.warnings Deprecated. Use <javac> 's nowarn attribute instead. | Don't disable warning messages. | true
|
Jvc Notes
Jvc will enable Microsoft extensions unless you set the property build.compiler.jvc.extensions
to false before invoking <javac>
.
<java>任务介绍:
Java
Description
Executes a Java class within the running (Ant) VM or forks another VM if specified.
If odd things go wrong when you run this task, set fork="true" to use a new JVM.
As of Ant 1.6.3, you can interact with a forked VM, as well as sending input to it via the input
and inputstring
attributes.
Parameters
Attribute | Description | Required |
classname | the Java class to execute. | Either jar or classname |
jar | the location of the jar file to execute (must have a Main-Class entry in the manifest). Fork must be set to true if this option is selected. | Either jar or classname |
args | the arguments for the class that is executed. deprecated, use nested <arg> elements instead. | No |
classpath | the classpath to use. | No |
classpathref | the classpath to use, given as reference to a PATH defined elsewhere. | No |
fork | if enabled triggers the class execution in another VM (disabled by default) | No |
spawn | if enabled allows to start a process which will outlive ant. Requires fork=true, and not compatible with timeout, input, output, error, result attributes. (disabled by default) | No |
jvm | the command used to invoke the Java Virtual Machine, default is 'java'. The command is resolved by java.lang.Runtime.exec(). Ignored if fork is disabled. | No |
jvmargs | the arguments to pass to the forked VM (ignored if fork is disabled). deprecated, use nested <jvmarg> elements instead. | No |
maxmemory | Max amount of memory to allocate to the forked VM (ignored if fork is disabled) | No |
failonerror | Stop the buildprocess if the command exits with a returncode other than 0. Default is "false" (see note) | No |
resultproperty | The name of a property in which the return code of the command should be stored. Only of interest if failonerror=false and if fork=true. | No |
dir | The directory to invoke the VM in. (ignored if fork is disabled) | 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. | 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. | 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. | No |
input | A file from which the executed command's standard input is taken. This attribute is mutually exclusive with the inputstring attribute | No; default is to take standard input from console (unless spawn="true" ) |
inputstring | A string which serves as the input stream for the executed command. This attribute is mutually exclusive with the input attribute. | No; default is to take standard input from console (unless spawn="true" ) |
newenvironment | Do not propagate old environment when new environment variables are specified. Default is "false" (ignored if fork is disabled). | No |
timeout | Stop the command if it doesn't finish within the specified time (given in milliseconds). It is highly recommended to use this feature only if fork is enabled. | No |
Parameters specified as nested elements
arg and jvmarg
Use nested
<arg>
and
<jvmarg>
elements to specify arguments for the Java class and the forked VM respectively. See
Command line arguments.
sysproperty
Use nested
<sysproperty>
elements to specify system properties required by the class. These properties will be made available to the VM during the execution of the class (either ANT's VM or the forked VM). The attributes for this element are the same as for
environment variables.
syspropertyset
You can specify a set of properties to be used as system properties with
syspropertysets.
since Ant 1.6.
classpath
Java
's
classpath attribute is a
PATH like structure and can also be set via a nested
classpath element.
bootclasspath
The location of bootstrap class files can be specified using this
PATH like structure - will be ignored if
fork is not
true
or the target VM doesn't support it (i.e. Java 1.1).
since Ant 1.6.
env
It is possible to specify environment variables to pass to the forked VM via nested
env elements. See the description in the section about
execSettings will be ignored if fork is disabled.
permissions
Security permissions can be revoked and granted during the execution of the class via a nested
permissions element. For more information please see
permissionsWhen the permission RuntimePermission exitVM has not been granted (or has been revoked) the System.exit() call will be intercepted and treated like indicated in failonerror.
Note: if you specify failonerror="true"
and you do not specify permissions, a set of default permissions will be added to your Java invocation to make sure that a non-zero return code will lead to a BuildException
.
Settings will be ignored if fork is enabled.
since Ant 1.6.
assertions
You can control enablement of Java 1.4 assertions with an
<assertions> subelement.
Assertion statements are currently ignored in non-forked mode.
since Ant 1.6.
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 <java> 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 <java>
is ignored. Alternatively, you can set resultproperty
to the name of a property and have it assigned to the result code (barring immutability, of course). When you set failonerror="true"
, the only possible value for resultproperty
is 0. Any non zero response is treated as an error and would mean the build exits.
Similarly, if failonerror="false"
and fork="false"
, then <java>
must return 0 otherwise the build will exit, as the class was run by the build jvm.
Examples
<java classname="test.Main">
<arg value="-h"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
Run a class in this JVM with a new jar on the classpath
<java jar="dist/test.jar"
fork="true"
failonerror="true"
maxmemory="128m"
>
<arg value="-h"/>
<classpath>
<pathelement location="dist/test.jar"/>
<pathelement path="${java.class.path}"/>
</classpath>
</java>
Run the jar using the manifest supplied entry point, forking (as required), and with a maximum memory of 128MB. Any non zero return code breaks the build.
<java classname="test.Main"/>
<java classname="test.Main"
fork="yes" >
<sysproperty key="DEBUG" value="true"/>
<arg value="-h"/>
<jvmarg value="-Xrunhprof:cpu=samples,file=log.txt,depth=3"/>
</java>
Note: you can not specify the (highly deprecated) MSJVM, "jview.exe" as the JVM, as it takes different parameters for other JVMs, That JVM can be started from <exec>
if required.
Exec
Description
Executes a system command. When the os attribute is specified, then the command is only executed when 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 in 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.
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.
Cygwin Users
In general 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 Windows executable and is not aware of Cygwin conventions.
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 occured (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
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 |
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 |
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 nested, 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
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. | 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 in include\error.h in your windows compiler or wine files; 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:/Programme/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 let be open.
<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: Although it may work for you to specify arguments using a simple arg-element and separate them by spaces it may fail if you switch to a newer version of the JDK. JDK < 1.2 will pass these as separate arguments to the program you are calling, JDK >= 1.2 will pass them as a single argument and cause most calls to fail.
Note2: If you are using Ant on Windows and a new DOS-Window pops up for every command which is executed this may be a problem of the JDK you are using. This problem may occur with all JDK's < 1.2.
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.
posted on 2007-03-22 09:43
我爱佳娃 阅读(3281)
评论(0) 编辑 收藏 所属分类:
JAVA基础