使用 ant 从不同的工作目录运行可执行文件

Ope*_*tle 2 ant

是否可以使用包含可执行文件的工作目录以外的工作目录从 ant 运行可执行文件?这个小蚂蚁项目展示了我想要实现的目标。

文件夹结构

ant test
|   build.xml
|   bin
|   |   debug
|   |   |    program.exe
|   |   release
|   |   |    program.exe
|   |   inputs
|   |   |    ...
|   |   outputs
|   |   |    ...
Run Code Online (Sandbox Code Playgroud)

构建文件

<project name="test" basedir="./">    
    <target name="run">
        <exec executable="${configuration}\program.exe" dir="bin"/> 
    </target>
</project>
Run Code Online (Sandbox Code Playgroud)

${configuration}设置为release

bin需要在工作目录,以便在可执行文件的参考文件inputsoutputs正确的,所以我希望能够运行包含在可执行文件bin/releasebin目录。但是,ant 找不到可执行文件:

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, The system cannot find the file specified
Run Code Online (Sandbox Code Playgroud)

我可以通过cmd.exebin目录中启动然后将参数传递给 Windows 来解决这个问题release\program.exe,但是我需要能够在多个平台上使用 ant 文件,所以我希望找到一致的语法。

我考虑的一种选择是使用条件任务并为 windows 和 unix 使用单独的语法。但是,在实际项目中,exec语句出现在宏内部,目标调用宏的次数较多。由于条件只能影响目标级别的事物,因此我必须为我想要定位的每个语法复制一长串宏调用,例如

<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>
Run Code Online (Sandbox Code Playgroud)

man*_*uti 5

The dir attribute specifies the working directory but the executable path is resolved against the project basedir. Hence the task will search for the executable D:\ant test\release\program.exe.

A simple solution is to add resolveexecutable="true" to the task call:

<target name="run">
    <exec executable="${configuration}\program.exe" dir="bin" resolveexecutable="true" /> 
</target>
Run Code Online (Sandbox Code Playgroud)

From the exec task documentation:

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