-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.gradle.kts.example
90 lines (85 loc) · 2.75 KB
/
init.gradle.kts.example
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.Properties
// 外部仓库名称
val replaceName: String = "external"
// 外部仓库地址
val replaceMap = mapOf(
"${replaceName}public" to "https://mirrors.cloud.tencent.com/nexus/repository/maven-public/",
"${replaceName}central" to "https://mirrors.cloud.tencent.com/nexus/repository/maven-central/",
"${replaceName}gradlePlugin" to "https://maven.aliyun.com/repository/gradle-plugin/",
"${replaceName}spring" to "https://repo.spring.io/milestone/"
)
data class Define(val url: String? = null, val username: String? = null, val password: String? = null)
fun getPrivateRepoDefine(): Set<Define> {
fun getSeriesKey(key: String, sur: String, ad: String) = key.replace("$sur.", "$ad.")
val pSet = mutableSetOf<Define>()
val pMap = gradle.gradleUserHomeDir.listFiles()?.find {
it.name == "gradle.properties" && it.exists()
}?.let {
val p = Properties()
p.load(it.inputStream())
p
}
if (pMap == null) return pSet
pMap.forEach { (sk, sv) ->
val k = sk as? String?
val v = sv as? String?
if (k != null && k.toString().startsWith("usr.")) {
val url = pMap[getSeriesKey(k, "usr", "url")] as? String?
val pwd = pMap[getSeriesKey(k, "usr", "pwd")] as? String?
pSet += Define(url, username = v, password = pwd)
}
}
return pSet.filter { it.url != null && it.username != null && it.password != null }.toSet()
}
val privateRepoList = getPrivateRepoDefine()
fun RepositoryHandler.mappingPrivateRepoTask() {
privateRepoList.forEach {
maven {
url = uri(it.url!!)
credentials {
username = it.username
password = it.password
}
}
}
}
// 普通仓库
fun RepositoryHandler.mappingRepoTask() {
mavenLocal()
mappingPrivateRepoTask()
addExternalRepo("public")
addExternalRepo("central")
addExternalRepo("spring")
mavenCentral()
}
// 插件仓库
fun RepositoryHandler.mappingPluginRepoTask() {
mavenLocal()
mappingPrivateRepoTask()
addExternalRepo("spring")
gradlePluginPortal()
}
fun RepositoryHandler.addRepo(url: String, name: String? = null) {
val rUrl = url.removeSuffix("/")
val urlStringLists = map { (it as? MavenArtifactRepository)?.url?.toString()?.removeSuffix("/") }
if (urlStringLists.contains(rUrl)) return
else maven {
this.url = uri(url)
name?.also { this.name = it }
}
}
fun RepositoryHandler.addExternalRepo(name: String) {
val u = replaceMap["$replaceName$name"]
u?.also { addRepo(it, "$replaceName$name") }
}
beforeSettings {
pluginManagement { repositories { mappingPluginRepoTask() } }
dependencyResolutionManagement { repositories { mappingPluginRepoTask() } }
}
beforeProject {
repositories { mappingRepoTask() }
}
allprojects {
buildscript { repositories { mappingRepoTask() } }
repositories { mappingRepoTask() }
}