欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

build.gradle里dependencies标签页的实现原理 GradleSAP云平台SAP Cloud PlatformABAPGroovy 

程序员文章站 2022-04-01 17:33:14
...

build.gradle里的dependencies标签页:

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

如果把dependencies改成dependencies2, gradle build的输出会遇到错误消息:

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

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下面:

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

打开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是一个接口:

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

里面定义了dependencies这个方法:

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

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

将dependencies标签页里的implementation标签随便换个名字:

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

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.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

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类的成员:

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

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

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

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

 

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy 

 

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

build.gradle里dependencies标签页的实现原理
            
    
    
        GradleSAP云平台SAP Cloud PlatformABAPGroovy