unpacking build.gradle and gradlew: a guide to building projects ⚒️🚧
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 controlensures 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 projectcan automate tasks like compiling, testing, packaging in one line
how build.gradle
and gradlew
work together 🤝
run
gradlew
: run a command like./gradlew build
, which triggers the Gradle build processgradlew
looks atbuild.gradle
: Gradle checks build.gradle to follow the instructionsbuilding begins! Gradle compiles your code, runs tests and packages everything
takeaways 🥡
build.gradle
: project’s recipe - listing dependencies, plugins, tasks needed to build the codegradlew
: 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
Subscribe to my newsletter
Read articles from torchic directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by