1

如何使用 Ant 构建脚本检查 root 权限?我试着用 shellscript 任务来做,比如

<shellscript shell="bash">
    if [[ `whoami` != 'root' ]]; then
        echo "You need to be root to install ooplss";
        exit 1
    fi
</shellscript>

但这不会停止脚本的执行。

还有其他想法吗?

4

3 回答 3

4

shellscript任务是exec任务的扩展。如果脚本失败,您应该能够指定failonerror使构建过程失败:

failonerror:如果命令退出并返回代码信号失败,则停止构建过程。默认为假。

<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>
于 2011-06-14T18:32:28.507 回答
0

使用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>
于 2011-06-14T19:58:26.347 回答
0

您可以检查 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>上面的

于 2011-06-14T18:40:34.410 回答