Gradle 公开了一个 API 来声明仓库可能包含或不包含的内容。它有不同的用例
-
性能 当您知道依赖项永远不会在特定仓库中找到时
-
安全性 通过避免泄露私有项目中使用的依赖项
-
可靠性 当某些仓库包含无效或不正确的元数据或工件时
当考虑到声明的仓库顺序很重要时,这一点甚至更重要。
声明仓库过滤器
Kotlin
Groovy
repositories {
maven {
url = uri("https://repo.mycompany.com/maven2")
content {
// this repository *only* contains artifacts with group "my.company"
includeGroup("my.company")
}
}
mavenCentral {
content {
// this repository contains everything BUT artifacts with group starting with "my.company"
excludeGroupByRegex("my\\.company.*")
}
}
}
默认情况下,仓库包含所有内容,不排除任何内容
-
如果您声明了包含项,则它会排除除了包含项之外的所有内容。
-
如果您声明了排除项,则它会包含除了排除项之外的所有内容。
-
如果您同时声明了包含项和排除项,则它仅包含显式包含且未排除的内容。
可以按显式的组、模块或版本进行过滤,可以是严格的,也可以使用正则表达式。当使用严格版本时,可以使用版本范围,使用 Gradle 支持的格式。此外,还可以按解析上下文进行过滤:配置名称甚至配置属性。有关详细信息,请参阅 RepositoryContentDescriptor。
声明仅在一个仓库中找到的内容
使用仓库级别内容过滤器声明的过滤器不是独占的。这意味着声明仓库包含一个工件并不意味着其他仓库也不能拥有它:您必须声明每个仓库扩展包含的内容。
或者,Gradle 提供了一个 API,允许您声明仓库独占包含一个工件。如果您这样做
-
在一个仓库中声明的工件不能在任何其他仓库中找到
-
独占仓库内容必须在扩展中声明(就像仓库级别内容一样)
Kotlin
Groovy
repositories {
// This repository will _not_ be searched for artifacts in my.company
// despite being declared first
mavenCentral()
exclusiveContent {
forRepository {
maven {
url = uri("https://repo.mycompany.com/maven2")
}
}
filter {
// this repository *only* contains artifacts with group "my.company"
includeGroup("my.company")
}
}
}
可以按显式的组、模块或版本进行过滤,可以是严格的,也可以使用正则表达式。有关详细信息,请参阅 InclusiveRepositoryContentDescriptor。
如果您在 您的选择是在 settings 中声明所有仓库,或使用非独占内容过滤。 |
Maven 仓库过滤
对于 Maven 仓库,通常情况下,仓库要么包含发布版本,要么包含快照版本。Gradle 允许您使用此 DSL 声明在仓库中找到哪种类型的工件
Kotlin
Groovy
repositories {
maven {
url = uri("https://repo.mycompany.com/releases")
mavenContent {
releasesOnly()
}
}
maven {
url = uri("https://repo.mycompany.com/snapshots")
mavenContent {
snapshotsOnly()
}
}
}