-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.gradle
More file actions
173 lines (151 loc) · 6.3 KB
/
build.gradle
File metadata and controls
173 lines (151 loc) · 6.3 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
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath 'org.gretty:gretty:5.0.1'
}
}
ext {
projectVersion = '5.0.1'
versions = [
spring : '7.0.3',
jakartaServlet : '6.1.0',
jakartaJstl : '3.0.1',
logback : '1.5.25',
jackson : '2.21.0',
jacksonAnnotations : '2.21',
httpClient : '5.6',
junit : '6.0.2',
junitPlatform : '6.0.2'
]
}
subprojects {
group = 'Loop54'
version = rootProject.ext.projectVersion
repositories {
mavenLocal()
mavenCentral()
}
// Apply Java configuration to projects that use the java plugin
plugins.withType(JavaPlugin) {
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
test {
useJUnitPlatform()
}
tasks.named('build') {
dependsOn rootProject.tasks.named('checkVersionHeaders')
}
}
}
tasks.register("generateReadme") {
inputs.file("README.template.md")
inputs.file("build.gradle")
outputs.file("README.md")
doLast {
// Read version from build.gradle to get the current value (in case setVersion just updated it)
def buildContent = file("build.gradle").text
def versionMatch = buildContent =~ /projectVersion\s*=\s*'([^']*)'/
def currentVersion = versionMatch ? versionMatch[0][1] : projectVersion
def template = file("README.template.md").text
file("README.md").text = template.replace("{{VERSION}}", currentVersion)
}
}
def resolveVersionHeadersFile = {
fileTree(rootDir).matching {
include "**/VersionHeaders.java"
exclude "**/build/**"
exclude "**/test/**"
}.singleFile
}
def libVersionPattern = /LIB_VERSION\s*=\s*"Java:[^"]*"/
tasks.register("setVersion") {
description = "Sets the project version. Usage: ./gradlew setVersion -PnewVersion=x.y.z"
finalizedBy generateReadme
doLast {
if (!project.hasProperty('newVersion')) {
throw new GradleException("Missing required property 'newVersion'. Usage: ./gradlew setVersion -PnewVersion=x.y.z")
}
def newVersion = project.property('newVersion')
def buildFile = file("build.gradle")
def content = buildFile.text
def updated = content.replaceFirst(/projectVersion\s*=\s*'[^']*'/, "projectVersion = '${newVersion}'")
buildFile.text = updated
def versionHeadersFile = resolveVersionHeadersFile()
versionHeadersFile.text = versionHeadersFile.text.replaceFirst(libVersionPattern, "LIB_VERSION = \"Java:${newVersion}\"")
// Update ext property for generateReadme task
ext.projectVersion = newVersion
println "Version updated to ${newVersion}"
}
}
tasks.register("checkVersionHeaders") {
description = "Check that LIB_VERSION in VersionHeaders.java matches projectVersion in build.gradle"
doLast {
def versionHeadersFileContent = resolveVersionHeadersFile().text
def match = versionHeadersFileContent =~ libVersionPattern
if (!match) {
throw new GradleException("Could not find LIB_VERSION in VersionHeaders.java")
}
def libVersion = (versionHeadersFileContent =~ /LIB_VERSION\s*=\s*"Java:([^"]*)"/)[ 0][1]
if (libVersion != projectVersion) {
throw new GradleException("Version mismatch: LIB_VERSION is 'Java:${libVersion}' but projectVersion is '${projectVersion}'. " +
"Run: ./gradlew setVersion -PnewVersion=${projectVersion}")
}
println "Version check passed: ${projectVersion}"
}
}
tasks.register("checkRemoteArtifacts") {
description = "Checks via ssh if artifacts already exist on deploy server. Make sure the system running this task has a respective ssh key. " +
"Usage: ./gradlew checkRemoteArtifacts -PdeployHost=... -PdeployUser=... -PdeployPath=..."
doLast {
def requiredProps = ['deployHost', 'deployUser', 'deployPath']
def missingProps = requiredProps.findAll { !project.hasProperty(it) }
if (missingProps) {
throw new GradleException("Missing required properties: ${missingProps.join(', ')}.\nUsage: ./gradlew checkRemoteArtifacts -PdeployHost=... -PdeployUser=... -PdeployPath=...")
}
def host = project.property('deployHost')
def user = project.property('deployUser')
def basePath = project.property('deployPath')
def version = projectVersion
def subprojectsToCheck = ['core', 'spring']
def existingArtifacts = []
subprojectsToCheck.each { subprojectName ->
def subproj = project(":${subprojectName}")
def archivesName = subproj.base.archivesName.get()
def jars = ["${archivesName}-${version}.jar", "${archivesName}-all-${version}.jar"]
jars.each { jar ->
def remotePath = "${basePath}/${subprojectName}/${jar}"
def sshCommand = [
'ssh',
'-o', 'BatchMode=yes',
'-o', 'ConnectTimeout=10',
"${user}@${host}",
'test', '-f', remotePath
]
def process = sshCommand.execute()
def exitCode = process.waitFor()
if (exitCode == 255) {
def stderr = process.errorStream.text
throw new GradleException("SSH connection failed (make sure the running system provides a valid ssh key): " +
"${stderr ?: 'Could not connect to ' + host}")
}
if (exitCode == 0) {
existingArtifacts << "${subprojectName}/${jar}"
}
}
}
if (existingArtifacts) {
throw new GradleException(
"ERROR: The following artifacts already exist on the deploy server:\n" +
existingArtifacts.collect { " - ${it}" }.join('\n') + "\n\n" +
"Please increment the version before deploying:\n" +
" ./gradlew setVersion -PnewVersion=x.y.z"
)
}
println "OK: No existing artifacts found for version ${version}"
}
}