问题描述
|
如何使用Ant构建脚本检查root特权?我尝试用像
<shellscript shell=\"bash\">
if [[ `whoami` != \'root\' ]]; then
echo \"You need to be root to install ooplss\";
exit 1
fi
</shellscript>
但这不会停止脚本的执行。
还有其他想法吗?
解决方法
shellscript任务是exec任务的扩展。如果脚本失败,您应该能够指定ѭ1来使构建过程失败:
failonerror:如果发生以下情况,则停止构建过程
该命令以返回码退出
信号故障。默认为false。
<shellscript shell=\"bash\" failonerror=\"true\">
if [[ `whoami` != \'root\' ]]; then
echo \"You need to be root to install ooplss\";
exit 1
fi
</shellscript>
但是,没有shell脚本应该可以做到;以下未经测试:
<fail message=\"You need to be root to install ooplss\">
<condition>
<not>
<equals arg1=\"root\" arg2=\"${user.name}\"/>
</not>
</condition>
</fail>
, 您可以检查java属性user.name
:
<target name=\"checkroot\">
<condition property=\"isroot\">
<equals arg1=\"${os.user}\" arg2=\"root\"/>
</condition>
</target>
<target name=\"dostuff\" depends=\"checkroot\" unless=\"isroot\">
...
</target>
从ant 1.7开始,您还可以使用<scriptcondition>
在脚本中做一些聪明的事情,而不是上面的<equals>
, 使用Ant Plugin Flaka的直接方法=
<project xmlns:fl=\"antlib:it.haefelinger.flaka\">
<fl:when test=\" \'${user.name}\'.toupper eq \'ROOT\' \">
<!-- your tasks go here.. -->
</fl:when>
</project>