Gralde is developed at 2007. Gradle build scripts are written using Groovy or Kotlin.
- Build
- Dependency management
- Faster performance and simpler syntax than Maven
Gradle support caching and skipping the tasks when change doesn't occur.
- Initialization
- Configuration
Construct Directed Acyclic Graph(DAG) internally. - Execution
$ java --version
Install from this link
$ sdk install gradle 7.6
$ brew install gradle
$ gradle --version
$ gradle build
$ gradle build -x test # exclude test
# Print all tasks
$ gradle tasks
$ gradle properties
# Deletes the build folder and lets you start fresh
$ gradle clean build
This allows us to use the same gradle version anywhere we build.
Just contain script that runs on different OS
Support multiple project (multi module)
repositories {
mavenCentral() // find here at first
google() // find here at second
}
# Show dependency quietly
$ gradle -q dependencies
org.junit.jupiter:junit-jupiter-engine:5.9.0 has transitive dependencies as shown below
\--- org.junit.jupiter:junit-jupiter-engine:5.9.0
+--- org.junit:junit-bom:5.9.0
| +--- org.junit.jupiter:junit-jupiter-api:5.9.0 (c)
| +--- org.junit.jupiter:junit-jupiter-engine:5.9.0 (c)
| +--- org.junit.platform:junit-platform-engine:1.9.0 (c)
| \--- org.junit.platform:junit-platform-commons:1.9.0 (c)
+--- org.junit.platform:junit-platform-engine:1.9.0
| +--- org.junit:junit-bom:5.9.0 (*)
| +--- org.opentest4j:opentest4j:1.2.0
| \--- org.junit.platform:junit-platform-commons:1.9.0
| \--- org.junit:junit-bom:5.9.0 (*)
\--- org.junit.jupiter:junit-jupiter-api:5.9.0
+--- org.junit:junit-bom:5.9.0 (*)
+--- org.opentest4j:opentest4j:1.2.0
\--- org.junit.platform:junit-platform-commons:1.9.0 (*)
// This must be placed before `plugin`
buildscript {
ext {
jsoupVersion = '1.15.3'
}
}
// ..
dependencies {
// group-id:artifact-id:version
implementation "org.jsoup:jsoup:${jsoupVersion}"
}
Gradle stores the cache files at C:\Users{USER_NAME}.gradle\caches{GRADLE_VERSION}.
- If build is executed, gradle would store the cache files
- when building, Check dependency checking in ~/.gradle/caches if already exsists
- If exists, not install it
Not exists, install and refresh the cache.
Notify where the source files to gradle
sourceSets {
// Contains production source code of the project,
// Which is compiled and assembled into a JAR
// `main` is the name of source set, This is id (plugins: id 'java')
main {
java { // configures the java source this source set
srcDir 'src'
}
}
}
Faster compilation time due to fewer dependency path
dependencies {
implementation "org.jsoup:jsoup:${jsoupVersion}"
// junit is only needed to test -> compile or runtime
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
}
If I set jsoup as compileOnly
, main can't run it since it's just used to compiled not on runtime
dependencies {
compileOnly "org.jsoup:jsoup:${jsoupVersion}"
}
public class Practice {
public static void main(String[] args) {
// ⚠️ Can't execute this
// Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
Jsoup.connect("http://www.infomoney.com.br/mercados/bitcoin").ignoreContentType(true).execute().body();
}
}
if runtimeOnly
set,
error: package org.jsoup does not exist
import org.jsoup.Jsoup;
Use implementation
for required compile and running both.