iT邦幫忙

2023 iThome 鐵人賽

DAY 11
0

從下面我處理完成的程式碼中,可以看到 repositories {} 區塊分別在 pluginManagement {}dependencyResolutionManagement {} 都有出現。

這時會想說,為什麼不能只宣告一次 repositories {} 就好?
只使用一個 repositories {} 來統一儲存庫來源?

同樣名稱區塊重複出現,實在是很令人混淆及困擾呢。

settings.gradle

pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
        mavenCentral()
        maven { url "https://jitpack.io/" }
        maven { url "https://jcenter.bintray.com" }
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        // APNG file for animation
        maven { url "https://jitpack.io/" }
        // 'io.github.youth5201314:banner:2.2.2' 依賴項 使用
        maven { url "https://s01.oss.sonatype.org/content/groups/public" }
        // 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1' 依賴項 使用
        maven { url "https://jcenter.bintray.com" }
    }
}

rootProject.name = "com.my.work"

include ':app'

我們先來看一則 stackoverflow 討論呈現的 repositories 多重宇宙,關於儲存庫的宣告有以下很多種方式:

  1. Cross-configuration inside allprojects {} or subprojects {} in the build.gradle.kts file

在 build.gradle(Module :app) 內使用 allprojects {} 或 subprojects {} 配置儲存庫

  1. Use the dependencyResolutionManagement {} block in the settings.gradle.kts file

在 settings.gradle 使用 dependencyResolutionManagement {} 配置儲存庫

  1. Use the pluginManagement {} block in the settings.gradle.kts file

在 settings.gradle 使用 pluginManagement {} 配置儲存庫

  1. Use the buildscript {} block

在 build.gradle(Project:專案名稱)、build.gradle(Module :app) 內使用 buildscript {} 配置儲存庫

這則討論底下有網友提供一個統整:

Generally speaking there are two different use-cases where Gradle uses repositories: dependency resolution and plugin resolution

一般來說,Gradle 在使用儲存庫時有 2 種使用方式:依賴項解決方案(dependency resolution)和插件解決方案(plugin resolution)

For the repository you look up dependencies dependencyResolutionManagement is the most modern approach and is likely the best one to start with. It is available from Gradle 6.8 onward and was specifically created to have a single place to declare your repositores in.

在 settings.gradle 中使用 dependencyResolutionManagement{} 配置儲存庫是一個最近興起的作法,也可能是最佳的起步。從 Gradle 6.8 後你便可以使用這個專門在同一處宣告儲存庫的作法。

The subprojects / allprojects approach gets you basically the same result with the difference that the values will be in-memory copy pasted onto all subprojects. However this likely only makes a difference if you use plugins that itself modify repository declarations. But I don't see any advantage either going this route in current Gradle versions.

subprojects/allprojects 基本上會得到相同的效果。不同之處在於這些值將在記憶體中複製並貼上至所有的 subprojects。然而,這只在使用會修改儲存庫宣告的插件時有所差異,但在最近的 Gradle 版本中使用 subprojects / allprojects 並沒有任何優勢。

The pluginManagement and buildscript repositories are for separate things and are not used to look up dependent libraries of your application. Instead those are used by gradle to look up libraries used for the build itself, i.e. if you delcare any Gradle Plugins that are not part of the Gradle core application they are resolved through those additional repositories.

pluginManagement 和 buildscript 儲存庫用在不同的事,而且不用在應用程式中尋找依賴庫。相反地,Gradle 使用它們來尋找構建 Gradle 自身的儲存庫。例如,如果你宣告任何不屬於 Gradle 核心程式的 Gradle 插件,這些 Gradle 插件將使用這邊宣告的額外儲存庫來解析。

However, since they are predefined to link to the Gradle Plugin portal you likely don't need to declare them at all. You could declare them if your company has any build plugins that are only available in internal repositories.

但是,它們已被預設連結至 Gradle 插件入口網頁,因此可能沒必要使用 pluginManagement 去額外宣告儲存庫。
使用 pluginManagement 去額外宣告儲存庫的時機點,會是在你們公司有使用任何僅限內部儲存庫使用的構建插件。

For completeness: pluginManagement is used for plugins declared in a plugin block while buildscript is used for libraries you import within the buildscript block (which might also be plugins)

為了完整說明:pluginManagement 的儲存庫用在 plugin 區塊中聲明的插件。而 buildscript 的儲存庫用在你引入的依賴項或插件。


上一篇
[Day 10] dependencyResolutionManagement 的作用
下一篇
[Day 12] 宣告 repositories
系列文
[Android] 怎麼蓋地基?論 build.gradle 與它的快樂夥伴們30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言