2011-06-14 78 views
1

如何使用Ant構建腳本檢查root權限?我試圖用類似Ant Build-Script如何檢查root權限

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

這樣的腳本執行它,但是這不會停止腳本的執行。

還有其他想法嗎?

回答

4

shellscript任務是exec任務的擴展。你應該能夠指定failonerror使構建過程失敗,如果腳本失敗:

failonerror: Stop the buildprocess if the command exits with a return code signaling failure. Defaults to 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> 
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> 

由於螞蟻1.7,你也可以使用<scriptcondition>做一些聰明的腳本,而不是<equals>上述

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>