## Apply/ExecOn

The name `execon` is deprecated and only kept for backwards compatibility.

### 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.

The files and/or directories of a number of Resource Collections – including but not restricted to FileSets, DirSets (since Ant 1.6) or FileLists (since Ant 1.6) – are passed as arguments to the system command.

If you specify a nested mapper, the timestamp of each source file is compared to the timestamp of a target file which is defined by the nested mapper element and searched for in the given dest, if specified.

At least one fileset or filelist is required, and you must not specify more than one mapper.

Note that you cannot interact with the forked program, the only way to send input to it is via the input and inputstring attributes.

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

If you run Ant as a background process (like `ant &`) and use the `<apply>` 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
executable | the command to execute without any command line arguments. | Yes
dest | the directory where the command is expected to place target files when it is executed. This attribute is valid only when used in conjunction with a nested mapper; if omitted, the target filenames returned by the mapper will be interpreted as absolute paths. | No
spawn | whether or not you want the commands to be spawned. 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, default is false
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.
relative | whether the filenames should be passed on the command line as relative pathnames (relative to the base directory of the corresponding fileset/list for source files or the dest attribute for target files). | No, default is false
forwardslash | whether the file names should be passed with forward slashes even if the operating system requires other file separator. The option is ignored if the system file separator is a forward slash. | No, default is false
os | list of Operating Systems on which the command may be executed. | No
osfamily | OS family as used in the <os> condition. since Ant 1.7 | No
output | the file to which the output of the command should be redirected. 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 should be appended to or overwrite an existing file. If you set parallel to false, you will probably want to set this one to true. | No, default is false
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. If you set parallel to false, only the result of the first execution will be stored. | 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 returncode other than 0. | No
failifexecutionfails | Stop the build if we can't start the program. Defaults to true. | No
skipemptyfilesets | Don't run the command, if no source files have been found or are newer than their corresponding target files. Despite its name, this attribute applies to filelists as well. | No, default is false
parallel | Run the command only once, appending all files as arguments. If false, command will be executed once for every file. | No, default is false
type | One of file, dir or both. If set to file, only the names of plain files will be sent to the command. If set to dir, only the names of directories are considered. Note: The type attribute does not apply to nested dirsets - dirsets always implicitly assume type to be dir. | No, default is file
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 if 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
maxparallel | Limit the amount of parallelism by passing at most this many sourcefiles at once. Set it to <= 0 for unlimited. Since Ant 1.6. | No, unlimited by default
addsourcefile | Whether source file names should be added to the command automatically. Since Ant 1.6. | No, default is true
verbose | Whether to print a summary after execution or not. Since Ant 1.6. | No, default false
ignoremissing | Whether to ignore nonexistent files specified via filelists. Since Ant 1.6.2. | No, default is true
force | Whether to bypass timestamp comparisons for target files. Since Ant 1.6.3. | No, default is false

### Parameters specified as nested elements

#### fileset

You can use any number of nested `<fileset>` elements to define the files for this task and refer to `<fileset>`s defined elsewhere.

#### filelist

Since Ant 1.6

You can use any number of nested `<filelist>` elements to define the files for this task and refer to `<filelist>`s defined elsewhere.

#### dirset

Since Ant 1.6

You can use any number of nested `<dirset>` elements to define the directories for this task and refer to `<dirset>`s defined elsewhere.

#### Any other Resource Collection

since Ant 1.7

You can use any number of nested resource collections.

#### mapper

A single `<mapper>` specifies the target files relative to the `dest` attribute for dependency checking. If the `dest` attribute is specified it will be used as a base directory for resolving relative pathnames returned by the mapper. At least one `<fileset>` or `<filelist>` is required.

#### arg

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

#### srcfile

By default the file names of the source files will be added to the end of the command line (unless you set addsourcefile to `false`). If you need to place it somewhere different, use a nested `<srcfile>` element between your `<arg>` elements to mark the insertion point.

Attribute | Description | Required
prefix | a prefix to place in front of the file name when building the command line argument. Since Ant 1.8.0 | No.
suffix | a suffix to append to the file name when building the command line argument. Since Ant 1.8.0 | No.

#### targetfile

