
How to Run Executable & Native Programs in an Ant Script
Apache Ant allows you to run executable files (.exe
) and native programs using the <exec>
task. This guide explains how to:
β
Execute an external program or script
β
Pass arguments to the program
β
Handle output and errors
β
Run Windows executables (.exe
) and Unix shell scripts (.sh
)
1. Running an Executable (.exe
) in Ant Script
If you have a Windows executable (e.g., myprogram.exe
) and want to run it using Ant, use the <exec>
task.
Example: Run an EXE File
<project name="RunExeExample" default="runExe" basedir=".">
<target name="runExe">
<exec executable="C:\path\to\myprogram.exe">
<arg value="--option1"/>
<arg value="value1"/>
</exec>
</target>
</project>
β This will run:
C:\path\to\myprogram.exe --option1 value1
Key Points:
- Replace
C:\path\to\myprogram.exe
with the actual path of your executable. - Use
<arg>
to pass arguments to the executable.
2. Running a Native Program (Any OS)
You can run system commands (e.g., cmd.exe
for Windows or sh
for Unix).
Example: Running a Shell Script (.sh
)
<target name="runShell">
<exec executable="sh">
<arg value="-c"/>
<arg value="ls -la"/>
</exec>
</target>
β This runs the command:
sh -c "ls -la"
Example: Running a Windows Command (cmd.exe
)
<target name="runCmd">
<exec executable="cmd.exe">
<arg value="/c"/>
<arg value="dir"/>
</exec>
</target>
β This runs:
cmd.exe /c "dir"
(Lists directory contents on Windows.)
3. Capturing Output and Logging
You can capture output from the executable into a file or display it in the Ant log.
Example: Redirect Output to a Log File
<target name="runExeWithLog">
<exec executable="C:\path\to\myprogram.exe" output="output.log" error="error.log"/>
</target>
β
This saves standard output to output.log
and errors to error.log
.
4. Setting Environment Variables
You may need to set environment variables before running an executable.
Example: Setting an Environment Variable
<target name="runWithEnv">
<exec executable="myprogram.exe">
<env key="MY_ENV_VAR" value="some_value"/>
</exec>
</target>
β
This sets MY_ENV_VAR=some_value
before running myprogram.exe
.
5. Running Background Processes
To run a program without waiting for it to finish, set spawn="true"
.
<target name="runInBackground">
<exec executable="C:\path\to\myprogram.exe" spawn="true"/>
</target>
β
This starts myprogram.exe
and allows Ant to continue execution.
6. Full Example: Running an EXE, Capturing Output, and Setting Env Variables
<project name="FullExample" default="runExe" basedir=".">
<target name="runExe">
<exec executable="C:\path\to\myprogram.exe" output="program_output.log" error="program_error.log">
<env key="CONFIG_PATH" value="C:\configs"/>
<arg value="--config"/>
<arg value="config.json"/>
</exec>
</target>
</project>
β This:
- Runs
myprogram.exe --config config.json
- Captures output in
program_output.log
and errors inprogram_error.log
- Sets the environment variable
CONFIG_PATH=C:\configs
7. Conclusion
With Antβs <exec>
task, you can execute native programs, EXE files, and shell scripts, pass arguments, capture output, and set environment variables. π
Would you like help automating builds, integrating with Java, or scheduling tasks in Ant? Let me know! π
Apache Ant <exec>
Task Parameters Table
The <exec>
task in Apache Ant is used to run external programs, executables, scripts, or system commands. The following table lists all the available parameters for the <exec>
task along with their descriptions and example usage.
π Table: <exec>
Task Parameters
Parameter | Description | Required | Example Usage |
---|---|---|---|
executable | The program or command to execute (full path if necessary) | β Yes | <exec executable="cmd.exe"/> |
dir | Specifies the working directory for the command execution | β No | <exec executable="myprogram.exe" dir="C:\mydir"/> |
os | Restricts execution to specific OS (comma-separated) | β No | <exec executable="ls" os="Linux,MacOS"/> |
failonerror | If true , the build fails on non-zero exit codes | β No (default: false ) | <exec executable="myprogram.exe" failonerror="true"/> |
spawn | If true , runs the process in the background | β No (default: false ) | <exec executable="notepad.exe" spawn="true"/> |
output | Captures the programβs standard output to a file | β No | <exec executable="myprogram.exe" output="output.log"/> |
error | Captures the programβs error output to a file | β No | <exec executable="myprogram.exe" error="error.log"/> |
input | Redirects a file as standard input for the process | β No | <exec executable="myprogram.exe" input="input.txt"/> |
logerror | If true , logs error messages to the Ant console | β No (default: false ) | <exec executable="myprogram.exe" logerror="true"/> |
resultproperty | Stores the exit code of the command in a property | β No | <exec executable="myprogram.exe" resultproperty="exit.code"/> |
timeout | Sets a timeout (in milliseconds) for execution | β No | <exec executable="myprogram.exe" timeout="5000"/> |
π Special Nested Elements in <exec>
Element | Description | Example Usage |
---|---|---|
<arg> | Passes command-line arguments | <arg value="--config config.json"/> |
<env> | Sets environment variables for execution | <env key="JAVA_HOME" value="C:\Java"/> |
π Example: Full <exec>
Task Usage
<project name="RunExecutable" default="execute" basedir=".">
<target name="execute">
<exec executable="C:\path\to\myprogram.exe" dir="C:\mydir" failonerror="true" timeout="10000" output="output.log" error="error.log">
<arg value="--config"/>
<arg value="config.json"/>
<env key="CONFIG_PATH" value="C:\configs"/>
</exec>
</target>
</project>
β This script:
- Runs
myprogram.exe --config config.json
inC:\mydir
- Stops execution if an error occurs (
failonerror="true"
) - Captures standard output (
output.log
) and errors (error.log
) - Uses an environment variable
CONFIG_PATH=C:\configs
π― Conclusion
The <exec>
task in Ant is highly flexible and allows running external programs with environment control, error handling, and logging. By using parameters and nested elements effectively, you can automate complex tasks seamlessly. π
Would you like additional help with Ant automation, Java builds, or integrating with Jenkins? Let me know! π
Iβm a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.Β
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND