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{}

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