gradle-學習筆記(1)-初步使用

最近想深刻的學習一下工程化方面相關的東西,在maven和gradle直接糾結不已,由於maven的擴展性太差勁了,學習成本頗高,因此最後投入了gradle的懷抱中,之後有時間再從新學習一下maven吧shell

最近的學習筆記是基於gradle 5 系列,其中各類教程和例子大都是來源於官方文檔或者網絡上的博客。內容涵蓋我在學習gradle過程當中的各類心得和gradle的一些使用方法網絡

注意: 這裏使用的配偶語言世kotlin 而不是使用groovyapp

gradle建立項目

一個命令maven

gradle init

用戶能夠經過這個命令建立一個基本的gradle項目包括gradle項目的目錄結構函數

├── build.gradle (1)
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar (2)
│       └── gradle-wrapper.properties (3)
├── gradlew (4)
├── gradlew.bat (5)  
└── settings.gradle (6)
  • (1) gradle 的構建腳本用來構建當前的gradle項目,最核心的配置文件
  • (2) (3) 一個gradle副本和配置文件,用來當若是系統中的gradle版本和項目使用的gradle版本不一樣,將會在這裏下載一個項目中的版本
  • (4) (5) 配套使用的命令行工具 沒有.bat後綴的是unix系統命令有的是windowns系統,能夠用來執行gradle中定義的各類task 任務
  • (6) 用於配置Gradle構建的Gradle設置腳本

gradle的task

gradle 方便用戶進行配置的特性是源於gradle提供了方便使用task參數
這裏編寫一個很基本的copy文件的權限,在路徑中添加一個src文件夾和dest文件夾,在src文件中添加一個文件markfile.txt 而且裏面有一個內容hello world!工具

而後在build.gradle 中編寫一個任務學習

task copy(type:Copy,group:"custom",description:"test one"){
    from "src"
    into "dest"
}

其中的type 字段將會調用系統中的Copy函數,而group和description 只是描述這個過程的描述符,只會影響系統的log輸出,並不會影響實際的效果gradle

./gradlew Copy

運行對應的命令就能運行對應的copy方法ui

使用一個gradle內部的插件系統

在項目中使用插件標籤plugins添加指定的base插件到系統中this

plugins {
    id "base"
}

而後在指定的位置咱們添加一個插件(和任務的使用方法相同,我懷疑其實gradle的插件就是打包好的任務)

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

而後運行對應的命令,就能夠在對應的目錄 build/distributions 中找到 src目錄下的壓縮文件了

./gradlew Zip

查看當前擁有的task

gradle 有一個內置的命令

./gradlew task

這條命令將會將全部的當前的gradle項目中擁有的構建命令所有列出來

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Archive tasks
-------------
zip - Archives sources in a zip file

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
clean - Deletes the build directory.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Custom tasks
------------
copy - Copies sources to the dest directory

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'gradle'.
components - Displays the components produced by root project 'gradle'. [incubating]
dependencies - Displays all dependencies declared in root project 'gradle'.
dependencyInsight - Displays the insight into a specific dependency in root project 'gradle'.
dependentComponents - Displays the dependent components of components in root project 'gradle'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'gradle'. [incubating]
projects - Displays the sub-projects of root project 'gradle'.
properties - Displays the properties of root project 'gradle'.
tasks - Displays the tasks runnable from root project 'gradle'.

Verification tasks
------------------
check - Runs all checks.

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.

To see all tasks and more detail, run gradlew tasks --all

To see more detail about a task, run gradlew help --task <task>

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
<-------------> 0% WAITING
> IDLE

gradle 提供的在線查看構建詳情的方法 --scan命令

咱們在使用構建命令的時候能夠在命令的後面添加一個 --scan命令,經過這個命令能夠連接到gradle官方的view顯示倉庫或者鏈接到自定義的連接倉庫中,而後輕鬆的查看項目使用的構建任務,構建時間等等信息

./gradlew Zip --scan

而後能夠登入命令行打出的一個網址,在其中的form表單中填寫郵箱地址,而後就會將構建的信息發送到郵箱中了

注意:這個功能是收費的

gradle 展現當前系統的可用參數信息 properties命令

$ ./gradlew properties
> Task :properties

------------------------------------------------------------
Root project
------------------------------------------------------------

buildDir: /Users/.../basic-demo/build
buildFile: /Users/.../basic-demo/build.gradle
description: null
group:
name: basic-demo
projectDir: /Users/.../basic-demo
version: unspecified

BUILD SUCCESSFUL
相關文章
相關標籤/搜索