我懂了:
repositories {
maven.url 'http://example.com/artifactory/libs-release'
maven.url 'http://example.com/artifactory/libs-snapshot'
}
和
repositories {
maven { url 'http://example.com/artifactory/libs-release' }
maven { url 'http://example.com/artifactory/libs-snapshot' }
}
两者都可以。
According to documentation
MavenArtifactRepository
具有url
属性。接下来是什么-对
url
值的方法调用或对属性 setter 的调用: maven.url 'http://example.com/artifactory/libs-release'
接下来是什么(我猜它正在调用
MavenArtifactRepository
构造函数): maven { url 'http://example.com/artifactory/libs-release' }
最佳答案
首先,让我们分析url
属性访问(在两个示例中):
它是方法调用还是属性 setter 的调用?好吧,两者都有。让我们引用Mr. Hakis blog:
Gradle will create a proxy class for the actual class implementation and adds (among other things) also a property setter method. The method has the name of the property and has a single argument of the same type as the property. It is different from the
setProperty
andgetProperty
methods already added by Groovy.
这是纯语法糖,支持结合配置和脚本编写的Gradle感觉。如果您认为它具有误导性,则只需使用赋值运算符(由于Groovy,它也将使用setter)。
现在,那
maven
部分呢?它看起来像
repositories
的属性。但是,如果我们查看其类型 RepositoryHandler
的定义,则会检测到它是一种创建新 MavenArtifactRepository
的方法,该方法接受闭包或用于配置的操作(对于Java使用)。这仅说明了第二个示例,因为我们传递了一个闭包,然后该闭包配置了
url
属性。 Groovy允许跳过括号。第一个例子比较棘手。我们仍然使用相同的方法,但是这次我们给出一个空的配置闭包作为参数。该方法仍然返回一个新的
MavenArtifactRepository
,我们访问其url
属性。
关于gradle - 为maven.url解释Gradle DSL魔术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43536262/