Gradle二(教程)

Projects 和 tasks

Gradle 里的任何东西都是基于这两个基础概念:

  • projects ( 项目 )
  • tasks ( 任务 )

每一个构建都是由一个或多个 projects 构成的. 一个 project 到底代表什么取决于你想用 Gradle 做什么. 举个例子, 一个 project 可以代表一个 JAR 或者一个网页应用. 它也可能代表一个发布的 ZIP 压缩包, 这个 ZIP 可能是由许多其他项目的 JARs 构成的. 但是一个 project 不一定非要代表被构建的某个东西. 它可以代表一件**要做的事, 比如部署你的应用.

不要担心现在看不懂这些说明. Gradle 的合约构建可以让你来具体定义一个 project 到底该做什么.

每一个 project 是由一个或多个 tasks 构成的. 一个 task 代表一些更加细化的构建. 可能是编译一些 classes, 创建一个 JAR, 生成 javadoc, 或者生成某个目录的压缩文件.

目前, 我们先来看看定义构建里的一些简单的 task. 以后的章节会讲解多项目构建以及如何通过 projects 和 tasks 工作.

教程

初始化项目

>mkdir basic-demo
>cd basic-demo

>gradle init

Select type of project to generate:
  1: basic
  2: application
  3: library
  4: Gradle plugin
Enter selection (default: basic) [1..4] 1

Select build script DSL:
  1: Groovy
  2: Kotlin
Enter selection (default: Groovy) [1..2] 1

Project name (default: basic-demo):


> Task :init
Get more help with your project: https://guides.gradle.org/creating-new-gradle-builds

BUILD SUCCESSFUL in 8s
2 actionable tasks: 2 executed

Groovy

├── build.gradle  1
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar  2
│       └── gradle-wrapper.properties  3
├── gradlew  4
├── gradlew.bat  5
└── settings.gradle  6
  • 1 当前项目的Gradle构建脚本
  • 2 Gradle构建的jar包
  • 3 Gradle构建jar包的配置文件
  • 4 Unix的gradle脚本
  • 5 Window的gradle脚本
  • 6 Gradle构建的配置脚本

创建任务

  • 初始化
> mkdir src 
> 添加myfile.txt到src目录下  内容为:Hello World!
> 定义一个copy任务
  • 定义任务

build.gradle

task copy(type: Copy, group: "Custom", description: "Copies sources to the dest directory") {
    from "src"
    into "dest"
}
  • 执行copy任务
> gradlew.bat copy
> Task :copy

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

插件应用

插件仓库

  • 添加插件

build.gradle

plugins {
    id "base"
}

... rest of the build file ...
  • 添加任务

build.gradle

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

生成 basic-demo-1.0.zip 在 build/distributions目录下

浏览和调试构建

浏览
> gradlew tasks

> 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 'basic-demo'.                         components - Displays the components produced by root project 'basic-demo'. [incubating]
dependencies - Displays all dependencies declared in root project 'basic-demo'.
dependencyInsight - Displays the insight into a specific dependency in root project 'basic-demo'.
dependentComponents - Displays the dependent components of components in root project 'basic-demo'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'basic-demo'. [incubating]
projects - Displays the sub-projects of root project 'basic-demo'.                                                      properties - Displays the properties of root project 'basic-demo'.
tasks - Displays the tasks runnable from root project 'basic-demo'.

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 979ms
1 actionable task: 1 executed
分析和调试
> gradlew zip --scan

BUILD SUCCESSFUL in 11s
1 actionable task: 1 up-to-date

Publishing a build scan to scans.gradle.com requires accepting the Gradle Terms of Service defined at https://gradle.com/terms-of-service. Do you accept these terms? [yes, no] yes

Gradle Terms of Service accepted.

Publishing build scan...
https://gradle.com/s/qeawj2tpgbuzs

发现可以属性
> gradlew properties

> Task :properties

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

