首先我們要安裝gradle并配置環(huán)境變量,參考官網(wǎng)
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,構(gòu)建一個task任務(wù),
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編寫完成后即可執(zhí)行該任務(wù)
gradle copy
執(zhí)行成功后我們看下目錄結(jié)構(gòu)
F:\gradledemo\basic-demo>ls
build.gradle dest/ gradle/ gradlew* gradlew.bat settings.gradle src/
果然生成了dest文件夾洽蛀,進入我們可查看到myFile.txt文件,說明這個task完成了拷貝文件的任務(wù)
壓縮文件
首先需要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還提供了基于構(gòu)建的豐富的基于Web的視圖曲掰,稱為構(gòu)建掃描,通過--scan
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這個地址就是我們構(gòu)建zip Task的基于web的視圖
通過瀏覽構(gòu)建掃描,則應(yīng)該能夠輕松找出執(zhí)行了哪些任務(wù)胎许,花費了多長時間峻呛,應(yīng)用了哪些插件等等
Gradle構(gòu)建屬性
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