1. 程式人生 > >gradle學習筆記(六) 官方文件筆記+理解

gradle學習筆記(六) 官方文件筆記+理解

前言

接著學習筆記(五),這篇文章是官方文件的筆記,和自己的一些理解。看了好幾天,終於發現一個比較能夠講清楚的邏輯:

  1. User Guide第三大章都有必要看
  2. 看完User Guide直接看Gradle Build Language Reference即資料夾中的dsl,馬上就能有個清晰的理解。

必須要看 User Guide 的 Chapter 25. Gradle Plugins 和 Gradle DSL。這樣一來就差不多能夠理解 Gradle是個框架,真正功能是外掛實現的 這句話了。



以下是自己的一些筆記。

Gradle DSL筆記:
先理解一些概念,以幫助你寫指令碼。
第一,

Gradle指令碼是配置指令碼,當執行指令碼時,會生成與該指令碼對應的特殊物件。
build.gradle生成org.gradle.api.Project物件;
settings.script 生成 org.gradle.api.initialization.Settings 物件
第二, 每個指令碼物件都實現 org.gradle.api.Script 介面,所以你可以使用它定義的屬性和方法。

A build script is made up of zero or more statements and script blocks. Statements can include method calls, property assignments, and local variable definitions. A script block is a method call which takes a closure as a parameter. The closure is treated as a configuration closure which configures some delegate object as it executes.

Gradle指令碼都是由語句和指令碼塊組成的。
語句可以是方法呼叫,新增變數等。
語句塊是一個方法,其引數是一個閉包,該閉包將代表的是配置資訊。

  1. 從Gradle DSL中大概可以看到,Gradle本身只是提供了一個框架,它的方法並不多,真正能夠完成構建任務的是相應的外掛,這點也映證了User Guide Chapter 25 Gradle Plugins的說法,真正的功能都是由外掛實現的。

  2. 可以看到語句塊本身接收的是一個閉包引數,而其中描述了我們的配置資訊,回想一下Android中的配置,完全是這樣的。至此,對Gradle構建有了一個完整的認識。 當然,還是需要練習的。

UserGuide筆記 :

Chapter 14. Build Script Basics

建立你的第一個Gradle程式: Hello World。
.The gradle command looks for a file called build.gradle in the current directory. We call this build.gradle file a build script.

Chapter 16,Writing Build Scripts

org.gradle.api.Project類,它對應於build.gradle檔案,當執行build.gradle指令碼檔案時,會相應的生成該類。

  • 指令碼中未定義的方法,會到project中尋找
  • 指令碼中未定義的屬性,會到project中尋找

建立變數:
指令碼中有兩種變數:local variables and extra properties.

Some Groovy basics一些Groovy基礎
其中介紹了一些Groovy的基礎,和在Gradle中的應用,就是之前學的groovy基礎。

Chapter 17. More about Tasks

獲取task:
1. each task is available as a property of the project, using the task name as the property name
每一個task就是該project的一個屬性,可以通過task name來獲取該task.

2.Tasks are also available through the tasks collection.
可以通過task collections獲取

Chapter 20. The Build Lifecycle

Gradle會把這些task形成一個有向圖來構建,保證了每個都會被執行到並且只執行一次。

Build phases構建階段

  1. Initialization初始化
    初始化時,Gradle會去判斷哪些應該被構建,併為這些需要構建的專案建立相應的Project物件。
  2. Configuration配置
    During this phase the project objects are configured.The build scripts of all projects which are part of the build are executed.
    所有專案的build.gradle指令碼會被執行。
  3. Execution執行
    Gradle determines the subset of the tasks, created and configured during the configuration phase, to be executed.
    執行在Configuration階段生成的一系列tasks。

Settings file

對應 org.gradle.api.initialization.Settings 類。

A multiproject build must have a settings.gradle file in the root project of the multiproject hierarchy. It is required because the settings file defines which projects are taking part in the multi-project build
mutiproject必須要有settings.gradle,因為settings.gradle定義了哪些project要被構建。

Multi-project builds

1.Hierarchical layouts
include 'project1', 'project2:child', 'project3:child1'
對應於該方法void include(String[] projectPaths)
'services:hotels:api' 意味著將建立3個project, services,hotels,api.

2.Flat layouts
includeFlat 'project3', 'project4'
用這種方法引入的project必須是rootProject的一級project。

