-
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathbuild.gradle.kts
More file actions
358 lines (315 loc) · 14.6 KB
/
build.gradle.kts
File metadata and controls
358 lines (315 loc) · 14.6 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import io.github.hylexus.jt.gradle.utils.JtFrameworkConfig.jtFrameworkConfig
import io.github.hylexus.jt.gradle.utils.logInfo2
import io.github.hylexus.jt.gradle.utils.logTip
plugins {
id("java-library")
id("io.spring.dependency-management")
id("maven-publish")
id("io.gitee.pkmer.pkmerboot-central-publisher") apply false
id("signing")
id("checkstyle")
}
printCompatibilityMatrix()
group = jtFrameworkConfig.projectGroupId
version = jtFrameworkConfig.projectVersion
run {
jtFrameworkConfig.checkStyleTaskEnabled
jtFrameworkConfig.debugModulefatJarEnabled
jtFrameworkConfig.needSign
}
val mavenRepoConfig = jtFrameworkConfig.mavenRepoConfig
configure(allprojects) {
group = jtFrameworkConfig.projectGroupId
version = jtFrameworkConfig.projectVersion
}
// region Java
configure(subprojects) {
version = jtFrameworkConfig.projectVersion
if (!isJavaProject()) {
return@configure
}
logInfo2("configuring project: ${project.name}")
apply(plugin = "java-library")
java {
sourceCompatibility = obtainJavaVersion()
targetCompatibility = obtainJavaVersion()
}
tasks.test {
useJUnitPlatform()
// https://github.com/gradle/gradle/issues/7773
// systemProperties(System.getProperties().map { (k, v) -> k.toString() to v }.toMap())
}
tasks.withType<JavaCompile> {
sourceCompatibility = obtainJavaVersion().majorVersion
targetCompatibility = obtainJavaVersion().majorVersion
options.encoding = "UTF-8"
options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:-options", "-Xlint:deprecation", "-parameters"))
options.release.set(obtainJavaVersion().majorVersion.toInt())
}
tasks.compileTestJava {
sourceCompatibility = obtainJavaVersion().majorVersion
targetCompatibility = obtainJavaVersion().majorVersion
options.encoding = "UTF-8"
options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:-options", "-Xlint:deprecation", "-parameters"))
}
tasks.javadoc {
val docletOptions = options as StandardJavadocDocletOptions
description = "Generates project-level javadoc for use in -javadoc jar"
docletOptions.encoding = "UTF-8"
docletOptions.memberLevel = JavadocMemberLevel.PROTECTED
docletOptions.author(true)
docletOptions.header = project.name
docletOptions.use(true)
docletOptions.addStringOption("Xdoclint:none", "-quiet")
docletOptions.charSet("UTF-8")
docletOptions.addStringOption("charset", "UTF-8")
docletOptions.version(true)
docletOptions.links("https://docs.oracle.com/en/java/javase/11/docs/api")
docletOptions.source = obtainJavaVersion().majorVersion
docletOptions.addBooleanOption("html5", true)
isFailOnError = false
version = jtFrameworkConfig.projectVersion
logging.captureStandardError(LogLevel.INFO)
logging.captureStandardOutput(LogLevel.INFO)
}
// region DependencyManagement
apply(plugin = "io.spring.dependency-management")
dependencyManagement {
resolutionStrategy {
cacheChangingModulesFor(0, TimeUnit.SECONDS)
}
applyMavenExclusions(false)
generatedPomCustomization {
enabled(false)
}
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${obtainSpringBootBomVersion()}")
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${obtainSpringCloudBomVersion()}")
mavenBom("io.github.hylexus.xtream:xtream-codec-dependencies:0.5.0-rc.1")
}
dependencies {
// 其他依赖版本都由上面的 mavenBom 控制
// 这里指定 mavenBom 中没有包含的依赖版本
dependency("org.hibernate:hibernate-validator:8.0.0.Final")
dependency("io.github.hylexus.oaks:oaks-common-utils:1.0.7") {
exclude("org.projectlombok:lombok")
}
dependency("com.github.sarveswaran-m:util.concurrent.blockingMap:0.91")
dependency("com.google.guava:guava:31.1-jre")
dependency("com.google.code.findbugs:jsr305:3.0.2")
dependency("com.google.code.findbugs:annotations:3.0.1")
dependency("javax.annotation:javax.annotation-api:1.3.2")
dependency("org.apache.commons:commons-collections4:4.4")
dependency("org.bouncycastle:bcprov-jdk18on:1.78.1")
dependency("org.jspecify:jspecify:1.0.0")
dependency("org.jetbrains:annotations:26.0.2")
}
group = jtFrameworkConfig.projectGroupId
version = jtFrameworkConfig.projectVersion
}
// endregion DependencyManagement
// region checkstyle
// 严重影响构建时间
if (jtFrameworkConfig.checkStyleTaskEnabled) {
apply(plugin = "checkstyle")
checkstyle {
toolVersion = "10.23.0"
configDirectory.set(rootProject.file("build-script/checkstyle/"))
}
tasks.withType<Checkstyle> {
enabled = jtFrameworkConfig.checkStyleTaskEnabled
}
// tasks.withType<Checkstyle>().configureEach {
// javaLauncher = javaToolchains.launcherFor {
// languageVersion.set(JavaLanguageVersion.of(project.obtainJavaVersion().majorVersion))
// }
// }
} else {
logTip("\tDisabling task [checkstyle] in project [${project.name}] (jt-framework.backend.build.checkstyle.enabled == false)")
}
// endregion checkstyle
// region CommonDependencies
dependencies {
if (jtFrameworkConfig.checkStyleTaskEnabled) {
// 最后一个支持 java8 的版本: 9.3
checkstyle("com.puppycrawl.tools:checkstyle:9.3")
}
if (needLombok()) {
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
testCompileOnly("org.projectlombok:lombok")
testAnnotationProcessor("org.projectlombok:lombok")
}
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation("org.mockito:mockito-core")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
api("org.jspecify:jspecify")
api("org.jetbrains:annotations")
}
// endregion CommonDependencies
}
// endregion Java
// region Maven
configure(subprojects) {
if (!isJavaProject()) {
return@configure
}
normalization {
runtimeClasspath {
ignore("META-INF/MANIFEST.MF")
}
}
tasks.jar {
manifest {
manifest.attributes["Implementation-Title"] = project.name
manifest.attributes["Implementation-Version"] = jtFrameworkConfig.projectVersion
manifest.attributes["Automatic-Module-Name"] = project.name.replace('-', '.')
// manifest.attributes["Created-By"] = "${System.getProperty("java.version")} (${System.getProperty("java.specification.vendor")})"
manifest.attributes["Created-By"] = "${System.getProperty("java.version")} (${System.getProperty("java.vendor")})"
manifest.attributes["Minimum-Jdk-Version"] = obtainJavaVersion().majorVersion
manifest.attributes["X-Requires-Java-Version"] = obtainJavaVersion().majorVersion
}
from(rootProject.projectDir) {
include("LICENSE")
include("NOTICE")
into("META-INF")
rename("LICENSE", "LICENSE.txt")
}
}
val sourcesJar by tasks.registering(Jar::class) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
archiveClassifier.set("sources")
from(sourceSets.getByName("main").java.srcDirs)
}
val javaDocJar by tasks.registering(Jar::class) {
archiveClassifier.set("javadoc")
from(tasks.named("javadoc"))
}
if (isMavenPublication()) {
val stagingRepositoryPath = jtFrameworkConfig.centralPortalArtifactsTempDir
apply(plugin = "maven-publish")
if (jtFrameworkConfig.centralPortalMavenRepoEnabled) {
apply(plugin = "io.gitee.pkmer.pkmerboot-central-publisher")
tasks.withType<io.gitee.pkmer.tasks.BundleTask>().configureEach {
dependsOn(tasks.test, tasks.checkstyleTest, tasks.checkstyleMain)
// 只有部分模块有这两个任务
dependsOn(tasks.matching { it.name in setOf("compileJmhJava", "checkstyleJmh") })
}
// 延迟配置,在插件完全应用后再执行
afterEvaluate {
project.extensions.findByType<io.gitee.pkmer.extension.PkmerBootPluginExtension>()?.apply {
sonatypeMavenCentral {
stagingRepository.set(file(stagingRepositoryPath))
username.set(mavenRepoConfig.getProperty("maven-central-portal.username"))
password.set(mavenRepoConfig.getProperty("maven-central-portal.password"))
publishingType.set(io.gitee.pkmer.enums.PublishingType.USER_MANAGED)
}
}
}
}
publishing {
publications {
create<MavenPublication>("maven") {
groupId = jtFrameworkConfig.projectGroupId
artifactId = project.name
version = jtFrameworkConfig.projectVersion
from(components["java"])
artifact(sourcesJar)
artifact(javaDocJar)
pom {
name.set(project.name)
packaging = "jar"
description.set(project.name)
url.set(jtFrameworkConfig.projectScmUrl)
licenses {
license {
name.set(jtFrameworkConfig.projectLicenseName)
url.set(jtFrameworkConfig.projectLicenseUrl)
distribution.set(jtFrameworkConfig.projectLicenseDistribution)
}
}
scm {
url.set(jtFrameworkConfig.projectScmUrl)
connection.set(jtFrameworkConfig.projectScmConnection)
developerConnection.set(jtFrameworkConfig.projectScmDeveloperConnection)
}
developers {
developer {
id.set(jtFrameworkConfig.projectDeveloperId)
name.set(jtFrameworkConfig.projectDeveloperName)
email.set(jtFrameworkConfig.projectDeveloperEmail)
}
}
issueManagement {
system.set(jtFrameworkConfig.projectIssueManagementSystem)
url.set(jtFrameworkConfig.projectIssueManagementUrl)
}
versionMapping {
usage("java-api") {
fromResolutionOf("runtimeClasspath")
}
usage("java-runtime") {
fromResolutionResult()
}
}
}
repositories {
// 1. 发布到你自己的私有仓库
if (jtFrameworkConfig.privateMavenRepoEnabled) {
maven {
name = "private"
url = uri(mavenRepoConfig.getProperty("privateRepo-release.url"))
credentials {
username = mavenRepoConfig.getProperty("privateRepo-release.username")
password = mavenRepoConfig.getProperty("privateRepo-release.password")
}
}
}
// 2. 发布到 GitHub Packages
if (jtFrameworkConfig.githubMavenRepoEnabled) {
maven {
name = "GitHubPackages"
url = uri(mavenRepoConfig.getProperty("github-pkg-jt-framework.url"))
credentials {
username = System.getenv("GITHUB_ACTOR")
?: System.getProperty("gpr.user")
?: mavenRepoConfig.getProperty("github-pkg-jt-framework.username")
password = System.getenv("GITHUB_TOKEN")
?: System.getProperty("gpr.key")
?: mavenRepoConfig.getProperty("github-pkg-jt-framework.password")
}
}
}
// 3. 发布到 Maven 中央仓库
// 已废弃: 新版中央仓库发版参考 io.gitee.pkmer.pkmerboot-central-publisher
// maven {
// name = "centralPortal"
// url = uri(mavenRepoConfig.getProperty("sonatype-staging.url"))
// credentials {
// username = mavenRepoConfig.getProperty("sonatype-staging.username")
// password = mavenRepoConfig.getProperty("sonatype-staging.password")
// }
// }
maven {
name = "centralPortalLocalArtifacts"
// Specify the local staging repo path in the configuration.
url = uri(stagingRepositoryPath)
}
}
}
}
}
if (jtFrameworkConfig.needSign) {
apply(plugin = "signing")
signing {
// 如果需要签名
// 记得将 build-script/gradle/debug-template.gradle.properties 中的 gpg 配置放到 ~/.gradle/gradle.properties
sign(publishing.publications["maven"])
}
}
}
}
// endregion Maven
fun Project.obtainJavaVersion(): JavaVersion = project.compatibilityDefinition().jdkVersion
fun Project.obtainSpringBootBomVersion(): String = project.compatibilityDefinition().springBootVersion
fun Project.obtainSpringCloudBomVersion(): String = project.compatibilityDefinition().springCloudVersion