gradle - Gradle 在多项目中共享属性
问题描述
我对 Gradle 很陌生,正在尝试制作多项目。在根项目中声明所有公共库(以及它们的版本作为属性)并应用插件。
例如,root
子项目common
。
Inroot
的settings.gradle
类型:
rootProject.name = 'root'
include 'common'
Inroot
的build.gradle
类型:
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
subprojects {
buildscript {
repositories {
mavenCentral()
}
}
apply plugin: 'kotlin'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
}
现在我只想在特定的子项目中使用另一个库。在common
's中执行此操作build.gradle
:
buildscript {
dependencies {
classpath "org.jetbrains.kotlin:kotlin-allopen:$kotlin_version"
}
}
从 's 文件夹运行 gradle 命令时它工作正常,但从's 文件夹运行时root
失败并显示消息。Could not get unknown property 'kotlin_version' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler
common
我做错了什么?或者有什么办法吗?在多项目中共享库和属性的最佳实践是什么?
对我来说,它似乎common
对它的“父”项目一无所知,所有关系都在 root 的设置中定义。
解决方案
The reason why Gradle cannot resolve the property is because the project in folder common
is named commons
. This is caused by a spelling mistake in common
's settings.gradle
. This is fortunately easy to fix (common/settings.gradle
) :
rootProject.name = 'common'
Alternatively, just delete the common/settings.gradle
, it's fully optional in this case.
Consider reading the official Gradle documentation for authoring multi-project builds and the guide create multi-project builds for more information and best practices around multi-project builds.
推荐阅读
- html - 垂直对齐 li 内的文本
- python - 在python中将大整数转换为浮点数
- c# - 下载 URL 会导致旧文件
- python - 我的图形图例颜色与我的图形线颜色不匹配?
- .net - 在调用具有基本类型参数的 .NET Core Web API 控制器时,无法将派生类作为参数从角度代码发布
- wpf - 让用户在窗口外抓取窗口以调整大小
- java - java.time.format.DateTimeParseException:尝试解析时区日期时间时
- kubernetes - 如何让 Google Cloud Composer (airflow) 在不同的 kubernetes 集群上运行作业?
- python - 哪个R包提供了类似于pythons urllib的功能
- java - Java中不同程序的并发读写