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

时间:2014-09-26 10:10:28

标签: gradle

例如:如果我有一个可以接受并使用名为DB的属性的gradle文件 用于测试命名的那个。

gradle test将针对某些默认

运行

gradle test -PDB=asd1将针对特定实例运行

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

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

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

4 个答案:

答案 0 :(得分:2)

我应该首先说我是从ANT开始学习的,而我的项目不是基于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}"
}

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

答案 1 :(得分:1)

目前还没有很好的方法让房产自我记录。但这是一个很好的问题。您是否介意在Gradle forum上将此问题作为一个想法发布?这肯定是Gradle核心有用的东西。目前,用户将不得不依赖项目的文档。

答案 2 :(得分:0)

我认为您可以使用任务rules(但我不确定它如何与gradle tasks)一起使用,或者如果可能的测试环境准备列表并为每个位置创建任务。< / p>

伪代码:

def envs = ['e1', 'e2', 'e3']

envs.each { e ->
   projects.task(e) << {
       //TODO
   }
}

第二个想法将打印所有任务的列表。

答案 3 :(得分:0)

您可以在任务说明中“记录”。

task test {
    description = "Does stuff against thing declared by property 'db'. Ex: gradle test -Ddb=foo'
}

运行gradle tasks时,这将显示在任务名称旁边。

通过CLI将参数传递给任务的另一种方法是使用Gradle的@Option注释,但这是一个可能会更改的内部API。