buildgradle里dependencies标签页的实现原理

33次阅读

共计 2655 个字符,预计需要花费 7 分钟才能阅读完成。

build.gradle 里的 dependencies 标签页:

如果把 dependencies 改成 dependencies2, gradle build 的输入会遇到谬误音讯:

A problem occurred evaluating root project ‘quickstart’.

Could not find method dependencies2() for arguments [build_a2307i03s3k13jdug3afl2lin$_run_closure3@21c69f73] on root project ‘quickstart’ of type org.gradle.api.Project.

找到这个 org.gradle.api.Project 类, 位于目录 orggradleapi 上面:

关上 Project.java, 查看对于 dependencies 的阐明:

<p>A project generally has a number of dependencies it needs in order to do its work. Also, a project generally

  • produces a number of artifacts, which other projects can use. Those dependencies are grouped in configurations, and
  • can be retrieved and uploaded from repositories. You use the {@link org.gradle.api.artifacts.ConfigurationContainer}
  • returned by {@link #getConfigurations()} method to manage the configurations. The {@link
  • org.gradle.api.artifacts.dsl.DependencyHandler} returned by {@link #getDependencies()} method to manage the
  • dependencies. The {@link org.gradle.api.artifacts.dsl.ArtifactHandler} returned by {@link #getArtifacts()} method to
  • manage the artifacts. The {@link org.gradle.api.artifacts.dsl.RepositoryHandler} returned by {@link
  • getRepositories()} method to manage the repositories.</p>

Project 是一个接口:

外面定义了 dependencies 这个办法:

这个办法的实现在哪里呢?

将 dependencies 标签页里的 implementation 标签轻易换个名字:

gradle build 出错:

A problem occurred evaluating root project ‘quickstart’.
Could not find method implementation2() for arguments [{group=commons-collections, name=commons-collections, version=3.2.2}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

阐明 Implementation 标签页的实现就位于 org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler 类里.

group 改成 group2:

build 出错:

A problem occurred evaluating root project ‘quickstart’.
Could not set unknown property ‘group2′ for DefaultExternalModuleDependency{group=’null’, name=’commons-collections’, version=’3.2.2′, configuration=’default’} of type org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency.

所以 build.gradle 文件 dependencies 里的标签对应了 DefaultExternalModuleDependency 类的实例. 前者 dependencies 属性 group, name 和 version 对应了 DefaultExternalModuleDependency 类的成员:

再把 dependenies 里的 group 属性值稍作批改, 改成一个并不存在的库文件名:

gradle build 报错:

Could not resolve all files for configuration ‘:compileClasspath’.
Could not find commons-collections-:commons-collections:3.2.2.

 Searched in the following locations:
   - https://repo.maven.apache.org/maven2/commons-collections-/commons-collections/3.2.2/commons-collections-3.2.2.pom
 If the artifact you are trying to retrieve can be found in the repository but without metadata in 'Maven POM' format, you need to adjust the 'metadataSources {...}' of the repository declaration.
 Required by:
     project :

从错误信息的 url 能看出 gradle build 下载依赖的地址:https://repo.maven.apache.org…

如果把 url 里多余的 - 去掉, 在浏览器里即可失常拜访:

https://repo.maven.apache.org…

要获取更多 Jerry 的原创文章,请关注公众号 ” 汪子熙 ”:

正文完
 0