Gradle-- create a simple project

Project & task

Everything Gradle are based on projects and tasks.

Constructed from one or multiple projects. The concept of the project is very abstract, it depends on what you use Gradle do. Jar project can be a library or a web application. It can be a zip archive, you can deploy the project to a production environment, and so on;

A project is composed of one or more tasks; task is to build atomic unit of work, the smallest unit of work. For example: compiled byte code, create a jar.

Hello World

Creating a simple task, output Hello World !.

Groovy

task hello{
    doLast {
        println 'Hello World.'
    }
}

Kotlin

tasks.register("hello"){
    doLast {
        println ("Hello World.")
    }
}

Enter the command to run the task

gradle hello

Line results

Create a simple building

Create a new directory

Use the init command to create a building project

Gradle now supports two --Kotlin DSL and Groovy DSL. The default is based on Groovy, and can be used if you want to use Kotlin --dsl kotlin

gradle init --dsl kotlin

initialization

It will generate the following directory

Generated directory

Which is .gitigonre git ignore file; .gradle some of the information gradle.

We need to focus on is the following files

├── cradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── build.gradle.kts
├── gradlew
├── gradlew.bat
└── settings.gradle.kts

  • warpper directory is used Gradle Wrapper
  • gradle-wrapper.jar is performed using Gradle Wrapper
  • gradle-wrapper.properties is a configuration attribute Gradle Wrapper
  • Gradle build.gradle.kts used to configure the current project build script
  • gradlew script is executed on a Unix machine
  • gradlew.bat script is executed on the window
  • settings.gradle.kts for Gradle Gradle build script configuration settings

Create a task

Gradle provides the creation and configuration tasks by Groovy or DSL-based kotlin of the api. Project contains a set of tasks, each perform basic operations.

Gradle provides a task library, we can configure these tasks on their own projects. For example, there is a core task type Copy, which can copy files.
Here we configure this task to use it.

Create a src directory
create a file name of any in src, for example myFile.txt. Add any content, such as Hello World!
Add a copy Copy type of task configuration script (build.gradle.kts), copied from the directory src to dest directory

Gradle will automatically create dest directory at execution time.

Kotlin

 tasks.register<Copy>("copy"){
     description = "Copies sources to the dest directory"
     group = "Custom"
     from("src")
     into("dest")
    
 }

Groovy

task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}

description and group are optional, you can easily define; also can be omitted;

Perform copy tasks

gradle copy

Check that the inside of the myFile.txt copying is complete.

Use plug-ins

Gradle provides a large number of plug-in Gradle plug-in station can be found

Today, using a combination of plug-base type Zip its core tasks and achieve the packaged archive task.

Use base plug

Kotlin

plugins {
    id("base")
}

... rest of the build file ...

Groovy

plugins {
    id "base"
}

... rest of the build file ...

Creating a zip task, extends from the core type Zip

Kotlin

tasks.create<Zip>("zip") {
    description = "Archives sources in a zip file"
    group = "Archive"

    from("src")
    setArchiveName("basic-demo-1.0.zip")
}

Groovy

task zip(type: Zip, group: "Archive", description: "Archives sources in a zip file") {
    from "src"
    setArchiveName "basic-demo-1.0.zip"
}

Execution zip

gradle zip

Thus, a simple building completed.

Analysis of the build process

Gradle provides a web-based view Construction - Construction Scan

Use --scan option on or explicitly used to build plug-ins are available for free scans in https://scans.gradle.com create a view scan analysis

gradle zip --scan

After the same will be sent directly to the Terms of Service platform to build scan, copy the link below to open.

Construction of scan

After opening the analysis will build a detailed project

Analysis of Building

End

Guess you like

Origin www.cnblogs.com/skymxc/p/11298755.html