系統(tǒng)學(xué)習(xí) Gradle ,不局限于Android 俗扇,學(xué)習(xí)《實(shí)戰(zhàn)Gradle》并簡(jiǎn)要記錄
1 安裝
直接參考 Gradle主頁(yè) 安裝方式驶社,安裝即可奔脐!
兩種安裝方式:
1.1 Install with a package manager (使用 包管理器 安裝)
對(duì)于Mac系統(tǒng),需要先安裝 Homebrew 星澳,然后在使用下面的命令 安 Gradle
$ brew install gradle
運(yùn)行之后疚顷,提示如下信息(偶然性),需要升級(jí) Xcode 版本禁偎,或者 干掉它腿堤。
刪掉還是升級(jí)如暖,任選一種吧笆檀,處理之后:
驗(yàn)證安裝是否成功
$ gradle -v
查詢結(jié)果如下,表示安裝成功
1.2 Install manually(手動(dòng)安裝)
參考官網(wǎng)盒至,本文采取第一種安裝方式
2 命令行選項(xiàng)
gradle 命令允許同時(shí)定義一個(gè)或多個(gè)命令行選項(xiàng)
比如下文要提到的 -i 和 -s 選項(xiàng)酗洒,就可以合并為 -is 或 -i -s
本文將介紹 最重要的10個(gè)命令行選項(xiàng),并進(jìn)行簡(jiǎn)單分類:
- 日志選項(xiàng) 3個(gè) 枷遂,-i / -s / -q
- 參數(shù)選項(xiàng) 2個(gè)樱衷,-D / -P
- 幫助任務(wù) 2個(gè) ,tasks / properties
- 其它選項(xiàng)3個(gè), -? / -b / --offline
日志選項(xiàng) (-i / -s / -q)
2.1 開(kāi)始使用 Gradle 酒唉,并使用 -q 命令行選項(xiàng)
創(chuàng)建腳本文件矩桂,然后運(yùn)行。
Gradle 構(gòu)建腳本默認(rèn)名字是 build.gradle
2.1.1 在對(duì)應(yīng)路徑下黔州,創(chuàng)建腳本文件耍鬓,從 Hello World 開(kāi)始
腳本語(yǔ)言是Groovy(Gradle的通用語(yǔ)言),其中 task 和 action (doLast 就是一個(gè) action)是這門語(yǔ)言(Gradle就是Groovy)的重要元素流妻!
task helloWorld{
doLast{
println 'hello world !'
}
}
<< 可以簡(jiǎn)單代表 doLast 牲蜀,上述代碼可簡(jiǎn)化為:
task helloWorld << {
println 'hello world !'
}
2.1.2 運(yùn)行腳本
gradle -q helloworld
<< 替換 doLast 后的兩次運(yùn)行結(jié)果一致,如下:
去掉 -q 命令行選項(xiàng)绅这,打印的信息如下:
- doLast 是 task 執(zhí)行的最后一個(gè) action 涣达,可用 << 簡(jiǎn)單代表
- 通過(guò) -q 命令行選項(xiàng) 使其只輸出該task相關(guān)的信息。同 --quiet 命令行選項(xiàng)
gradle --quiet helloworld
2.2 命令行選項(xiàng) -i 证薇, --info
默認(rèn)設(shè)置中不會(huì)提供大量的輸出信息度苔,如果想知道構(gòu)建中發(fā)生了什么,
日志級(jí)別改到 INFO 浑度,可以獲取到更多信息
2.3 命令行選項(xiàng) -s 寇窑, --stacktrace
-s 選項(xiàng)在有異常拋出時(shí)會(huì)打印出簡(jiǎn)短的堆棧跟蹤信息,以幫助進(jìn)行調(diào)試
將腳本故意多寫一個(gè)大括號(hào)箩张,以測(cè)試 -s 選項(xiàng)
task helloWorld << {
println 'hello world !'
}
}
對(duì)比未使用 -s 和使用時(shí)的區(qū)別:
可以看到詳細(xì)的堆棧信息(中間的沒(méi)有截圖,太多了)
參數(shù)選項(xiàng) (-D / -P)
2.4 命令行選項(xiàng) -D 饮笛, --system-prop
Gradle 是以一個(gè) JVM進(jìn)程運(yùn)行的咨察,和所有的 Java 進(jìn)程一樣,可以提供一個(gè)系統(tǒng)參數(shù)福青。
-D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue).
2.5 命令行選項(xiàng) -P , --project-prop
項(xiàng)目參數(shù)是構(gòu)建腳本中可用的變量摄狱,可以使用這個(gè)選項(xiàng)直接向構(gòu)建腳本中傳入?yún)?shù)
-P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue).
幫助任務(wù) (tasks / properties)
2.6 命令行選項(xiàng) tasks
顯示項(xiàng)目中所有可運(yùn)行的task,包括它們的描述信息无午。
項(xiàng)目中應(yīng)用的插件可能會(huì)提供一些額外的task媒役。
2.7 命令行選項(xiàng) properties
顯示出項(xiàng)目中所有可用的屬性。
jake@bogon:~/Documents/gradleTest$ gradle properties
> Task :properties
------------------------------------------------------------
Root project
------------------------------------------------------------
allprojects: [root project 'gradleTest']
ant: org.gradle.api.internal.project.DefaultAntBuilder@1a8f065b
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@5cdd0183
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@5e0ed76
asDynamicObject: DynamicObject for root project 'gradleTest'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@7dfd9b3b
buildDir: /Users/jake/Documents/gradleTest/build
buildFile: /Users/jake/Documents/gradleTest/build.gradle
buildPath: :
buildScriptSource: org.gradle.groovy.scripts.TextResourceScriptSource@1f22e701
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@2b473c7f
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@294eef3c
components: SoftwareComponentInternal set
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@387b9d6d
configurationTargetIdentifier: org.gradle.configuration.ConfigurationTargetIdentifier$1@30e97f8c
configurations: configuration container
convention: org.gradle.api.internal.plugins.DefaultConvention@225f192a
defaultTasks: []
deferredProjectConfiguration: org.gradle.api.internal.project.DeferredProjectConfiguration@7cb62552
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@18425f5c
dependencyLocking: org.gradle.internal.locking.DefaultDependencyLockingHandler_Decorated@632c46c7
depth: 0
description: null
displayName: root project 'gradleTest'
ext: org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension@396085c1
extensions: org.gradle.api.internal.plugins.DefaultConvention@225f192a
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@74b1ab0a
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@7e83a27f
gradle: build 'gradleTest'
group:
helloWorld: task ':helloWorld'
identityPath: :
inheritedScope: org.gradle.api.internal.ExtensibleDynamicObject$InheritedDynamicObject@7f18d916
layout: org.gradle.api.internal.file.DefaultProjectLayout@426b15b9
listenerBuildOperationDecorator: org.gradle.configuration.internal.DefaultListenerBuildOperationDecorator@3b7b0f05
logger: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger@48044c24
logging: org.gradle.internal.logging.services.DefaultLoggingManager@5808d24f
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@61616031
modelSchemaStore: org.gradle.model.internal.manage.schema.extract.DefaultModelSchemaStore@15fbb562
module: org.gradle.api.internal.artifacts.ProjectBackedModule@7d30806e
name: gradleTest
normalization: org.gradle.normalization.internal.DefaultInputNormalizationHandler_Decorated@7bbd6779
objects: org.gradle.api.internal.model.DefaultObjectFactory@18dd5c2c
parent: null
parentIdentifier: null
path: :
pluginManager: org.gradle.api.internal.plugins.DefaultPluginManager_Decorated@6597fd2
plugins: [org.gradle.api.plugins.HelpTasksPlugin@6cd3e7c0]
processOperations: org.gradle.api.internal.file.DefaultFileOperations@74b1ab0a
project: root project 'gradleTest'
projectConfigurator: org.gradle.api.internal.project.BuildOperationCrossProjectConfigurator@7c45c38c
projectDir: /Users/jake/Documents/gradleTest
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@e0bcc14
projectPath: :
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@43e185e8
properties: {...}
providers: org.gradle.api.internal.provider.DefaultProviderFactory@76c75cee
repositories: repository container
resourceLoader: org.gradle.internal.resource.transfer.DefaultUriTextResourceLoader@30ff87b3
resources: org.gradle.api.internal.resources.DefaultResourceHandler@62f46b83
rootDir: /Users/jake/Documents/gradleTest
rootProject: root project 'gradleTest'
script: false
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@32cf70a5
scriptPluginFactory: org.gradle.configuration.ScriptPluginFactorySelector@7d4e6dd1
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$4@31c0afd4
services: ProjectScopeServices
standardOutputCapture: org.gradle.internal.logging.services.DefaultLoggingManager@5808d24f
state: project state 'EXECUTED'
status: release
subprojects: []
tasks: task set
version: unspecified
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/4.10.2/userguide/command_line_interface.html#sec:command_line_warnings
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
其它選項(xiàng) (-? / -b / --offline)
2.8 命令行選項(xiàng) -指厌? , -h , --help
打印出所有可用的命令行選項(xiàng)刊愚。包含描述信息
// 任選一個(gè)命令
gradle -?
gradle -h
gradle --help
2.9 命令行選項(xiàng) -b , --build-file
Gradle 構(gòu)建腳本的默認(rèn)命名約定是 build.gradle。
使用該命令可以執(zhí)行一個(gè)特定名字的構(gòu)建腳本踩验。
命令結(jié)構(gòu)為: gradle -b [文件名] [任務(wù)名]
gradle -b jake.gradle test
gradle -q -b jake.gradle test
2.10 命令行選項(xiàng) --offline
使用該選項(xiàng)可以以離線模式運(yùn)行構(gòu)建箕憾,僅僅在本地緩存中檢查依賴是否存在牡借。
在無(wú)網(wǎng)絡(luò)且本地緩存沒(méi)有時(shí),若不使用該選項(xiàng)袭异,構(gòu)建都會(huì)失敗钠龙。
Gradle 更多,待續(xù)...