Bài viết phổ biến của tác giả
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个类来帮助我加载不同类型的属性(local.properties
,gradle.properties
,$GRADLE_HOME/gradle.properties
,环境变量,系统属性和自定义属性文件(也许采用yml
,xml
等其他格式)。
另外,我想在buildSrc/build.gradle.kts
,settings.gradle.kts
Vàbuild.gradle.kts
中使用它。
请考虑我们正在使用Gradle 6.+
.
此类的一个简单实现是(完整实现将更加强大):
plugins/properties/build.gradle.kts:
package com.example
object Properties {
val environmentVariables = System.getenv()
}
Properties
,
buildSrc/build.gradle.kts
,
settings.gradle.kts
)中成功导入此
build.gradle.kts
类并从那里使用它?就像是:
println(com.example.Properties.environmentVariables["my.property"])
apply("plugins/properties/build.gradle.kts")
1 Câu trả lời
我对这种方法并不完全满意,但也许可以帮助其他人。
我无法在所有地方重用一个类,而是一个函数,如下所示:
settings.gradle.kts
apply("plugin/properties/build.gradle.kts")
@Suppress("unchecked_cast", "nothing_to_inline")
inline fun uncheckedCast(target: Any?): T = target as T
val getProperty = uncheckedCast<(key: String) -> String>(extra["getProperty"])
println(getProperty("group"))
apply("../plugin/properties/build.gradle.kts")
@Suppress("unchecked_cast", "nothing_to_inline")
inline fun uncheckedCast(target: Any?): T = target as T
val getProperty = uncheckedCast<(key: String) -> String>(extra["getProperty"])
println(getProperty("group"))
// Can be used inside of the file
apply("plugin/properties/build.gradle.kts")
@Suppress("unchecked_cast", "nothing_to_inline")
inline fun uncheckedCast(target: Any?): T = target as T
val getProperty = uncheckedCast<(key: String) -> String>(extra["getProperty"])
println(getProperty("group"))
buildScript {
// Since the other getProperty is not visible here we need to do this again.
apply("plugin/properties/build.gradle.kts")
@Suppress("unchecked_cast", "nothing_to_inline")
inline fun uncheckedCast(target: Any?): T = target as T
val getProperty = uncheckedCast<(key: String) -> String>(extra["getProperty"])
println(getProperty("group"))
}
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths
import java.util.Properties as JavaProperties
import org.gradle.api.initialization.ProjectDescriptor
object Properties {
lateinit var rootProjectAbsolutePath : String
val local: JavaProperties by lazy {
loadProperties(JavaProperties(), Paths.get(rootProjectAbsolutePath, "local.properties").toFile())
}
val environment by lazy {
System.getenv()
}
val system: JavaProperties = JavaProperties()
val gradle: JavaProperties by lazy {
loadProperties(JavaProperties(), Paths.get(rootProjectAbsolutePath, "gradle.properties").toFile())
}
val globalGradle: JavaProperties by lazy {
loadProperties(JavaProperties(), Paths.get(System.getProperty("user.home"), ".gradle", "gradle.properties").toFile())
}
fun containsKey(vararg keys: String): Boolean {
if (keys.isNullOrEmpty()) return false
keys.forEach {
when {
local.containsKey(it) -> return true
environment.containsKey(it) -> return true
system.containsKey(it) -> return true
gradle.containsKey(it) -> return true
globalGradle.containsKey(it) -> return true
}
}
trả về sai
}
fun get(vararg keys: String): String {
return this.getAndCast(*keys) ?: throw IllegalArgumentException("Property key(s) ${keys} not found.")
}
fun getOrNull(vararg keys: String): String? {
return getAndCast(*keys)
}
inline fun getOrDefault(vararg keys: String, default: R?): R? {
return getAndCast(*keys) ?: default
}
inline fun getAndCast(vararg keys: String): R? {
if (keys.isNullOrEmpty()) return null
keys.forEach {
val value = when {
local.containsKey(it) -> local[it]
environment.containsKey(it) -> environment[it]
system.containsKey(it) -> system[it]
gradle.containsKey(it) -> gradle[it]
globalGradle.containsKey(it) -> globalGradle[it]
else -> null
}
// TODO Improve the casting using Jackson
if (value != null) return value as R
}
return null
}
private fun loadProperties(target: JavaProperties, file: File): JavaProperties {
if (file.canRead()) {
file.inputStream().use { target.load(it) }
}
return target
}
}
if (rootProject.name == "buildSrc") {
Properties.rootProjectAbsolutePath = rootDir.parent
} khác {
Properties.rootProjectAbsolutePath = rootDir.absolutePath
}
extra["getProperty"] = {key: String -> Properties.get(key)}
关于gradle - 如何在buildSrc/build.gradle.kts,settings.gradle.kts和build.gradle.kts中导入帮助程序类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60251228/
Tôi là một lập trình viên xuất sắc, rất giỏi!