`<targetfile>` is similar to `<srcfile>` and marks the position of the target filename on the command line. If omitted, the target filenames will not be added to the command line at all. This element can only be specified if you also define a nested mapper.

Attribute | Description | Required
prefix | a prefix to place in front of the file name when building the command line argument. Since Ant 1.8.0 | No.
suffix | a suffix to append to the file name when building the command line argument. Since Ant 1.8.0 | No.

#### env

It is possible to specify environment variables to pass to the system command via nested `<env>` elements. See the description in the section about exec

#### redirector

Since Ant 1.6.2

A nested I/O Redirector can be specified. <apply>'s behavior is like that of exec with regard to redirectors, with the exception that, in non-parallel mode, file mapping will take place with each iteration. This grants the user the capacity to receive input from, and send output to, different files for each sourcefile.

In parallel-mode the redirector will be reset for each batch of executions (with maxparallel > 0) and null will be used a source file just like it is in the case of `exec`.

### Examples

>     <apply executable="ls">
>       <arg value="-l"/>
>       <fileset dir="/tmp">
>         <patternset>
>           <exclude name="**/*.txt"/>
>         </patternset>
>       </fileset>
>       <fileset refid="other.files"/>
>     </apply>

invokes `ls -l`, adding the absolute filenames of all files below `/tmp` not ending in `.txt` and all files of the FileSet with `id` `other.files` to the command line.

>     <apply executable="somecommand" parallel="false">
>       <arg value="arg1"/>
>       <srcfile/>
>       <arg value="arg2"/>
>       <fileset dir="/tmp"/>
>     </apply>

invokes `somecommand arg1 SOURCEFILENAME arg2` for each file in `/tmp` replacing SOURCEFILENAME with the absolute filename of each file in turn. If `parallel` had been set to true, SOURCEFILENAME would be replaced with the absolute filenames of all files separated by spaces.

>     <apply executable="cc" dest="src/C" parallel="false">
>       <arg value="-c"/>
>       <arg value="-o"/>
>       <targetfile/>
>       <srcfile/>
>       <fileset dir="src/C" includes="*.c"/>
>       <mapper type="glob" from="*.c" to="*.o"/>
>     </apply>

invokes `cc -c -o TARGETFILE SOURCEFILE` for each `.c` file that is newer than the corresponding `.o`, replacing TARGETFILE with the absolute filename of the `.o` and SOURCEFILE with the absolute name of the `.c` file.

>     <mapper id="out" type="glob"
>                from="src${file.separator}*.file"
>                to="dest${file.separator}*.out"/>

>     <apply executable="processfile" dest="dest">
>       <fileset dir="src" includes="*.file"/>
>       <mapper refid="out"/>
>       <redirector>
>         <outputmapper refid="out"/>
>       </redirector>
>     </apply>

Applies the fictitious "processfile" executable to all files matching `*.file` in the `src` directory. The `out` `<mapper>` has been set up to map `*.file` to `*.out`, then this `<mapper>` is used to specify `targetfile`s for this <apply> task. A reference to `out` is then used as an `<outputmapper>` nested in a `<redirector>`, which in turn is nested beneath this `<apply>` instance. This allows us to perform dependency checking against output files--the target files in this case.

>     <apply executable="ls" parallel="true"
>            force="true" dest="${basedir}" append="true" type="both">
>       <path>
>         <pathelement path="${env.PATH}"/>
>       </path>
>       <identitymapper/>
>     </apply>

Applies the "ls" executable to all directories in the PATH, effectively listing all executables that are available on the PATH.

>     <apply executable="jsmin" addsourcefile="false">
>         <!-- Collect the JS-files -->
>         <fileset dir="src" includes="*.js"/>
>         <redirector>
>             <!-- redirect STDIN; fileset collects relative to its dir, but we need -->
>             <!-- relative to basedir -->
>             <inputmapper type="glob" from="*" to="src/*"/>
>             <!-- redirect STDOUT to file in dest-dir -->
>             <outputmapper id="out" type="glob" from="*.js" to="dest/*.js"/>
>         </redirector>
>     </apply>

Conversion of the command `jsmin < src/a.js > dest/a.js` but for all files in the src-directory. Because the filename itself should not be passed to the `jsmin` program, the `addsourcefile` is set to `false`.