ITPub博客

首页 > 应用开发 > Java > build.gradle里dependencies标签页的实现原理

build.gradle里dependencies标签页的实现原理

原创 Java 作者:i042416 时间:2020-07-14 09:14:10 0 删除 编辑

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类,位于目录org\gradle\api下面:



打开Project.java, 查看关于dependencies的说明:

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.

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: -  repo.maven.apache.org/m  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下载依赖的地址: repo.maven.apache.org/m

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

repo.maven.apache.org/m



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


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/24475491/viewspace-2704274/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
SAP成都研究院开发专家,SAP社区导师,SAP中国技术大使。

注册时间:2018-01-20

  • 博文量
    2443
  • 访问量
    1055682