为什么gradle任务方法名称中的大小写?

wbi*_*bit 3 groovy gradle build.gradle

这个问题中,我感到困惑,因为我认为我们可以将参数传递给没有括号的方法.实际上,您可以将参数作为逗号分隔列表传递给类似的方法:

task ListOfStrings(type: ExampleTask) {
    //TheList 'one', 'two', 'three' // doesn't work
    theList 'one', 'two', 'three'
}
public class ExampleTask extends DefaultTask {
    //public void TheList(Object... theStrings) {
    //    theStrings.each { println it }
    //}
    public void theList(Object... theStrings) {
        theStrings.each { println it }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码有效,因为方法名称是camelCase.当使用TitleCase的方法名称(上面已注释掉)时,gradle会抛出一个错误:

  build file '/tmp/build.gradle': 16: unexpected token: one @ line 16, column 13.
         TheList 'one', 'two', 'three'
                 ^
Run Code Online (Sandbox Code Playgroud)

SO,问题是,"为什么的方法名此事的情况?" 总之,导致这种行为的原因是什么?这是一个惯例吗?如果是这样,它在哪里记录?

Mar*_*ira 6

这只是Groovy编译器将任何大写符号视为类引用,而不是方法.这里有一个含糊不清的地方,您可以通过以下方式修复:

  • 使用括号 Foo('one', 'two')

要么

  • 限定方法名称this.Foo 'one', 'two'.

一般来说,约定是类是大写的而方法不是.因为Groovy是一种动态语言,所以编译器在很大程度上依赖于这些约定.