-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.gradle
415 lines (366 loc) · 15.8 KB
/
build.gradle
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import org.apache.tools.ant.filters.ReplaceTokens
plugins {
id 'idea'
id "java"
id 'java-library'
id "com.github.hierynomus.license" version "0.16.1"
id "com.diffplug.spotless" version "6.19.0"
id 'com.google.cloud.tools.jib' version '3.4.0' apply false
id 'org.springframework.boot' version "${springBootVersion}"
// id 'org.graalvm.buildtools.native' version '0.9.23'
}
apply from: "config.gradle"
apply plugin: "com.diffplug.spotless"
// root关闭一些任务
gradle.taskGraph.whenReady {
tasks.each { task ->
if (task.name.startsWith('boot') ||
task.name.startsWith('jib') ||
task.name.startsWith('jar')) {
task.enabled = false
}
}
}
spotless {
java {
target project.fileTree(project.rootDir) {
include '**/*.java'
}
eclipse('4.26').configFile(rootProject.file('gradle/wrapper/formatter.xml'))
indentWithSpaces()
importOrder()
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
toggleOffOn("formatting:off", "formatting:on")
targetExclude("**/build/generated/source*/**/*.*")
}
}
// 所有项目下载仓库
allprojects {
apply plugin: "com.github.hierynomus.license"
// 编译jdk版本
java {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}
group = 'com.anyilanxin.cloud'
tasks.withType(Jar).configureEach {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
// 指定所有项目maven仓库
repositories {
mavenLocal()
maven {
content {
excludeGroupByRegex "com\\.anyilanxin.*"
excludeGroupByRegex "org\\.camunda.*"
excludeGroupByRegex "org\\.springframework.*"
}
url 'https://maven.aliyun.com/repository/central/'
}
mavenCentral()
}
//跳过测试
gradle.taskGraph.whenReady {
tasks.each { task ->
if (task.name.contains("test")) {
task.enabled = false
}
}
}
license {
header = rootProject.file('gradle/wrapper/LICENSE.txt')
headerDefinitions {
anyiee_custom_java {
firstLine = "/*"
endLine = " */"
beforeEachLine = " * "
firstLineDetectionPattern = "/\\*\$"
lastLineDetectionPattern = ".*\\*/(\\s|\\t)*\$"
allowBlankLines = false
isMultiline = true
}
}
mapping {
java = 'anyiee_custom_java'
}
includes(["**/*.java", "**/*.xml"])
}
}
// 设置git hook--start
//def os = "macos"
//if (System.properties['os.name'].contains('windows')) {
// os = "windows"
//}
//def hook = new File("$rootProject.projectDir/.git/hooks/pre-commit")
//hook.text = rootProject.file("gradle/wrapper/pre-commit-${os}").getText("utf-8")
//hook.setExecutable(true, false)
// 设置git hook--end
subprojects {
apply plugin: 'idea'
def isBootJarProject = false
def isPomProject = false
if (project.buildFile.isFile()) {
isBootJarProject = project.buildFile.collect {
return it.trim().replaceAll(" ", "")
}.findAll {
it.contains("bootJar")
}.contains("defbootJar=true")
isPomProject = project.buildFile.collect {
return it.trim().replaceAll(" ", "")
}.findAll {
it.contains("pom")
}.contains("defpom=true")
}
if (!isPomProject) {
apply plugin: "java"
// 指定编译编码
tasks.withType(JavaCompile).configureEach {
options.encoding = "UTF-8"
}
// 进行配置文件变量替换
processResources {
filteringCharset = 'UTF-8'
include('*.yml', 'META-INF/**', 'i18n/**')
// 只处理yml、yaml文件替换
filesMatching('*.yml') {
filter(
ReplaceTokens,
"tokens": [version: version, name: project.name],
"beginToken": "#@",
"endToken": "@#"
)
}
}
test {
useJUnitPlatform()
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
// 公共依赖
dependencies {
implementation platform(project(':anyi-cloud-bom'))
annotationProcessor platform(project(':anyi-cloud-bom'))
implementation platform("com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}")
implementation platform("com.alibaba.cloud:aliyun-spring-boot-dependencies:${springAlibabaVersion}")
implementation platform("org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}")
implementation platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
annotationProcessor platform("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
annotationProcessor "org.projectlombok:lombok"
annotationProcessor 'org.mapstruct:mapstruct'
annotationProcessor 'org.mapstruct:mapstruct-processor'
annotationProcessor "io.github.linpeilie:mapstruct-plus-processor"
compileOnly "org.springframework.boot:spring-boot-configuration-processor"
compileOnly "org.projectlombok:lombok"
compileOnly 'org.mapstruct:mapstruct'
compileOnly 'org.mapstruct:mapstruct-processor'
implementation "io.github.linpeilie:mapstruct-plus-spring-boot-starter"
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.springframework.boot:spring-boot-configuration-processor'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-aop'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter'
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
if (isBootJarProject) {
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel'
implementation 'io.micrometer:micrometer-registry-prometheus'
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'
implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'
implementation 'org.springframework.cloud:spring-cloud-starter-bootstrap'
}
implementation 'org.springframework.cloud:spring-cloud-starter-loadbalancer'
implementation 'org.springframework.retry:spring-retry'
implementation 'org.springframework:spring-context-indexer'
testImplementation 'org.springframework.amqp:spring-rabbit-test'
testImplementation 'org.projectlombok:lombok'
testImplementation 'org.springframework.cloud:spring-cloud-stream-test-support'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.mapstruct:mapstruct'
testImplementation 'org.mapstruct:mapstruct-processor'
}
// 是否为boot项目,boot允许编译启动jar,其余允许编译非启动jar并推送仓库
if (isBootJarProject) {
apply plugin: 'org.springframework.boot'
apply plugin: 'com.google.cloud.tools.jib'
// apply plugin: 'org.graalvm.buildtools.native'
// 定义jenkins版本一变随时apply k8s资源
def k8sRevision = '1'
def jenkinsBuildVersion = System.getenv('BUILD_NUMBER')
if (jenkinsBuildVersion) {
k8sRevision = jenkinsBuildVersion
}
// docker配置
def targetImagePrefix = rootProject.ext.docker['targetImagePrefix'].toString()
if (!targetImagePrefix.endsWith('/')) {
targetImagePrefix = targetImagePrefix + '/'
}
def dockerUsername = rootProject.ext.docker['username'].toString()
def envDockerUsername = System.getenv('DOCKER_USERNAME')
if (envDockerUsername) {
dockerUsername = envDockerUsername
}
def dockerPassword = rootProject.ext.docker['password'].toString()
def envDockerPassword = System.getenv('DOCKER_PASSWORD')
if (envDockerPassword) {
dockerPassword = envDockerPassword
}
jib {
from {
image = rootProject.ext.docker['baseImage'].toString()
auth {
username = dockerUsername
password = dockerPassword
}
}
to {
image = targetImagePrefix + project.name
tags = [version, 'latest', version + '-' + k8sRevision] as List<String>
auth {
username = dockerUsername
password = dockerPassword
}
}
extraDirectories {
paths {
path {
from = 'src/main/java'
into = '/app/classes'
includes = ['**/*.xml']
}
}
}
container {
jvmFlags = ['-Xms512m', '-Xmx2048m']
args = ['server.port=80']
ports = ['80']
format = 'OCI'
environment = ["TZ": 'Asia/Shanghai']
}
}
// 判断是否输出到统一目录
if (Boolean.parseBoolean(compileBootJarToParent)) {
tasks.withType(Jar).configureEach {
destinationDirectory = file("$project.parent.layout.buildDirectory.get()/bootJar")
}
}
tasks.register('copyXml', Copy) {
copy {
from layout.projectDirectory.dir("src/main/java")
include "**/xml/*.xml"
into layout.buildDirectory.dir("classes/java/main")
}
}
// 处理k8s deployment文件
tasks.register('k8sDeploymentCopy', Copy) {
copy {
from("docs/k8s") { include("*.yml") }
filter(ReplaceTokens, tokens: [version: version, k8sRevision: k8sRevision])
into("../build/k8s")
}
}
bootJar {
enabled = true
// 指定依赖包的路径
manifest {
attributes(
"Manifest-Version": 1.0,
'Implementation-Title': project.name,
'Implementation-Version': project.version,
'Created-By': System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')',
'Built-With': "gradle-${project.getGradle().getGradleVersion()}, groovy-${GroovySystem.getVersion()}",
'Build-Time': "${new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")}",
'Built-By': System.getProperty('user.name'),
'Built-On': "${InetAddress.localHost.hostName}/${InetAddress.localHost.hostAddress}"
)
}
}
} else {
apply plugin: 'maven-publish'
apply plugin: 'java-library'
jar {
enabled = true
archiveClassifier = '' // 去掉打包后jar文件有一个plain后缀
manifest {
attributes(
'Implementation-Title': project.name,
'Implementation-Version': project.version,
'Created-By': System.getProperty('java.version') + ' (' + System.getProperty('java.vendor') + ')',
'Built-With': "gradle-${project.getGradle().getGradleVersion()}, groovy-${GroovySystem.getVersion()}",
'Build-Time': "${new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")}",
'Built-By': System.getProperty('user.name'),
'Built-On': "${InetAddress.localHost.hostName}/${InetAddress.localHost.hostAddress}"
)
}
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForMavenPublication }
rename ".*", "pom.xml"
}
}
//打包源码
tasks.register('sourcesJar', Jar) {
from sourceSets.main.allJava
archiveClassifier = 'sources'
}
javadoc {
if (JavaVersion.current().isJava9Compatible()) {
options.addBooleanOption('html5', true)
}
options.addStringOption("charset", "UTF-8")
}
publishing {
publications {
maven(MavenPublication) {
groupId project.group
artifactId project.name
version project.version
from components.java
artifact sourcesJar
pom {
name = project.name
url = 'https://github.com/anyilanxin/anyi-cloud.git'
description = project.description
licenses {
license {
name = 'The Apache License, Version 2.0'
url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id = 'zxh'
name = 'zxh'
email = '[email protected]'
}
}
scm {
connection = 'scm:git:git://github.com:anyilanxin/anyi-cloud.git'
developerConnection = 'scm:git:ssh://github.com:anyilanxin/anyi-cloud.git'
url = 'https://anyilanxin.com'
}
withXml {
asNode().appendNode('packaging', 'jar')
asNode().appendNode('properties').appendNode('java.version', JavaVersion.VERSION_21)
}
}
}
}
repositories {
maven {
allowInsecureProtocol = true
credentials {
username = rootProject.ext.repo['username']
password = rootProject.ext.repo['password']
}
url = version.endsWith('SNAPSHOT') ? rootProject.ext.repo['snapshotsRepoUrl'] : rootProject.ext.repo['releasesRepoUrl']
}
}
}
}
}
}