Search code examples
gradlescriptingoutputzsh

How can I suppress ALL output from gradle?


I'm writing a script for zsh that essentially just needs to know the exit code of the following gradle command: ./gradlew installDebug. In other words, I don't want any output to the console while running this. I've tried the following: ./gradlew installDebug -q > /dev/null; 2>&1

This eliminates 99% of the output, but I still get lines like the following:

Note: Generating a MembersInjector for com.example.ui.BaseActivity. Prefer to run the dagger processor over that class instead.
Note: Generating a MembersInjector for com.example.widget.nav.NavigationDrawerActivity. Prefer to run the dagger processor over that class instead.
Note: Generating a MembersInjector for com.example.fragment.ShiftListFragment. Prefer to run the dagger processor over that class instead.

These warnings are coming from lint, which, in general, I want to keep (for when I build manually, without this script). I simply want to suppress the output in this one specific case.

I feel like I'm simply not redirecting output for a given stream correctly.


Solution

  • As long as you are using zsh you can use

    ./gradlew installDebug -q &> /dev/null
    

    For POSIX compliant shells (including zsh) you can use

    ./gradlew installDebug -q > /dev/null 2>&1
    

    The reason it did not work is the semicolon. A ; separates two commands but redirections only work per command and not per command-line. So what actually happens is equivalent to the following:

    ./gradlew installDebug -q > /dev/null
    2>&1
    
    • In the first command the standard output of ./gradlew is redirected to /dev/null but standard error is not changed and thus printed normally.

    • The second is actually just a redirection without a command. In this case the behavior depends on the setting of the the parameter NULLCMD and the options SH_NULLCMD and CSH_NULLCMD. By default zsh runs cat with the given redirection applied.