Sta*_*sev 102 dependency-management gradle
在Maven中有一个非常有用的功能,您可以<dependencyManagement>
在父POM 的部分中定义依赖项,并在不指定版本或范围或其他任何内容的情况下从子模块引用该依赖项.
Gradle有哪些替代方案?
Pet*_*ser 169
您可以在父脚本中声明公共依赖项:
ext.libraries = [ // Groovy map literal
spring_core: "org.springframework:spring-core:3.1",
junit: "junit:junit:4.10"
]
Run Code Online (Sandbox Code Playgroud)
从子脚本中,您可以使用依赖性声明,如下所示:
dependencies {
compile libraries.spring_core
testCompile libraries.junit
}
Run Code Online (Sandbox Code Playgroud)
要使用高级配置选项共享依赖性声明,您可以使用DependencyHandler.create
:
libraries = [
spring_core: dependencies.create("org.springframework:spring-core:3.1") {
exclude module: "commons-logging"
force = true
}
]
Run Code Online (Sandbox Code Playgroud)
可以使用相同的名称共享多个依赖项:
libraries = [
spring: [ // Groovy list literal
"org.springframework:spring-core:3.1",
"org.springframework:spring-jdbc:3.1"
]
]
Run Code Online (Sandbox Code Playgroud)
dependencies { compile libraries.spring }
然后将立即添加两个依赖项.
您无法以这种方式共享的一条信息是应该为其分配依赖项的配置(Maven术语中的范围).但是,根据我的经验,最好还是要明确这一点.
这是一个迟到的回复,但您可能还想查看:http://plugins.gradle.org/plugin/io.spring.dependency-management 它提供了导入maven'bom'的可能性,并重用了定义在'bom'中定义.当逐渐从maven迁移到gradle时,这肯定是一个很好的帮助!现在享受吧.
正如datta在他们的回答中所说,Gradle 现在有一个叫做版本目录的东西。\n这里是Kotlin DSL
(*.kts 文件)的示例。请注意,我使用的是 Gradle 7.4。
在settings.gradle.kts中定义依赖项:
\n// Configure dependencies aspects applied to all projects\ndependencyResolutionManagement {\n // By default, repositories declared by a project will override the ones here.\n // You can change this behavior with the repositoriesMode property.\n repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)\n\n // Define repositories for all projects\n repositories {\n mavenCentral()\n maven("https://jitpack.io")\n }\n\n versionCatalogs {\n create("libs") {\n // Versions are useful specially when you have libraries with the same\n // group and version which are updated together with the same version\n version("room", "2.4.1")\n // \xe2\x94\x82 \xe2\x94\x82\n // \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> The version notation\n // \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> Your desired name (alias)\n \n library("material", "com.google.android.material:material:1.4.0")\n // \xe2\x94\x82 \xe2\x94\x82\n // \xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> The dependency notation (coordinates)\n // \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> Your desired name (alias); only letters, digits and _ - .\n // \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80> Note that _ - . will all be normalized to .\n \n // You can configure the version as you would in a regular build file\n // Note that the group and module are separate parameters\n library("junit5", "org.junit.jupiter", "junit-jupiter").version {\n prefer("5.8.0")\n }\n \n // Using the same version for multiple dependencies\n library("room-ktx", "androidx.room", "room-ktx").versionRef("room")\n library("room-runtime", "androidx.room", "room-runtime").versionRef("room")\n }\n }\n}\n
Run Code Online (Sandbox Code Playgroud)\nbuild.gradle[.kts]中的用法:
\ndependencies {\n implementation(libs.material)\n implementation(libs.room.ktx)\n implementation(libs.room.runtime)\n testImplementation(libs.junit5)\n}\n
Run Code Online (Sandbox Code Playgroud)\n正如您所看到的,您不仅可以声明依赖项,还可以在此处声明存储库(而不是allprojects
在顶级构建脚本中使用 block 来为所有子项目定义存储库)。
有关上述解决方案的常规语法以及有关版本目录和集中存储库和依赖项配置的更多信息,请参阅Gradle 官方指南。
\n从Gradle 4.6开始,文档中建议使用依赖项约束来实现此目的。来自https://docs.gradle.org/current/userguide/declaring_dependencies.html#declaring_a_dependency_without_version:
对于较大的项目,建议的做法是声明没有版本的依赖项,并将依赖项约束用于版本声明。优点是,依赖关系约束使您可以在一处管理所有依赖关系的版本,包括可传递的依赖关系。
在您的父build.gradle
文件中:
allprojects {
plugins.withType(JavaPlugin).whenPluginAdded {
dependencies {
constraints {
implementation("com.google.guava:guava:27.0.1-jre")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
whenPluginAdded {
不必严格检查Java插件(... )来包装依赖项块,但是它将处理将非Java项目添加到同一内部版本。
然后,在儿童gradle项目中,您可以简单地省略版本:
apply plugin: "java"
dependencies {
implementation("com.google.guava:guava")
}
Run Code Online (Sandbox Code Playgroud)
子版本仍可以选择指定更高版本。如果指定了较低的版本,它将自动升级到约束中的版本。
归档时间: |
|
查看次数: |
30128 次 |
最近记录: |