-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle.kts
181 lines (162 loc) · 6.11 KB
/
build.gradle.kts
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.time.Duration
import java.net.URI
import java.net.http.HttpResponse
import java.util.Properties
plugins {
`java-library`
`maven-publish`
alias(libs.plugins.spring.deps)
alias(libs.plugins.kotlin.jvm)
}
group = "com.alesharik.spring"
version = "1.0-SNAPSHOT"
val localProperties = Properties()
if (rootProject.file("local.properties").exists()) {
localProperties.load(rootProject.file("local.properties").inputStream())
}
repositories {
mavenCentral()
}
subprojects {
apply(plugin = "java-library")
apply(plugin = "io.spring.dependency-management")
apply(plugin = "kotlin")
apply(plugin = "maven-publish")
configurations {
compileOnly {
extendsFrom(annotationProcessor.get())
}
}
repositories {
mavenCentral()
}
java {
withJavadocJar()
withSourcesJar()
}
tasks.withType<Test> {
useJUnitPlatform()
}
dependencies {
val libs = rootProject.libs
api(platform(libs.spring.boot.dependencies))
compileOnly(libs.lombok)
annotationProcessor(platform(libs.spring.boot.dependencies))
annotationProcessor(libs.lombok)
annotationProcessor(libs.spring.configuration.processor)
testImplementation(libs.spring.starter.test)
testImplementation(libs.assertj)
testImplementation(libs.mockito)
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "17"
}
}
tasks.javadoc {
if (JavaVersion.current().isJava9Compatible) {
(options as StandardJavadocDocletOptions).addBooleanOption("html5", true)
}
}
tasks.withType<GenerateMavenPom>().all {
doLast {
val file = File("$buildDir/publications/gpr/pom-default.xml")
var text = file.readText().replace("<dependencies/>", "<dependencies> </dependencies>")
val regex =
"(?s)(<dependencyManagement>.+?<dependencies>)(.+?)(</dependencies>.+?</dependencyManagement>)".toRegex()
val matcher = regex.find(text)
if (matcher != null) {
text = regex.replaceFirst(text, "")
val firstDeps = matcher.groups[2]!!.value
text = regex.replaceFirst(text, "$1$2$firstDeps$3")
}
file.writeText(text)
}
}
val token = localProperties["gpr.token"] as String? ?: System.getenv("GITHUB_TOKEN")
task("checkForExistingGithubArtifact") {
group = "publishing"
doLast {
val url =
"https://maven.pkg.github.com/alesharik/spring-baseinfra/com/alesharik/spring/${project.name}/${project.version}/${project.name}-${project.version}.pom"
val client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(20))
.build()
val request = HttpRequest.newBuilder()
.method("HEAD", HttpRequest.BodyPublishers.noBody())
.uri(URI.create(url))
.header("Authorization", "Bearer $token")
.build()
val response = client.send(request, HttpResponse.BodyHandlers.discarding()).statusCode()
if (response == 401) {
throw RuntimeException("Unauthorized gh maven user. Please provide valid token")
}
if (response == 200) {
gradle.taskGraph.allTasks.find {
it.project == project && it.name == "publishGprPublicationToGitHubPackagesRepository"
}?.enabled = false
}
}
}
task("checkForExistingCentralArtifact") {
group = "publishing"
doLast {
val url =
"https://repo1.maven.org/maven2/com/alesharik/spring/${project.name}/${project.version}/${project.name}-${project.version}.pom"
val client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(20))
.build()
val request = HttpRequest.newBuilder()
.method("HEAD", HttpRequest.BodyPublishers.noBody())
.uri(URI.create(url))
.header("Authorization", "Bearer $token")
.build()
val response = client.send(request, HttpResponse.BodyHandlers.discarding()).statusCode()
if (response == 401) {
throw RuntimeException("Unauthorized gh maven user. Please provide valid token")
}
if (response == 200) {
gradle.taskGraph.allTasks.find {
it.project == project && it.name == "publishGprPublicationToCentralRepository"
}?.enabled = false
}
}
}
publishing {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/alesharik/spring-baseinfra")
credentials(HttpHeaderCredentials::class) {
name = "Authorization"
value = "Bearer $token"
}
authentication {
create<HttpHeaderAuthentication>("header")
}
}
val user = System.getenv("CI_USERNAME")
val pass = System.getenv("CI_PASSWORD")
if (user != null && pass != null) {
maven {
name = "Central"
url = uri("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = user
password = pass
}
}
}
}
publications {
create<MavenPublication>("gpr") {
groupId = "com.alesharik.spring"
from(components["java"])
}
}
}
tasks.findByName("publishGprPublicationToGitHubPackagesRepository")?.dependsOn("checkForExistingGithubArtifact")
tasks.findByName("publishGprPublicationToCentralRepository")?.dependsOn("checkForExistingCentralArtifact")
}