Running an executable from a different working directory using ant -
is possible run executable ant using working directory other 1 contains executable? small ant project demonstrates i'm trying achieve.
folder structure
ant test | build.xml | bin | | debug | | | program.exe | | release | | | program.exe | | inputs | | | ... | | outputs | | | ...
build.xml
<project name="test" basedir="./"> <target name="run"> <exec executable="${configuration}\program.exe" dir="bin"/> </target> </project>
${configuration}
set release
.
bin
needs working directory executable can reference files in inputs
, outputs
correctly, want able run executable contained in bin/release
bin
directory. however, ant fails find executable:
build failed d:\ant test\build.xml:6: execute failed: java.io.ioexception: cannot run program "release\program.exe" (in directory "d:\ant test\bin"): createprocess error=2, system cannot find file specified
i able work around on windows launching cmd.exe
in bin
directory , passing parameter release\program.exe
, need able use ant file on multiple platforms, hoping find consistent syntax.
one option considered using conditional tasks , having separate syntaxes windows , unix. however, in actual project, exec
statement occurs inside macro, , target calls macro large number of times. since conditions can affect things @ target level, have duplicate long list of macro calls each syntax wanted target, e.g.
<target name="runall" depends="runall-win,runall-unix"/> <target name"runall-win" if="win"> <mymacro-win .../> <!-- huge list of macro calls --> ... <mymacro-win .../> </target> <target name"runall-unix" if="unix"> <mymacro-unix .../> <!-- huge list of macro calls --> ... <mymacro-unix .../> </target>
the dir
attribute specifies working directory executable path resolved against project basedir. hence task search executable d:\ant test\release\program.exe
.
a simple solution add resolveexecutable="true"
task call:
<target name="run"> <exec executable="${configuration}\program.exe" dir="bin" resolveexecutable="true" /> </target>
from exec
task documentation:
when attribute true, name of executable resolved firstly against project basedir , if not exist, against execution directory if specified. on unix systems, if want allow execution of commands in user's path, set false. since ant 1.6
Comments
Post a Comment