在 bin 目录 ant 中创建一个可执行脚本

Make a script executable in bin directory ant

这是我的 ant 脚本,它希望将脚本从 zip 文件转换为可执行文件。这是我所做的:

<chmod dir="tmp/temp/test/bin" perm="ugo+rx" includes="**/*"  />

<echo message="Making scripts exec: tmp/temp/test/bin" />

<exec executable="/bin/bash">
    <arg value="tmp/temp/test/bin/pkg"/>
    <arg value="validate"/>
</exec>

这不会使脚本可执行,而是给出此错误:

 Execute failed: java.io.IOException: Cannot run program "D:\tmp\temp\test\bin": CreateProcess error=5, Access is denied.

Cannot run program "D:\tmp\temp\test\bin": CreateProcess error=5, Access is denied.

你运行在Windows或Linux/Unix/MacOS下使用这个吗?

Windows在文件中没有可执行性的概念。相反,它将文件后缀与可执行程序相关联。例如,我可以将 .py 后缀关联到 python.exe 程序。打开 foo.py 将 运行 Python 脚本下的 Python 可执行文件。将名称更改为 foo.pl,脚本将不会 运行,或者如果您已将 .plperl.exe 相关联,则 运行 下的脚本将Perl.

您可以将脚本的后缀添加到 %PATHEXT% 环境变量,这将允许您键入没有扩展名的文件名。例如,如果我将 .py 添加到 %PATHEXT%,然后在命令行终端中输入 foo,Windows 可能会查找 foo.py 然后执行它. (除非它首先找到 foo.exefoo.bat。然后那些将执行)。

<chmod> 任务在 Windows 下不执行任何操作。在 <chmod> 手册页中,它指出:

Changes the permissions of a file or all files inside specified directories. Right now it has effect only under Unix or NonStop Kernel (Tandem).

此外,并非所有 Zip 实现都可以存储文件权限和所有权信息——尤其是 Unix 风格的权限和所有权。有些可以,有些则不能。即使你想以某种方式表达文件对 Unix 系统的可执行性,它也可能行不通。

我建议在脚本中包含一个 <condition> 测试来测试 OS。然后,您可以将 Linux/Windows/MacOS 系统上的操作与 Windows 系统上的操作分开。