/gradle-practice

How to use Gradle in right way

Primary LanguageJava

Introduction

Gralde is developed at 2007. Gradle build scripts are written using Groovy or Kotlin.

Why to use?

  • Build
  • Dependency management
  • Faster performance and simpler syntax than Maven
    Gradle support caching and skipping the tasks when change doesn't occur.

How to operate?

  1. Initialization
  2. Configuration
    Construct Directed Acyclic Graph(DAG) internally.
  3. Execution

Prerequisite

Install Java JDK

$ java --version

Install gradle manually

Windows

Install from this link

Unix

$ sdk install gradle 7.6

Mac

$ brew install gradle

Check version

$ gradle --version

Command

$ 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

Configuration build.gradle

gradle wrapper

This allows us to use the same gradle version anywhere we build.

gradlew(.bat)

Just contain script that runs on different OS

settings.gradle

Support multiple project (multi module)

Repositories

repositories {
    mavenCentral() // find here at first
    google() // find here at second
}

Transitive Dependency

# 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 (*)

Parameterization

// This must be placed before `plugin`
buildscript {
    ext {
        jsoupVersion = '1.15.3'
    }
}

// ..
dependencies {
    // group-id:artifact-id:version
    implementation "org.jsoup:jsoup:${jsoupVersion}"
}

Cache

Gradle stores the cache files at C:\Users{USER_NAME}.gradle\caches{GRADLE_VERSION}.

  1. If build is executed, gradle would store the cache files
  2. when building, Check dependency checking in ~/.gradle/caches if already exsists
  3. If exists, not install it
    Not exists, install and refresh the cache.

SourceSets

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'
        }
    }
}

What's the Implement, compileOnly, testImplement?

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.

What's the sequence of tasks? How to customize it? (ex. annotation)