Gradle构建语言手册

来源:互联网 发布:软件测试工程师工资 编辑:程序博客网 时间:2024/04/30 12:22


《Gradle构建语言参考手册》

                 版本V2.5



原网址:https://docs.gradle.org/current/dsl/index.html

简介:
    该参考指南描述了Gradle构建语言或者DSL(领域专用语言)的各个方面。

基础:
        Gradle脚本是配置脚本,作为配置脚本运行,它配置一个特定类型的对象。例如作为一个构建脚本执行时,它配置了工程(Project)类型的对象。这个对象被叫做这个脚本的委托对象。下面是每种Gradle脚本的委托对象。
Type of scriptDelegates to instance ofBuild scriptProjectInit scriptGradleSettings scriptSettings        你可以在脚本中用这些对象的属性和方法。
        每个Gradle脚本都实现了Script接口,接口中定义了很多你可以在脚本中使用的属性和方法。

构建脚本的结构:
        一个构建脚本由零到多个语句和脚本块组成,语句可以包含方法调用、属性赋值、局部变量定义。一个脚本块是一个带有闭包参数的方法调用,这个闭包可以看成一个配置闭包(当它执行时配置一些委托对象)。顶级脚本块如下:

BlockDescriptionallprojects { }

Configures this project and each of its sub-projects.

artifacts { }

Configures the published artifacts for this project.

buildscript { }

Configures the build script classpath for this project.

configurations { }

Configures the dependency configurations for this project.

dependencies { }

Configures the dependencies for this project.

repositories { }

Configures the repositories for this project.

sourceSets { }

Configures the source sets of this project.

subprojects { }

Configures the sub-projects of this project.

publishing { }

Configures the PublishingExtension added by the publishing plugin.

        一个构建脚本也是一个Groovy脚本,所以可以包含Groovy脚本中的方法定义或类定义等元素。

DependencyHandler依赖管理器:
        依赖管理器用来定义依赖的,而Dependencies(依赖)属于configurations(配置)。
        为一个配置定义指定的依赖,可以用下面的语法:
dependencies {  
配置名 依赖标志1, 依赖标志2, ...
}
配置名可以是:compile、runtime、testCompile、testRuntime、archives、default。

例如:
apply plugin: 'java'//以便于我们用'compile', 'testCompile' 配置依赖dependencies {  //要要使用的依赖,在Repository仓库中可以找到  //group:name:version,相当于Maven中的groupId、artifactId、version  compile 'commons-lang:commons-lang:2.6'  testCompile 'org.mockito:mockito:1.9.0-rc1'  //也可以是Map类型的notation  compile group: 'com.google.code.guice', name: 'guice', version: '1.0'  //声明任意文件作为依赖  compile files('hibernate.jar', 'libs/spring.jar')  //把所有libs目录下的jars都放到编译目录中  compile fileTree('libs')}

高级依赖配置:

dependencies {    配置名(依赖标志){        配置声明1        配置声明2    }}

例如:

apply plugin: 'java' //以便声明 'compile' 依赖dependencies {  compile('org.hibernate:hibernate:3.1') {    //万一版本冲突,3.1版本赢    force = true    //排除特定的传递依赖    exclude module: 'cglib' //通过artifact name(工件名)    exclude group: 'org.jmock' //通过group(组)    exclude group: 'org.unwanted', module: 'iAmBuggy' //通过名和组    //是否禁用该依赖的传递依赖    transitive = false  }}

外部依赖的Dependency notations(依赖标志):

configurationName "group:name:version:classifier@extension"

configurationName group: group:, name: name, version: version, classifier: classifier, ext: extension

工程依赖的依赖标志:

configurationName project(':someProject')

configurationName project(path: ':projectA', configuration: 'someOtherConfiguration')

文件依赖的依赖标志:

configurationName files('a file')

0 0
原创粉丝点击