監聽task

1.監聽task新增是否到Project中
You can receive a notification immediately after a task is added to a project

//tasks是taskContainer,引數是Action,查閱API理解
tasks.whenTaskAdded { task ->
    task.ext.srcDir = 'src/main/java'
}

2.監聽task執行前後
You can receive a notification immediately before and after any task is executed.

//org.gradle.api.invocation.Gradle
//org.gradle.api.execution.TaskExecutionGraph
//全是API
gradle.taskGraph.beforeTask { Task task ->
    println "executing $task ..."
}

gradle.taskGraph.afterTask { Task task, TaskState state ->
    if (state.failure) {
        println "FAILED"
    }
    else {
        println "done"
    }
}

Chapter 23. Dependency Management

23.4 新增依賴的幾種方式:

org.gradle.api.artifacts.dsl.DependencyHandler 中有更詳細的說明

1.External dependencies 外部依賴
External module dependencies are the most common dependencies. They refer to a module in an external repository.

dependencies {
  compile 'commons-lang:commons-lang:2.6'
}

2.Project dependencies 依賴專案

compile project(':someProject')

3.File dependencies 檔案依賴
compile fileTree(dir: ‘libs’, include: [‘*.jar’])

4.Excluding transitive dependencies 去除一些東西

compile("org.gradle.test.excludes:api:1.0") {
    exclude module: 'shared'
}

23.6 Repositories

You may configure any number of repositories, each of which is treated independently by Gradle.
有以下幾種倉庫:

repositories {
    mavenCentral() //Maven central repository
    jcenter() //Maven JCenter repository
    mavenLocal() //Local Maven repository,本地的Maven cache

    //Maven repositories, Adding custom Maven repository,自定義從該url倉庫中獲取依賴
    maven { 
        url "http://repo.mycompany.com/maven2"
    }
}

Chapter 25. Gradle Plugins

Gradle at its core intentionally provides very little for real world automation. All of the useful features, like the ability to compile Java code, are added by plugins.
Gradle本身提供了很少的自動化構建,所有有用的功能,都是由外掛提供的。

外掛擴充套件了project的功能,可以新增更多的DSL配置。
A plugin is simply any class that implements the Plugin interface.

新增外掛,呼叫Project.apply()方法:

apply plugin: 'java'

相關推薦

gradle學習筆記() 官方筆記+理解

前言 接著學習筆記(五),這篇文章是官方文件的筆記,和自己的一些理解。看了好幾天,終於發現一個比較能夠講清楚的邏輯: User Guide第三大章都有必要看 看完User Guide直接看Gradle Build Language Reference即資料

Python學習筆記處理

alt 筆記 lin 系統 顯式 當前位置 open 刷新 大小 一:打開文件 open(name,mode,[bufferSize]) name:文件路徑 mode:文件打開方式 二:文件讀取 read()方法:可以一次讀取文件的全部內容,Python把內容讀到

Spring Boot官方筆記--PartIV: Spring Boot特性

23. SpringApplication特性 Banner SpringApplicationBuilder Events and Listeners Web Environment ApplicationArguments: 獲取SpringApplication.run(...)

Spring Boot 官方筆記

【轉載】原文來源:https://blog.csdn.net/luqiang81191293/article/details/54949197 Spring Boot每次釋出時都會提供一個它所支援的精選依賴列表。實際上,在構建配置裡你不需要提供任何依賴的版本,因為Spring Boot已

Scrapy官方筆記

1.建立Scrapy專案 首先用cmd命令列去操作,輸入 scrapy startproject 專案名 #這裡輸入的專案名,就是在你輸入的目錄它會建立一個新的資料夾,這個資料夾裡面還是同樣名字的一個資料夾,專案新建的時候其實裡面只有一個,後來的.idea是被pycha

爬蟲筆記之BeautifulSoup模組官方筆記

                            爬蟲筆記之BeautifulSoup模組官方文件筆記 文章開始把我喜歡的這句話送個大家:這個世界上還

演算法工程師修仙之路:python3官方筆記(三)

本筆記來自於python手冊的中文版 Python 簡介 Python 中的註釋以 # 字元起始,直至實際的行尾。 註釋可以從行首開始,也可以在空白或程式碼之後,但是不出現在字串中。 文字字串中的 # 字元僅僅表示 # 。 程式碼中的註釋

