gradle系列--build.gradle

其他网址

gradle的build.gradle详解_霓虹深处-CSDN博客_build.gradle

Gradle 教程_w3cschool(不带子目录)

Gradle User Guide 中文版(带子目录)   (英文版:Gradle User Manual

Gradle DSL Version 6.5(Gradle英文文档)

概述

        一个项目中只放置一个build.gradle,一个项目代表一个组件(jar/war包),构建启动后Gradle会根据build.gradle实例化一个org.gradle.api.Project类,提供了对一个项目的基本配置。

示例(一个完整的构建脚本)

apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.5
version = '1.0'

jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    testCompile group: 'junit', name: 'junit', version: '4.+'
}

test {
    systemProperties 'property': 'value'
}

uploadArchives {
    repositories {
       flatDir {
           dirs 'repos'
       }
    }
}

属性

属性名称 所属 说明
group project  
version project 项目版本(全局的)
name(artifact) project 一般写在settings.gradle
sourceCompatibility = 1.8 java插件  
targetCompatibility = 1.8 java插件  
compileJava.options.encoding = 'UTF-8'     
compileTestJava.options.encoding = 'UTF-8'     

方法

方法 说明
apply 应用
repositories 添加仓库
dependencies 添加依赖
buildscript  
allprojects  
subprojects  
configurations  

repositories{}

简介

repositories是project一个方法,闭包作为参数

repositories {
    //本地仓库,地址是maven本地仓库路径
    mavenLocal()
    //maven私服,此处设置为ali的,地址是url
    maven{
        url "http://maven.aliyun.com/nexus/content/groups/public"
    }
    //远程仓库,地址是https://repo1.maven.org/maven2
    mavenCentral()
}

buildScript/allprojects/根的repositories作用

build.gradle

buildscript {
    repositories {
    ...
    }

    dependencies {
    ...
    }

}

build.gradle

 

allprojects {
    repositories {
    ...
    }

    dependencies {
    ...
    }

}

 

build.gradle

repositories {

...

}

dependencies {

...

}

 

 

buildScript块的repositories主要是为了Gradle脚本自身的执行,获取脚本依赖插件。我在写的一篇博客《尝试Artifactory》中Gradle脚本需要com.jfrog.artifactory插件才能执行成功,而这个插件是从URL为https://plugins.gradle.org/m2/的Maven仓库获得。

根级别的repositories主要是为了当前项目提供所需依赖包,比如log4j、spring-core等依赖包可从mavenCentral仓库获得。

allprojects块的repositories用于多项目构建,为所有项目提供共同所需依赖包。而子项目可以配置自己的repositories以获取自己独需的依赖包。

修改仓库地址

        Gradle、maven默认从中央仓库mavenCentral() http://repo1.maven.org/maven2/下载依赖包,但是在国内下载速度巨慢,我们只能使用国内的镜像。

法1:每个Gradle构建的项目中,在build.gradle做如下配置

repositories {
    //本地仓库,地址是maven本地仓库路径
    mavenLocal()
    //maven私服,此处设置为ali的,地址是url
    maven{
        url "http://maven.aliyun.com/nexus/content/groups/public"
    }
    //远程仓库,地址是https://repo1.maven.org/maven2
    mavenCentral()
}

法2:每个项目都如此配置难免麻烦些,配置一个全局配置文件可以只配置一次

.grable/init.grable

allprojects{
    repositories {
        def REPOSITORY_URL = 'http://maven.aliyun.com/nexus/content/groups/public/'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $REPOSITORY_URL."
                    remove repo
                }
            }
        }
        maven {
            url REPOSITORY_URL
        }
    }
}

dependencies{}

简介

dependencies是project一个方法,闭包作为参数。

依赖范围

其他网址:
gradle依赖:implementation 和compile的区别 - 简书
Managing Dependencies of JVM Projects
(官网文档)

说明

compile/testCompile    //deprecated

implementation/testImplementation

 

compile在3.x及之后依然能用。

compileOnly/testCompileOnly

只在编译时有效,不会参与打包 

compileClasspath

testCompileClasspath

继承自:compile, compileOnly, implementation

继承自:testCompile, testCompileOnly, testImplementation

annotationProcessor

使能注解处理器。比如:lombok就要到。

runtime/testRuntime   //deprecated

runtimeOnly/testRuntimeOnly

编译时不会参与,很少用

runtimeClasspath

testRuntimeClasspath

继承自:runtimeOnly, runtime, implementation

继承自:testRuntimeOnly, testRuntime, testImplementation

archives

uploadArchives任务会用到

default

继承自:runtimeClasspath

implementation与api区别

api:跟 2.x 版本的 compile完全相同
implementation:使用了该命令编译的依赖,它仅仅对当前的Module提供接口。

例如我们当前项目结构如下

LibraryA 中引用了 LibraryC 的库,如果对 LibraryC 的依赖用的是 implementation 关键字。 如下:

dependencies {
    . . . . 
    implementation project(path:':libraryC')
}

        那么LibraryC 中的接口,仅仅只能给 LibraryA 使用,而我们的 App Module 是无法访问到 LibraryC 提供的接口的,也就是将该依赖隐藏在内部,而不对外部公开。这就是implementation关键字的作用。

建议

在Google IO 相关话题的中提到了一个建议,就是依赖首先应该设置为implement的,如果没有错,那就用implement,如果有错,那么使用api指令,这样会使编译速度有所增快。

那为什么要这么做呢?
答案是: 1. 加快编译速度。2. 隐藏对外不必要的接口。

为什么能加快编译速度呢?

这对于大型项目含有多个Module模块的, 以上图为例,比如我们改动 LibraryC 接口的相关代码,这时候编译只需要单独编译LibraryA模块就行, 如果使用的是api或者旧时代的compile,由于App Module 也可以访问到 LibraryC,所以 App Module部分也需要重新编译。当然这是在全编的情况下。

写法

单个依赖

dependencies {
    //写法1
    compile group: 'ch.qos.logback', name: 'logback-classi', version: '1.2.3'
    //写法2
    compile "ch.qos.logback:logback-classic:1.2.3"
}

多个依赖

dependencies {
    //写法1
    compile "javax.servlet:javax.servlet-api:3.1-b07"
    compile "org.slf4j:slf4j-log4j12:1.7.5"
    //写法2
    compile (  
        "javax.servlet:javax.servlet-api:3.1-b07",  
        "org.slf4j:slf4j-log4j12:1.7.5"
    )  
}

工程依赖

同一个构建中可以建立工程依赖,一个工程的 jar 包可以提供给另外一个工程使用。例如我们可以让 api 工程以依赖于 shared 工程的 jar 包。这样 Gradle 在构建 api 之前总是会先构建 shared 工程。

api工程的build.gradle文件:

dependencies {
    compile project(':shared')
}

文件夹下的jar依赖

可以依赖指定文件夹下的jar文件,如,依赖lib目录下所有的jar文件

dependencies {
    implementation fileTree(dir:'lib',includes:['*.jar'])
}

buildscript{}

简介

        gradle在执行脚本时,会优先执行buildscript代码块中的内容,然后才会执行剩余的build脚本。

        如果你想在脚本中使用一些第三方的插件、类库等,就需要自己手动添加对这些插件、类库的 引用。而这些插件、类库又不是直接服务于项目的,而是支持其它build脚本的运行。所以你应当将这部分的引用放置在buildscript代码块中。

使用场景

allprojects{}

subprojects{}

configurations{}

artifacts{}

sourceSets{}

publishing{}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章