Explain Codes LogoExplain Codes Logo

How to run Gradle from the command line on Mac bash

java
gradle
bash
command-line
Alex KataevbyAlex Kataev·Dec 8, 2024
TLDR

For a quick start, use the command ./gradlew build in the root directory of a project that uses the Gradle Wrapper. Remember to give gradlew permission to execute via chmod +x gradlew. For those using a direct Gradle installation, gradle build should do the trick, provided gradle is in your PATH.

# If you're running with Gradle Wrapper (most likely you): ./gradlew build # Because wrapper knows best # If you're running with a direct Gradle installation gradle build # Ain't no wrapper like an unwrapped Gradle!

Ensure your CLI is at your rooting (pun intended) for your project's root, where the build.gradle file is. Move there with cd /path/to/project if needed.

Verifying project setup

Before showing off your Gradle command skills, check your project setup:

  • gradlew should be partying in your project root. Check that!

  • Visit ls -l gradlew to check if gradlew has its party hat on aka the x permission. If missing, gift it with chmod +x gradlew.

  • Add the path to gradlew to your PATH for handy access:

    export PATH="$PATH:`pwd`/gradlew" # Making the wrapper feel at home in PATH family
  • Run gradle -v to check Gradle's ID and confirm it indeed is who it claims.

When Gradle grumbles: Troubleshooting

Run into issues? Fret not, here're your Gradle Issue Goggles:

  • Seeing a "gradlew not found"? Confirm your directory or PATH has gradlew invited.
  • File permissions often play spoilsport. Ensure gradlew has its party hat (x permission) with chmod +x gradlew.
  • If issues still party on, your project's documentation or community forums might be the party pooper.
  • To check for freebies (tests), run:
    ./gradlew test # Anyone with "test" in their name gotta be free! Pay back in votes, maybe?
  • Tinkering with Android Studio or an Android project? Navigate to the party spot usually named android.

Customizing Gradle runs

Like how you prefer your coffee, here's how to tweak your Gradle commands to your liking:

  • Append --scan to your commands for a detailed brewing report.
  • Unveil all tasks prepared for the party with gradle tasks.
  • Bump into nasty environment variables or aliases? Check your shell's guest list (/.bashrc or /.bash_profile).

Gradle via Homebrew

If you prefer your Gradle installation straight-up (via Homebrew), do:

  • Install Gradle with:
    undefined

brew install gradle # Brew a pot of Gradle

- Post Homebrew installation, you can run Gradle commands **globally**. Handy right?