演算法工程師修仙之路:python3官方筆記(二)

本筆記來自於python手冊的中文版 使用 Python 直譯器 呼叫 Python 直譯器 通常你可以在主視窗輸入一個檔案結束符(Unix系統是Control-D,Windows系統是Control-Z)讓直譯器以 0 狀態碼退出。如果那沒有作用,你可以通過輸入

演算法工程師修仙之路:python3官方筆記(一)

本筆記來自於python手冊的中文版 第一章 開胃菜 雖然 Python 易於使用,但它卻是一門完整的程式語言。 與 Shell 指令碼或批處理檔案相比,它為編寫大型程式提供了更多的結構和支援。 Python 提供了比 C 更多的錯誤檢查

Profiling (移動裝置效能分析)官方筆記

本文件主要是對Unity官方教程的個人理解與總結(其實以翻譯記錄為主:>) 僅作為個人學習使用,不得作為商業用途,歡迎轉載,並請註明出處。 文章中涉及到的操作都是基於 Unity2017.3版本 參考連結: https://docs.unity3d.com

Vue-router 官方筆記

vue-router 個人理解:Vue中的路由相當於pc裡面的錨點超連結,如果點選了頁面轉向哪,也有點像ajax。 安裝 npm install vue-router 開始 用Vue.js + vue-router建立單頁應用,是非常簡單

演算法工程師修仙之路:python3官方筆記(四)

本筆記來自於python手冊的中文版 深入 Python 流程控制 if 語句 可能會有零到多個 elif 部分,else 是可選的。關鍵字 ‘elif’ 是 ’else if’ 的縮寫,這個可以有效地避免過深的縮排。if … elif … elif … 序列用於

Django Rest Framework--oauth實驗筆記--參考官方

Getting started Django OAuth Toolkit provide a support layer for Django REST Framework. This tutorial is based on the Django RES

python學習)---操作

not game seek read 終端設備 fas uic med ear 文件操作文件操作流程 1、打開文件,得到文件句柄並賦值給一個變量 2、通過句柄對文件進行操作 3、關閉文件現有文件如下: Somehow, it seems the love I knew

Spark學習之路--官方+簡單

一、學習spark中官方文件: 1.《Spark 官方文件》Spark快速入門 1.1 RDD Programming Guide 1.2 Spark SQL, DataFrames and Datasets Guide 二、簡單demo 以上技術全部

深度學習框架TensorFlow 官方中文版

TensofFlow文件已經被翻譯為中文,歡迎大家學習參考使用,下面節選基本使用方法一節,完整內容可以下載或訪問官方網站。 基本使用 使用 TensorFlow, 你必須明白 TensorFlow: 使用圖 (graph) 來表示計算任務. 在被稱之為 會話 (Session) 的上下文

Cassandra 3.x官方_理解結構

結構簡介 Cassandra 被設計成可以處理跨多個節點的大資料量工作負載,沒有單點故障。它的體系結構是基於這樣的理解,系統和硬體故障可以和確實發生。Cassandra解決這種故障的方式是,採用點對點的分散式系統,把資料均勻的分佈在叢集的所有節點之中。每一個節點通過點對點的

ALSA驅動分析,比ALSA官方理解多了

Linux ALSA音效卡驅動之一:ALSA架構簡介 http://blog.csdn.net/droidphone/article/details/6271122 Linux ALSA音效卡驅動之二:音效卡的建立 http://blog.csdn.net/droidpho

tensorflow學習筆記:tensorflow官方學習 Image Recognition(Inception v3模型)

我們大腦的成像過程似乎很容易。人們毫不費力地就能區分出獅子和美洲虎,閱讀符號,或是識別面孔。但是這些任務對於計算機而言卻是一個大難題:它們之所以看上去簡單,是因為我們的大腦有著超乎想象的能力來理解影象。 在過去幾年裡,機器學習在解決這些難題方面取得了巨大的進步。其中,

elasticsearch官方學習筆記----Getting Started

Getting Started 基本概念 1)準實時:ES搜尋是一個接近實時的搜尋平臺。這意味著從您索引一個文件的時間到它可搜尋的時間,有一個輕微的延遲(通常是一秒)。 2)叢集:ES是一個叢集,一個叢集由一個惟一的名稱標識id,預設情況下是“elasticsearch