Java
Beginner
1 min read
Gradle Basics and build.gradle
Example
// build.gradle (Groovy DSL)
plugins {
id 'java'
id 'application'
}
group = 'com.example'
version = '1.0.0'
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
repositories {
mavenCentral()
}
dependencies {
// implementation: available at compile + runtime, NOT exposed to consumers
implementation 'com.google.guava:guava:32.1.2-jre'
// compileOnly: only at compile time (e.g. Lombok, annotation processors)
compileOnly 'org.projectlombok:lombok:1.18.30'
annotationProcessor 'org.projectlombok:lombok:1.18.30'
// testImplementation: only in test source set
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
application {
mainClass = 'com.example.Main'
}
test {
useJUnitPlatform() // required for JUnit 5
}
// Custom task
tasks.register('printVersion') {
doLast {
println "Building version: $version"
}
}