【发布时间】:2025-12-05 17:45:02
【问题描述】:
是否可以使用包含可执行文件的工作目录以外的工作目录从 ant 运行可执行文件?这个小蚂蚁项目展示了我正在努力实现的目标。
文件夹结构
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} 设置为release。
bin 需要是工作目录,这样可执行文件才能正确引用inputs 和outputs 中的文件,所以我希望能够从bin 目录运行bin/release 中包含的可执行文件。但是,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
我可以通过在bin 目录中启动cmd.exe 然后将参数release\program.exe 传递给它来在Windows 上解决这个问题,但是我需要能够在多个平台上使用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>
【问题讨论】:
标签: ant