在用gradle时常规配置如下(D:\gradle-4.9\init.d\init.gradle文件,没有这个文件时自建):
ext { nexus = ‘http://192.168.127.128:8081/repository/maven-public/‘ username = ‘admin‘ password = ‘admin123‘ } allprojects{ repositories { all { ArtifactRepository repo -> if(repo instanceof MavenArtifactRepository){ def url = repo.url.toString() if (url.startsWith(‘https://repo1.maven.org/maven2‘) || url.startsWith(‘https://jcenter.bintray.com/‘) || !url.startsWith(nexus)) { project.logger.lifecycle "Repository ${repo.url} replaced by ${nexus}." remove repo } } } maven { url nexus } } }
在这种配置下,仅仅是项目中要用到的构件是从配置的nexus下载的。但是gradle自身的插件依然要从https://plugins.gradle.org/m2/下载。有时开发者机器是无法连接到互联网的,只有nexus机器能连接互联网,并不能访问这个https://plugins.gradle.org/m2/地址,也就无法下载gradle自身依赖的插件,就会报错。所以我们希望开发者的机器下载gradle插件时,也能从nexus下载。此时应在init.gradle中添加如下内容:
ext { nexus = ‘http://192.168.127.128:8081/repository/maven-public/‘ username = ‘admin‘ password = ‘admin123‘ } allprojects{ repositories { all { ArtifactRepository repo -> if(repo instanceof MavenArtifactRepository){ def url = repo.url.toString() if (url.startsWith(‘https://repo1.maven.org/maven2‘) || url.startsWith(‘https://jcenter.bintray.com/‘) || !url.startsWith(nexus)) { project.logger.lifecycle "Repository ${repo.url} replaced by ${nexus}." remove repo } } } maven { url nexus } } } settingsEvaluated { settings -> settings.pluginManagement { resolutionStrategy { } repositories { maven { url nexus } } } }
注意:
gradle学习方法就是,多看gradle文档,多思考,适当学groovy。本文参考的文档是:https://docs.gradle.org/current/userguide/plugins.html#sec:script_plugins
原文地址:https://www.cnblogs.com/dingyingsi/p/9401498.html
时间: 2024-10-10 20:31:14