Gradle探索之路(一)

首先我们要安装gradle并配置环境变量,参考官网
https://docs.gradle.org/current/userguide/installation.html

1.首先新建目录

mkdir basic-demo
cd basic-demo
gradle init

然后可以查看到basic-demo目录下生成了如下文件

build.gradle  gradle/  gradlew*  gradlew.bat  settings.gradle

我们想要完成拷贝文件夹的task,所以可以在当前目录下新建一个src文件夹,然后在该文件夹下touch一个文件myFile.txt,然后打开build.gradle,构建一个task任务,
build.gradle

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

目标是将src文件夹下的myFile.txt拷贝到dest文件夹,当然dest是自动生成的
task编写完成后即可执行该任务

gradle copy

执行成功后我们看下目录结构

F:\gradledemo\basic-demo>ls
build.gradle  dest/  gradle/  gradlew*  gradlew.bat  settings.gradle  src/

果然生成了dest文件夹,进入我们可查看到myFile.txt文件,说明这个task完成了拷贝文件的任务

压缩文件
首先需要add base plugin to your build script file ,base插件一定要放到.gradle文件头部

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

然后即可看到build/distributions目录下生成了我们压缩的文件

F:\gradledemo\basic-demo\build\distributions>ls
basic-demo-1.0.zip

Gradle还提供了基于构建的丰富的基于Web的视图,称为构建扫描,通过--scan

image.png

gradle zip --scan
F:\gradledemo\basic-demo>gradle zip --scan

BUILD SUCCESSFUL in 4s
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] ye
Please enter 'yes' or 'no': yes

Gradle Terms of Service accepted.

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

attempt to call a nil value
F:\gradledemo\basic-demo>

https://gradle.com/s/ga3kzf23dvrjw这个地址就是我们构建zip Task的基于web的视图
通过浏览构建扫描,则应该能够轻松找出执行了哪些任务,花费了多长时间,应用了哪些插件等等

Gradle构建属性

gradle properties
F:\gradledemo\basic-demo>gradle properties

> Task :properties

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

