build.gradle裏dependencies標籤頁的實現原理

build.gradle裏的dependencies標籤頁:java

若是把dependencies改爲dependencies2, gradle build的輸出會遇到錯誤消息:apache

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.api

找到這個org.gradle.api.Project類,位於目錄orggradleapi下面:瀏覽器

打開Project.java, 查看關於dependencies的說明:maven

<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是一個接口:gradle

裏面定義了dependencies這個方法:ui

這個方法的實如今哪裏呢?lua

將dependencies標籤頁裏的implementation標籤隨便換個名字:url

gradle build出錯:spa

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的原創文章,請關注公衆號"汪子熙":

相關文章
相關標籤/搜索