build.gradle里repositories的mavenCentral实现原理解析

举报
汪子熙 发表于 2022/04/15 18:54:22 2022/04/15
【摘要】 如果把build.gradle里的mavenCentral改成mavenCentral2会发生什么事?错误消息:Could not find method mavenCentral2() for arguments [] on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositor...

如果把build.gradle里的mavenCentral改成mavenCentral2会发生什么事?


错误消息:Could not find method mavenCentral2() for arguments [] on repository container of type org.gradle.api.internal.artifacts.dsl.DefaultRepositoryHandler.

这个DefaultRepositoryHandler,到Gradle目录下搜索,找到了对应的Java文件:


DEFAULT_MAVEN_CENTRAL_REPO_NAME常量的定义在下列文件里:

DefaultBaseRepositoryFactory的createMavenCentralRepository方法内,创建Maven仓库,url为Repository.MAVEN_CENTRAL_URL:

这个常量定义在文件ArtifactRepositoryContainer.java里:

String MAVEN_CENTRAL_URL = “https://repo.maven.apache.org/maven2/”;

浏览器里可以打开这个url,里面存放着各种开发库:

因此build.gradle里下列这段配置信息,意思是告诉Gradle从Maven中央仓库获取工具库的内容,即url https://repo.maven.apache.org/maven2/

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:
- 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/maven2/commons-collections-/commons-collections/3.2.2/commons-collections-3.2.2.pom

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

https://repo.maven.apache.org/maven2/commons-collections/commons-collections/3.2.2/commons-collections-3.2.2.pom

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。