rogerxu / rogerxu.github.io

Roger Xu's Blog
2 stars 2 forks source link

Gradle #275

Open rogerxu opened 1 year ago

rogerxu commented 1 year ago

Gradle User Manual

rogerxu commented 1 year ago

Build

Lifecycle

Build Lifecycle (gradle.org)

A multi-project build must have a settings.gradle file in the root project of the multi-project hierarchy.

settings.gradle.kts

rootProject.name = "basic"
println("This is executed during the initialization phase.")

Build Script

Build Script Basics (gradle.org)

Gradle Plugins

Using Gradle Plugins

rogerxu commented 1 year ago

Maven

Migrating Builds From Apache Maven (gradle.org)

rogerxu commented 1 year ago

Java

Building Java Applications Sample (gradle.org)

Building Java & JVM projects (gradle.org)

build.gradle.kts

plugins {
    `java-library`
}

java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(11))
    }
}

version = "1.2.1"

The Java Library Plugin also integrates the above tasks into the standard Base Plugin lifecycle tasks:

Dependency Management

build.gradle.kts

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.hibernate:hibernate-core:3.6.7.Final")
}
rogerxu commented 1 year ago

Command Line

Command-Line Interface (gradle.org)

rogerxu commented 1 year ago

Multi-Project Build

Structuring and Building a Software Component with Gradle

Build Logic

Sharing Build Logic between Subprojects (gradle.org)

We recommend putting source code and tests for the convention plugins in the special buildSrc directory in the root directory of the project.

Using buildSrc to organize build logic

Sharing build logic between subprojects Sample (gradle.org)

Structuring Large Projects (gradle.org)

Structuring Software Projects Sample (gradle.org)

Composing builds (gradle.org)

rogerxu commented 1 year ago

JaCoCo

The JaCoCo Plugin (gradle.org)

build.gradle.kts

plugins {
    java,
    jacoco
}

tasks.jacocoTestReport {
    reports {
        xml.required.set(true)
        csv.required.set(true)
    }
}

tasks.test {
    finalizedBy(tasks.jacocoTestReport) // report is always generated after tests run
}

subprojects in the root project, legacy approach

subprojects {
    apply(plugin = "java")
    apply(plugin = "jacoco")

    val jacocoTestReport by tasks.existing(JacocoReport::class) {
        reports {
            xml.required.set(true)
            csv.required.set(true)
        }
    }

    val test by tasks.existing {
        finalizedBy(jacocoTestReport)
    }
}

Run command

$ ./gradlew check
rogerxu commented 1 year ago

SonarQube

SonarScanner for Gradle (sonarqube.org)

Gradle - Plugin: org.sonarqube

build.gradle.kts

plugins {
    id("org.sonarqube") version "3.5.0.2730"
}

sonarqube {
    properties {
        property("sonar.projectName", "Project Name")
        property("sonar.projectDescription", "Project description")
        property("sonar.links.ci", "https://cirrus-ci.com/github/SonarSource/sonar-kotlin")
        property("sonar.links.scm", "https://github.com/SonarSource/sonar-kotlin")
        property("sonar.links.issue", "https://jira.sonarsource.com/browse/SONARKT")
    }
}

subproject/build.gradle.kts

plugins {
    java
    jacoco
}

sonarqube {
    properties {
        property("sonar.coverage.exclusions", "**/*")
    }
}

Trigger sonar scan

$ ./gradlew check sonar