-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathbuild.gradle
More file actions
259 lines (228 loc) · 9.31 KB
/
Copy pathbuild.gradle
File metadata and controls
259 lines (228 loc) · 9.31 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
plugins {
id "java"
id "org.sonarqube" version "7.3.1.8318"
id 'maven-publish'
id 'com.gradleup.shadow' version '8.3.11'
id 'application'
}
// In specific scenario's (f.e. macOS with docker through colima), `which docker` works yet it fails
// when trying to run docker commands. This allows for a manual override to include a hard coded
// path instead. Example (.bash_profile): export DOCKER_PATH="/usr/local/bin/docker"
def docker = System.getenv("DOCKER_PATH") ?: "docker"
ext {
javaMainClass = "org.molgenis.emx2.RunMolgenisEmx2"
javaVersion = JavaVersion.VERSION_21
}
// Settings such as MOLGENIS_POSTGRES_URI/USER/PASS, MOLGENIS_HTTP_PORT or feature flags
// are read inside the Java code (EnvironmentProperty.getParameter). A running Gradle
// daemon does not pass command-line -D system properties to forked JVMs, so they are
// forwarded explicitly onto every forked Test/JavaExec task (test, run, dev, cleandb,
// generateTypes).
// Every key of a repo-root .env file is forwarded, read straight from disk once at
// configuration time (daemon-safe, no `source` needed), as is every MOLGENIS_ system property
// given on the command line. Precedence per key: a -D CLI system property wins; otherwise the
// .env value; otherwise nothing (Java default applies).
// Whitespace is trimmed the way JavaScript String.trim() does it, so that this parser and the
// one in apps/dev-env.js read the same keys out of the same file.
def javascriptTrim = { String text -> text.replaceAll('^[\\s\\u00A0\\uFEFF]+|[\\s\\u00A0\\uFEFF]+$', '') }
ext.molgenisDotenv = { ->
def envFile = new File(rootDir, '.env')
def result = [:]
if (envFile.exists()) {
envFile.eachLine { line ->
def trimmed = javascriptTrim(line)
if (trimmed.isEmpty() || trimmed.startsWith('#')) return
def idx = trimmed.indexOf('=')
if (idx < 0) return
def key = javascriptTrim(trimmed.substring(0, idx))
def value = javascriptTrim(trimmed.substring(idx + 1))
if (value.length() >= 2 &&
((value.startsWith('"') && value.endsWith('"')) ||
(value.startsWith("'") && value.endsWith("'")))) {
value = value.substring(1, value.length() - 1)
}
result[key] = value
}
}
result
}()
ext.forwardDotenvProps = { task ->
def commandLineKeys = System.getProperties().stringPropertyNames().findAll { it.startsWith('MOLGENIS_') }
(rootProject.molgenisDotenv.keySet() + commandLineKeys).each { key ->
def value = System.getProperty(key) ?: rootProject.molgenisDotenv[key]
if (value != null) task.systemProperty key, value
}
}
ext.resolveDotenvValue = { key ->
System.getProperty(key) ?: rootProject.molgenisDotenv[key] ?: System.getenv(key)
}
ext.applyDotenvHeap = { task ->
def heap = rootProject.ext.resolveDotenvValue('MOLGENIS_JVM_XMX')
if (heap) task.maxHeapSize = heap
}
allprojects {
if(rootProject.nyxState.releaseScope.previousVersion == rootProject.version) {
version = rootProject.nyxState.releaseScope.previousVersion + "-SNAPSHOT"
}
else {
version = rootProject.version.replace("SNAPSHOT.1","SNAPSHOT")
}
}
println "Corrected version checking for optional snapshot: " + rootProject.version
nyxPublish.dependsOn assemble
sonar {
properties {
property "sonar.scanner.skipJreProvisioning", "true"
property 'sonar.projectName', 'molgenis-emx2'
property 'sonar.projectKey', 'molgenis_molgenis-emx2'
property 'sonar.coverage.jacoco.xmlReportPaths', "${projectDir}/backend/build/reports/jacoco/jacocoMergedReport/jacocoMergedReport.xml"
}
}
tasks.withType(Test) {
maxParallelForks = Runtime.runtime.availableProcessors() / 2;
}
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
implementation project(':backend:molgenis-emx2-run')
}
shadowJar {
zip64 = true
archiveBaseName = 'molgenis-emx2'
mergeServiceFiles()
archiveVersion = project.version.replace("v","")
// Multi-release is needed for graalvm lib
manifest {
attributes(
"Main-Class": "org.molgenis.emx2.RunMolgenisEmx2",
"Multi-Release": "true"
)
}
exclude("META-INF/*.SF")
exclude("META-INF/*.DSA")
exclude("META-INF/*.RSA")
}
publishing {
repositories {
maven {
// change to point to repo later
url = "${layout.buildDirectory.get()}/repo"
}
}
}
project.ext.ghToken = project.hasProperty('ghToken') ? project.getProperty('ghToken') : System.getenv('GITHUB_TOKEN') ?: null
def imageName = 'docker.io/molgenis/molgenis-emx2'
def tagName = project.version.toString().replace("v","")
if (version.toString().endsWith('-SNAPSHOT')) {
ext.hash = 'git rev-parse --short HEAD'.execute().text.trim()
imageName = "docker.io/molgenis/molgenis-emx2-snapshot"
tagName = "${project.version.toString().replace("v","")}-${ext.hash}"
}
// write a file to pickup in deployment to use specific tags in upgrade
tasks.register('ci', WriteProperties) {
destinationFile.set(file('build/ci.properties'))
property 'TAG_NAME', tagName
}
tasks.register('dockerPrepare') {
dependsOn installDist
doLast {
def installLib = file("build/install/${project.name}/lib")
def depsDir = file("build/docker/deps")
def appDir = file("build/docker/app")
delete(depsDir, appDir)
depsDir.mkdirs()
appDir.mkdirs()
def projectVersion = project.version.toString().replace("v", "")
installLib.eachFile { f ->
def isProjectJar = f.name.contains(projectVersion)
java.nio.file.Files.copy(f.toPath(), new File(isProjectJar ? appDir : depsDir, f.name).toPath())
}
}
}
// Docker build and push tasks using Docker CLI (Gradle 9 compatible)
tasks.register('dockerBuild', Exec) {
dependsOn dockerPrepare
description = 'Build Docker image using Docker CLI'
doFirst {
logger.lifecycle("Building Docker image: ${imageName}:${tagName} and ${imageName}:latest")
}
commandLine docker, 'build',
'--build-arg', "JAR_FILE=${shadowJar.archiveFile.get().asFile.name}",
'-t', "${imageName}:${tagName}",
'-t', "${imageName}:latest",
'.'
}
tasks.register('dockerPush', Exec) {
dependsOn dockerBuild
description = 'Push Docker image to registry using Docker CLI'
doFirst {
logger.lifecycle("Pushing Docker image: ${imageName} with tags: ${tagName}, latest")
}
commandLine docker, 'push', imageName, '--all-tags'
}
tasks.register('dockerClean', Delete) {
description = 'Clean JAR files from project root directory'
delete fileTree(dir: projectDir, include: '*.jar')
}
String getGitHash() {
try {
// Use ProcessBuilder to properly inherit environment and handle git command
def process = new ProcessBuilder('git', 'rev-parse', '--short', 'HEAD')
.directory(rootProject.projectDir)
.redirectErrorStream(true)
.start()
process.waitFor()
if (process.exitValue() == 0) {
return process.inputStream.text.trim()
} else {
logger.warn("Git command failed, using 'unknown' as hash")
return "unknown"
}
} catch (Exception e) {
logger.warn("Failed to get git hash: ${e.message}, using 'unknown' as hash")
return "unknown"
}
}
application {
mainClass.set(javaMainClass)
}
tasks.withType(JavaExec).configureEach { forwardDotenvProps(it) }
tasks.named('run') { rootProject.ext.applyDotenvHeap(it) }
// Prints the realized launch configuration of the dev task so a CI step can grep it; that grep is
// the only automated exercise of the .env path, which is invisible to CI because .env is gitignored.
tasks.register('printDevConfig') {
group = 'help'
description = 'Prints the realized dev task system properties and heap without starting the application'
def devTask = project(':backend:molgenis-emx2-webapi').tasks.named('dev', JavaExec).get()
def devSystemProperties = new TreeMap<String, String>(devTask.systemProperties.collectEntries { key, value ->
[key, key.toUpperCase() =~ /PASS|SECRET|TOKEN/ ? '<HIDDEN>' : String.valueOf(value)]
})
def devHeap = devTask.maxHeapSize ?: 'unbounded'
doLast {
devSystemProperties.each { key, value -> println "molgenis.dev.systemProperty.${key}=${value}" }
println "molgenis.dev.maxHeapSize=${devHeap}"
}
}
jar {
reproducibleFileOrder = true
manifest {
attributes(
'Specification-Version': project.version.toString(),
'Implementation-Version': getGitHash(),
'Created-By': "Gradle ${gradle.gradleVersion}",
'Build-Jdk': "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
'Build-OS': "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}"
)
}
}
//task to install pre-commit hook that applies formatting
tasks.register('installPreCommitGitFormatApplyHook', Copy) {
from new File(rootProject.rootDir, 'pre-commit-format-apply')
rename 'pre-commit-format-apply', 'pre-commit'
into { new File(rootProject.rootDir, '.git/hooks') }
filePermissions {
unix(0775)
}
}