allprojects: [root project 'zzl_demo']
ant: org.gradle.api.internal.project.DefaultAntBuilder@bc47d9f
antBuilderFactory: org.gradle.api.internal.project.DefaultAntBuilderFactory@66545860
archivesBaseName: zzl_demo
artifacts: org.gradle.api.internal.artifacts.dsl.DefaultArtifactHandler_Decorated@60073ba0
asDynamicObject: DynamicObject for root project 'zzl_demo'
baseClassLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@2e4882a9
buildDir: F:\gradledemo\basic-demo\build
buildFile: F:\gradledemo\basic-demo\build.gradle
buildPath: :
buildScriptSource: org.gradle.groovy.scripts.TextResourceScriptSource@6c83a7a
buildscript: org.gradle.api.internal.initialization.DefaultScriptHandler@429bf59b
childProjects: {}
class: class org.gradle.api.internal.project.DefaultProject_Decorated
classLoaderScope: org.gradle.api.internal.initialization.DefaultClassLoaderScope@1b980c53
components: SoftwareComponentInternal set
configurationActions: org.gradle.configuration.project.DefaultProjectConfigurationActionContainer@712f9843
configurationTargetIdentifier: org.gradle.configuration.ConfigurationTargetIdentifier$1@367377f6
configurations: configuration container
convention: org.gradle.api.internal.plugins.DefaultConvention@2b597b8e
copy: task ':copy'
defaultArtifacts: org.gradle.api.internal.plugins.DefaultArtifactPublicationSet_Decorated@70780022
defaultTasks: []
deferredProjectConfiguration: org.gradle.api.internal.project.DeferredProjectConfiguration@7d1863ee
dependencies: org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@50f9777c
dependencyLocking: org.gradle.internal.locking.DefaultDependencyLockingHandler_Decorated@301ce3e6
depth: 0
description: null
displayName: root project 'zzl_demo'
distsDir: F:\gradledemo\basic-demo\build\distributions
distsDirName: distributions
ext: org.gradle.internal.extensibility.DefaultExtraPropertiesExtension@d59cd7
extensions: org.gradle.api.internal.plugins.DefaultConvention@2b597b8e
fileOperations: org.gradle.api.internal.file.DefaultFileOperations@3eec8830
fileResolver: org.gradle.api.internal.file.BaseDirFileResolver@25b55a06
gradle: build 'zzl_demo'
group:
identityPath: :
inheritedScope: org.gradle.internal.extensibility.ExtensibleDynamicObject$InheritedDynamicObject@72e18e85
layout: org.gradle.api.internal.file.DefaultProjectLayout@5e7e972a
libsDir: F:\gradledemo\basic-demo\build\libs
libsDirName: libs
listenerBuildOperationDecorator: org.gradle.configuration.internal.DefaultListenerBuildOperationDecorator@48929dd logger: org.gradle.internal.logging.slf4j.OutputEventListenerBackedLogger@b633620
logging: org.gradle.internal.logging.services.DefaultLoggingManager@7588046f
modelRegistry: org.gradle.model.internal.registry.DefaultModelRegistry@5f650a33
modelSchemaStore: org.gradle.model.internal.manage.schema.extract.DefaultModelSchemaStore@126f07c3
module: org.gradle.api.internal.artifacts.ProjectBackedModule@1715fd56
mutationState: project :
name: zzl_demo
normalization: org.gradle.normalization.internal.DefaultInputNormalizationHandler_Decorated@aff198
objects: org.gradle.api.internal.model.DefaultObjectFactory@3531369e
parent: null
parentIdentifier: null
path: :
pluginManager: org.gradle.api.internal.plugins.DefaultPluginManager_Decorated@42728699
plugins: [org.gradle.buildinit.plugins.BuildInitPlugin@28c9d00b, org.gradle.buildinit.plugins.WrapperPlugin@5715c7bb, org.gradle.api.plugins.HelpTasksPlugin@6bac9e01, org.gradle.language.base.plugins.LifecycleBasePlugin@3dbc8ba3, org.gradle.api.plugins.BasePlugin@5b935d97]
processOperations: org.gradle.process.internal.DefaultExecActionFactory$DecoratingExecActionFactory@22c27141
project: root project 'zzl_demo'
projectConfigurator: org.gradle.api.internal.project.BuildOperationCrossProjectConfigurator@2d11af8e
projectDir: F:\gradledemo\basic-demo
projectEvaluationBroadcaster: ProjectEvaluationListener broadcast
projectEvaluator: org.gradle.configuration.project.LifecycleProjectEvaluator@74a9d81c
projectPath: :
projectRegistry: org.gradle.api.internal.project.DefaultProjectRegistry@35974cfb
properties: {...}
providers: org.gradle.api.internal.provider.DefaultProviderFactory@46e7b71
publicType: org.gradle.api.plugins.BasePluginConvention
repositories: repository container
resourceLoader: org.gradle.internal.resource.transfer.DefaultUriTextResourceLoader@24a3d9ad
resources: org.gradle.api.internal.resources.DefaultResourceHandler@1ef08c1d
rootDir: F:\gradledemo\basic-demo
rootProject: root project 'zzl_demo'
script: false
scriptHandlerFactory: org.gradle.api.internal.initialization.DefaultScriptHandlerFactory@118febac
scriptPluginFactory: org.gradle.configuration.ScriptPluginFactorySelector@66cdbf1f
serviceRegistryFactory: org.gradle.internal.service.scopes.ProjectScopeServices$4@5c996cda
services: ProjectScopeServices
standardOutputCapture: org.gradle.internal.logging.services.DefaultLoggingManager@7588046f
state: project state 'EXECUTED'
status: integration
subprojects: []
tasks: task set
version: unspecified
zip: task ':zip'

BUILD SUCCESSFUL in 1s

你可能感兴趣的:(Gradle探索之路(一))