Getting Started with Gradle

A brief introduction to Gradle:

Gradle is an open source tool for project automation and build based on the concepts of Apache Ant and Apache Maven. It uses a domain-specific language (DSL) based on Groovy to declare project settings, and also adds a kotlin-based DSL based on the Kotlin language, abandoning various cumbersome configurations based on XML. Mainly for Java applications. It currently supports C++, Java, Groovy, Kotlin, Scala and Swift, and plans to support more languages ​​in the future.

Gradle is a JVM-based build tool. It is a universal and flexible build tool that supports maven and Ivy warehouses. It supports transitive dependency management without the need for remote warehouses or pom.xml and ivy.xml configuration files. Based on Groovy, The build script is written in Groovy.

Additionally, Gradle supports partial builds.

Official website: Gradle Build Tool

Why choose Gradle:  Gradle | Comparison of Gradle and Maven

Compatibility: Compatibility Matrix (gradle.org)

 Gradle installation: Gradle | Install (gradle.org)

Version selection: Gradle | Releases

After downloading, unzip the zip file and add it to the system environment variable. Test cmd: gradle

Use the Gradle you downloaded when building the project:

Gradle’s soul projects and tasks

Gradle's own domain objects mainly include Project and Task

Project provides the execution container and context for Task

Task Add custom task Add code in build.gradle.kts

task("sayHello") {
    println("hello Gradle")
}

The compiler performs tasks:

PS E:\kotlinTest\GradleTest> gradle sayHello

> Configure project :
hello Gradle

BUILD SUCCESSFUL in 1s
 

Gradle add dependency example:

 Load images from the network and save them locally

build.gradle.kts file:

    // https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
    implementation("com.squareup.okhttp3:okhttp:4.10.0")
//去哪里下载
repositories {
    mavenCentral()
}

dependencies {
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")

    // https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
    implementation("com.squareup.okhttp3:okhttp:4.10.0")

    implementation(kotlin("stdlib-jdk8"))

}

Code:

package org.example

import okhttp3.*
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream


fun main(args: Array<String>) {
    // 创建一个 OkHttpClient 实例
    val okHttpClient = OkHttpClient()
 
    // 使用 okHttpClient 发送请求...
    val request = Request.Builder()
        .url("https://pic.616pic.com/photoone/00/04/07/618ce5e7968227656.jpg")
        .build()

    okHttpClient.newCall(request).enqueue(object : Callback {
        override fun onFailure(call: Call, e: IOException) {
            // 请求失败处理...
        }

        override fun onResponse(call: Call, response: Response) {
            // 请求成功处理响应...

            val inputStream = response.body?.byteStream()
            if (inputStream != null) {
                // 创建输出文件
                // 调用函数将字节流转换为图片文件并保存到本地
                convertByteStreamToImageAndSave(inputStream, "demo.jpg")

                println("Image downloaded and saved to ")
            } else {
                println("Failed to get input stream")
            }


        }
    })
}


fun convertByteStreamToImageAndSave(byteStream: InputStream, outputPath: String) {
    // 创建输出文件
    val outputFile = File(outputPath)

    // 创建输出流
    val outputStream = FileOutputStream(outputFile)

    // 将字节流写入输出流
    byteStream.use { inputStream ->
        inputStream.copyTo(outputStream)
    }

    // 关闭输出流
    outputStream.close()

    println("Image saved to $outputPath")
}

Simple test case:

Gradle dependency conflict resolution and exclusion of dependencies

    // https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
    implementation("com.squareup.okhttp3:okhttp:4.10.0"){
        exclude("com.squareup.okhttp3","okhttp")
    }

Gradle plug-in custom extension Task types 

Reference documentTask - Gradle DSL Version 8.5       Task types 

 Delete files using Kotlin syntax Add code in build.gradle.kts

Delete usage example 

//Task 添加自定义任务 删除文件
task("myDeleteFile",Delete::class) {
    setDelete("src/temp")
}

Copy Usage Example 

//Task  拷贝文件
task("myCopyFile",Copy::class) {
    from("src")
    into("new")
}

Jar example 

//Task 添加自定义任务 拷贝文件
task("myJar",Jar::class) {
    from("src")
    into("abd.jar")
}

How to learn Gradle

GitHub - gradle/kotlin-dsl-samples: Sample builds using the Gradle Kotlin DSL

Guess you like

Origin blog.csdn.net/jiayou2020527/article/details/135092163