allprojects: [root project 'basic-demo']
ant: org.gradle.api.internal.project.DefaultAntBuilder@58228d28
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@63cf5cdd
archivesBaseName: basic-demo
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@e3a744e
asDynamicObject: DynamicObject for root project 'basic-demo'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@5934de7a
buildDir: D:\learn\gradle\basic-demo\build
buildFile: D:\learn\gradle\basic-demo\build.gradle
buildPath: :
buildScriptSource: org.gradle.groovy.scripts.TextResourceScriptSource@153353b9
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@c83cc76
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@51c641ac
components: SoftwareComponentInternal set
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@2efe82e0
configurationTargetIdentifier: org.gradle.configuration.ConfigurationTargetIdentifier$1@21dfa21d
configurations: configuration container
convention: org.gradle.api.internal.plugins.DefaultConvention@33ad3a2c
copy: task ':copy'
defaultArtifacts: org.gradle.api.internal.plugins.DefaultArtifactPublicationSet_Decorated@5005c6d4
defaultTasks: []
deferredProjectConfiguration: org.gradle.api.internal.project.DeferredProjectConfiguration@ab6b7b6
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@56a523b7
dependencyLocking: org.gradle.internal.locking.DefaultDependencyLockingHandler_Decorated@c030f15
depth: 0
description: null
displayName: root project 'basic-demo'
distsDir: D:\learn\gradle\basic-demo\build\distributions
distsDirName: distributions
ext: org.gradle.internal.extensibility.DefaultExtraPropertiesExtension@72ce4b27
extensions: org.gradle.api.internal.plugins.DefaultConvention@33ad3a2c
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@4dfa49cf
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@323d635c
gradle: build 'basic-demo'
group:
identityPath: :
inheritedScope: org.gradle.internal.extensibility.ExtensibleDynamicObject$InheritedDynamicObject@26d2b9a9
layout: org.gradle.api.internal.file.DefaultProjectLayout@26454927
libsDir: D:\learn\gradle\basic-demo\build\libs
libsDirName: libs
listenerBuildOperationDecorator: org.gradle.configuration.internal.DefaultListenerBuildOperationDecorator@396ed811
logger: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger@60907be3
logging: org.gradle.internal.logging.services.DefaultLoggingManager@53bc89a9
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@5f3cee88
modelSchemaStore: org.gradle.model.internal.manage.schema.extract.DefaultModelSchemaStore@10dd1b2e
module: org.gradle.api.internal.artifacts.ProjectBackedModule@5051ea02
mutationState: project :
name: basic-demo
normalization: org.gradle.normalization.internal.DefaultInputNormalizationHandler_Decorated@73f135b5
objects: org.gradle.api.internal.model.DefaultObjectFactory@44370ba3
parent: null
parentIdentifier: null
path: :
pluginManager: org.gradle.api.internal.plugins.DefaultPluginManager_Decorated@7cc7e503
plugins: [org.gradle.api.plugins.HelpTasksPlugin@43b95de, org.gradle.buildinit.plugins.BuildInitPlugin@7e9d5471, org.gradle.buildinit.plugins.WrapperPlugin@20b3212, org.gradle.language.base.plugins.LifecycleBasePlugin@412720ad, org.gradle.api.plugins.BasePlugin@196ac832]
processOperations: org.gradle.process.internal.DefaultExecActionFactory$DecoratingExecActionFactory@d4029b2
project: root project 'basic-demo'
projectConfigurator: org.gradle.api.internal.project.BuildOperationCrossProjectConfigurator@568011aa
projectDir: D:\learn\gradle\basic-demo
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@2e5a719f
projectPath: :
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@613e173e
properties: {...}
providers: org.gradle.api.internal.provider.DefaultProviderFactory@6d3fbf12
publicType: org.gradle.api.plugins.BasePluginConvention
repositories: repository container
resourceLoader: org.gradle.internal.resource.transfer.DefaultUriTextResourceLoader@3b64ee5e
resources: org.gradle.api.internal.resources.DefaultResourceHandler@46cfa624
rootDir: D:\learn\gradle\basic-demo
rootProject: root project 'basic-demo'
script: false
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@5759de24
scriptPluginFactory: org.gradle.configuration.ScriptPluginFactorySelector@478e2cf2
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$4@5b043c43
services: ProjectScopeServices
standardOutputCapture: org.gradle.internal.logging.services.DefaultLoggingManager@53bc89a9
state: project state 'EXECUTED'
status: integration
subprojects: []
tasks: task set
version: unspecified
zip: task ':zip'

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值