gpt4 book ai didi

gradle - 如何在 gradle 中记录项目属性?

转载 作者:行者123 更新时间:2023-12-04 03:57:46 25 4
gpt4 key购买 nike

例如:如果我有一个可以接受和使用名为 DB 的属性的 gradle 文件
用于对命名的进行测试。
gradle test将针对某些默认值运行
gradle test -PDB=asd1将针对特定实例运行

但是没有简单的方法向用户显示特定的属性被处理

作为用户,我希望它在 gradle tasks 中输出

我应该定义一些任务,例如 advancedOptions并在该任务的输出中列出这些东西?

最佳答案

我应该首先说我是从 ANT 来到 gradle 的,我的项目不是基于 Java 的,所以很可能我并不总是以最佳的 gradle 或 groovy 方式做事......

也就是说,我还想记录命令行属性,并验证那些与值一起输入的属性(如果它们只接受特定的值列表)。
我的方法是将可能的命令行属性映射到包含可能值列表的对象,第一个是默认值,其余是其他接受的值,以便可以验证在命令行上设置的属性。
这个对象还有一个描述字段,我用它来记录属性,很像任务描述字段。
除了使命令行属性默认设置为自动之外,这还允许我验证命令行属性,并在自定义“描述”任务中显示此信息以及关键任务描述(因为保留了帮助)

这是一些示例代码,它设置命令行属性信息并在构建开始验证命令行属性时运行(为简洁起见,我没有此处显示的值检查):

task setCommandLineDefaults () {
description 'set defaults for missing command line properties, and defines the possible values for each'
project.ext.cmdLineDefaults = [
_platform: new CmdLineInfo(values:["ios", "googleplay", "kyowon", "k_ios", "fuhu", "air", "awe"], description: "k_ios is for kyowon on ios - we may want to use this prop for just the OS target and have the partnerID separate down the road...")
, _debug: new CmdLineInfo(values:["true", "false"], description:"if true, sets compiler-directives so that debug code (e.g., logging) can be excluded from the compiled code")
, _showStats: new CmdLineInfo(values:["true", "false"], description: "if not set on the command line, the setting of _debug is used for the default")
, _connect: new CmdLineInfo(values:["false", "true"], description: "only is relevant for DEBUG true, looks for an IP debug connection on startup if true")
, _buildType: new CmdLineInfo(values:["dev", "adhoc", "release"], description: "(adhoc is ios only)")
, _buildLevel: new CmdLineInfo(values:["", "alpha", "beta", "release"], description: "default is value in version.properties")
...
]
// create the 'documentation' text for the properties
project.ext.cmdLineInfo = "Command line info: this list comprises the only command line settings allowed, the first item in values is the default, if more than one, then those are the only valid values"
cmdLineDefaults.each() { prop, value ->
cmdLineInfo += "\n$prop: $value"
}
// verify that any command line properties set are expected
def cmdLineProps = cmdLineDefaults.keySet() as String[]
String errStr = ""
def alts
gradle.startParameter.projectProperties.each() { prop, value ->
def values = cmdLineDefaults[prop]?.values
if (!values) {
alts = getClosestStrings(cmdLineProps, prop, 3) // this uses fuzzy string comparison to get at most 3 of the properites the user might have intended
errStr += "Command line property '${prop}' not known, did you mean one of these? $alts \n"
}
}
if (errStr) {
throw new InvalidUserDataException("$errStr ${cmdLineInfo}")
}
}

所以像这样的命令行
gradle compile -P_platfrm=ios

会返回一个错误,如:
Command line property '_platfrm' not known, did you mean one of these? [_platform]

然后我还有描述任务,它使用上面设置的属性信息以及任务描述:
task describe() {}
describe << {
// tasks to leave out of description list (as I mentioned, my project is not Java, so a lot of built in stuff is not relevant)
def excludeTasks = ["buildDependents","buildNeeded","check","checkForFatalErrors","classes","compileJava","compileTestJava","jar","javadoc","processResources","processTestResources","test","testClasses"]
project.tasks.each {
if (it.description && !excludeTasks.contains(it.name)) {
println "task ${it.name}: ${it.description}"
}
}
println "\n${cmdLineInfo}"
}

我比“任务”任务更喜欢这个,因为我的项目中有很多重复和不相关的信息,这也添加了属性描述。
希望这对某人有帮助。

关于gradle - 如何在 gradle 中记录项目属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26057124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com