gradle鉴权脚本
String ANDROID_PUBLIC_MAVEN_URL = 'xxx'
String MAVEN_USERNAME = 'xxx'
String MAVEN_PASSWORD = 'xxx'
def setMavenCredentials = { MavenArtifactRepository repo ->
repo.setAllowInsecureProtocol(true)
repo.credentials {
username = MAVEN_USERNAME
password = MAVEN_PASSWORD
}
repo.authentication {
basic(BasicAuthentication)
}
}
def String fixedUrl(String url) {
url = url.trim()
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1)
}
return url
}
def setAndroidPublicMaven = { RepositoryHandler repositoryHandler ->
repositoryHandler.maven { MavenArtifactRepository repo ->
repo.url(ANDROID_PUBLIC_MAVEN_URL)
setMavenCredentials(repo)
}
repositoryHandler.all { ArtifactRepository repo ->
if (repo.class.name == 'org.gradle.plugin.use.internal.PluginArtifactRepository') {
try {
repo = repo.delegate
} catch (Throwable ignored) {
}
}
if (repo instanceof MavenArtifactRepository) {
URI uri = repo.url
if (uri != null) {
String url = uri.normalize().toString()
if (fixedUrl(url) == fixedUrl(ANDROID_PUBLIC_MAVEN_URL)) {
setMavenCredentials(repo)
}
}
}
}
}
gradle.beforeSettings { Settings settings ->
settings.pluginManagement { pluginManagementSpec ->
setAndroidPublicMaven(pluginManagementSpec.repositories)
}
// 设置构建脚本的 buildscript 部分的仓库
setAndroidPublicMaven(settings.buildscript.repositories)
try {
// 用于配置依赖解析管理
settings.dependencyResolutionManagement { dependencyManagementSpec ->
setAndroidPublicMaven(dependencyManagementSpec.repositories)
}
} catch(Exception e) {
// 低版本gradle不支持dependencyResolutionManagement
}
}
gradle.beforeProject { Project project ->
setAndroidPublicMaven(project.buildscript.repositories)
setAndroidPublicMaven(project.repositories)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
上次更新: 2023/10/29, 17:37:28