Question

How can I check for root privileges with an Ant build script? I tried doing it with the shellscript task like

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

But this won't stop the execution of the script.

Any other ideas?

Was it helpful?

Solution

The shellscript task is an extension of the exec task. You should be able to specify failonerror to make the build process fail if the script fails:

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>

It should however be possible to do without a shell script; the following is untested:

<fail message="You need to be root to install ooplss">
 <condition>
   <not>
     <equals arg1="root" arg2="${user.name}"/>
   </not>
 </condition>
</fail>

OTHER TIPS

You can check the java property user.name:

<target name="checkroot">
  <condition property="isroot">
    <equals arg1="${os.user}" arg2="root"/>
  </condition>
</target>
<target name="dostuff" depends="checkroot" unless="isroot">
...
</target>

Since ant 1.7 you can also use <scriptcondition> to do something clever in a script instead of the <equals> above

a straight approach with 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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top