unpacking build.gradle and gradlew: a guide to building projects ⚒️🚧

torchictorchic
2 min read

have you ever opened up a new project and wondered what is the mysterious build.gradle file and gradle folder? what do they actually do, and why are they important in projects?

heres’s a quick guide!


“back in my day”

before gradle, setting up a Java project meant handling each step manually

  • compiling - you had to compile each file with javac
javac MyClass.java
  • downloading libraries - you had to download the .jar file
javac -cp library.jar MyClass.java
  • running tests - required manual setup and commands
java ~/junit.jar org.junit.runner.JUnitCore com.MyClass
  • packaging into JAR file - manually with the jar command
jar cvf Class.jar -C build/classes

time consuming right…?


what is build.gradle?

  • this file contains instructions and tells Gradle exactly how to build your project
# a basic build.gradle file
plugins {
    id 'java'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web:2.5.4'
}

tasks.named('test') {
    useJUnitPlatform()
}
  • dependencies - external libraries your code needs to function

    • we are adding a library that helps build web applications (spring boot)
  • plugins - tools to add features like java support/testing capabilities

    • using the java plugin to work with java code
  • build tasks - steps to compile, test, package the code

    • configures our test tasks to use junit testing framework

what is gradlew?

  • if build.gradle is the instructions for what to do, gradlew is the universal remote control

  • ensures that everyone builds the project the same way

  • how is it helpful?

    • wrapper script to ensure everyone is using the same version of Gradle

    • gradlew downloads and runs the correct version for the project

    • can automate tasks like compiling, testing, packaging in one line


how build.gradle and gradlew work together 🤝

  1. run gradlew: run a command like ./gradlew build, which triggers the Gradle build process

  2. gradlew looks at build.gradle: Gradle checks build.gradle to follow the instructions

  3. building begins! Gradle compiles your code, runs tests and packages everything


takeaways 🥡

  • build.gradle: project’s recipe - listing dependencies, plugins, tasks needed to build the code

  • gradlew: universal remote control - builds project the same way for everyone

they’re like the backstage crew that makes sure everything’s ready, so that You can focus on the main act - coding

0
Subscribe to my newsletter

Read articles from torchic directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

torchic
torchic