-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathbuild.sbt
296 lines (265 loc) · 11.2 KB
/
build.sbt
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
import com.typesafe.sbt.SbtNativePackager.autoImport.NativePackagerHelper.*
import com.typesafe.sbt.packager.docker.DockerPlugin.autoImport.Docker
import com.typesafe.sbt.packager.docker.DockerPlugin.autoImport.dockerRepository
import com.typesafe.sbt.packager.docker.Cmd
import com.typesafe.sbt.packager.docker.ExecCmd
import sbt.*
import sbt.Keys.version
import scala.language.postfixOps
import scala.sys.process.*
import org.knora.Dependencies
import org.knora.LocalSettings
import java.time.Instant
//////////////////////////////////////
// GLOBAL SETTINGS
//////////////////////////////////////
// when true enables run cancellation w/o exiting sbt
// use Ctrl-c to stop current task but not quit SBT
Global / cancelable := true
Global / scalaVersion := Dependencies.ScalaVersion
Global / semanticdbEnabled := true
Global / semanticdbVersion := scalafixSemanticdb.revision
val gitCommit = ("git rev-parse HEAD" !!).trim
val gitBranch = Option("git rev-parse --abbrev-ref HEAD" !!)
.map(_.trim)
.filter(b => !(b == "main" || b == "HEAD"))
.map(_.replace('/', '-'))
val gitVersion = ("git describe --tag --dirty --abbrev=7 --always " !!).trim + gitBranch.fold("")("-" + _)
ThisBuild / version := gitVersion
lazy val buildCommit = ("git rev-parse --short HEAD" !!).trim
lazy val buildTime = Instant.now.toString
lazy val aggregatedProjects: Seq[ProjectReference] = Seq(webapi, sipi, integration)
lazy val year = java.time.LocalDate.now().getYear
lazy val buildSettings = Seq(
organization := "org.knora",
version := (ThisBuild / version).value,
headerLicense := Some(
HeaderLicense.Custom(
s"""|Copyright © 2021 - $year Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
|SPDX-License-Identifier: Apache-2.0
|""".stripMargin,
),
),
)
lazy val rootBaseDir = ThisBuild / baseDirectory
lazy val dockerImageTag = taskKey[String]("Returns the docker image tag")
lazy val root: Project = Project(id = "root", file("."))
.aggregate(
webapi,
sipi,
integration,
)
.settings(
// values set for all sub-projects
// These are normal sbt settings to configure for release, skip if already defined
Global / onChangedBuildSource := ReloadOnSourceChanges,
ThisBuild / licenses := Seq("Apache License, Version 2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0")),
ThisBuild / homepage := Some(url("https://github.com/dasch-swiss/dsp-api")),
ThisBuild / scmInfo := Some(
ScmInfo(url("https://github.com/dasch-swiss/dsp-api"), "scm:git:[email protected]:dasch-swiss/dsp-api.git"),
),
Global / scalaVersion := Dependencies.ScalaVersion,
// override generated version string because docker hub rejects '+' in tags
ThisBuild / version ~= (_.replace('+', '-')),
dockerImageTag := (ThisBuild / version).value,
publish / skip := true,
name := "dsp-api",
)
addCommandAlias("fmt", "; all root/scalafmtSbt root/scalafmtAll; root/scalafixAll")
addCommandAlias(
"headerCreateAll",
"; all webapi/headerCreate webapi/Test/headerCreate integration/headerCreate integration/Test/headerCreate",
)
addCommandAlias(
"headerCheckAll",
"; all webapi/headerCheck webapi/Test/headerCheck integration/headerCheck integration/Test/headerCheck",
)
addCommandAlias("check", "; all root/scalafmtSbtCheck root/scalafmtCheckAll; root/scalafixAll --check; headerCheckAll")
addCommandAlias("it", "integration/test")
//////////////////////////////////////
// DSP's custom SIPI
//////////////////////////////////////
lazy val sipi: Project = Project(id = "sipi", base = file("sipi"))
.enablePlugins(DockerPlugin)
.settings(
Compile / packageDoc / mappings := Seq(),
Compile / packageSrc / mappings := Seq(),
Docker / dockerRepository := Some("daschswiss"),
Docker / packageName := "knora-sipi",
dockerUpdateLatest := true,
dockerBaseImage := Dependencies.sipiImage,
dockerBuildxPlatforms := Seq("linux/arm64/v8", "linux/amd64"),
Docker / maintainer := "[email protected]",
Docker / dockerExposedPorts ++= Seq(1024),
Docker / defaultLinuxInstallLocation := "/sipi",
Universal / mappings ++= {
directory("sipi/scripts")
},
dockerCommands += Cmd(
"""HEALTHCHECK --interval=30s --timeout=30s --retries=4 --start-period=30s \
|CMD bash /sipi/scripts/healthcheck.sh || exit 1""".stripMargin,
),
// use filterNot to return all items that do NOT meet the criteria
dockerCommands := dockerCommands.value.filterNot {
// ExecCmd is a case class, and args is a varargs variable, so you need to bind it with @
// remove ENTRYPOINT
case ExecCmd("ENTRYPOINT", args @ _*) => true
// remove CMD
case ExecCmd("CMD", args @ _*) => true
case Cmd("USER", args @ _*) => true
// don't filter the rest; don't filter out anything that doesn't match a pattern
case cmd => false
},
)
//////////////////////////////////////
// WEBAPI (./webapi)
//////////////////////////////////////
run / connectInput := true
lazy val webApiCommonSettings = Seq(
name := "webapi",
)
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
val customScalacOptions = Seq(
"-feature",
"-unchecked",
"-deprecation",
"-Yresolve-term-conflict:package",
"-Wconf:src=target/.*:s", // silence TWIRL templates unused imports warnings
"-Wunused:imports",
"-Wunused:privates",
"-Wunused:locals",
"-Wunused:explicits",
"-Wunused:implicits",
"-Wunused:params",
"-Wvalue-discard",
"-Xmax-inlines:64",
// "-Xfatal-warnings",
)
lazy val webapi: Project = Project(id = "webapi", base = file("webapi"))
.settings(buildSettings)
.settings(
inConfig(Test) {
Defaults.testSettings
},
Test / testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
Test / fork := true, // run tests in a forked JVM
Test / testForkedParallel := true, // run tests in parallel
Test / parallelExecution := true, // run tests in parallel
libraryDependencies ++= Dependencies.webapiDependencies ++ Dependencies.webapiTestDependencies,
)
.enablePlugins(SbtTwirl, JavaAppPackaging, DockerPlugin, JavaAgent, BuildInfoPlugin, HeaderPlugin)
.settings(
name := "webapi",
resolvers ++= Seq(
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
),
libraryDependencies ++= Dependencies.webapiDependencies ++ Dependencies.webapiTestDependencies,
)
.settings(
// add needed files to production jar
Compile / packageBin / mappings ++= Seq(
(rootBaseDir.value / "webapi" / "scripts" / "fuseki-repository-config.ttl.template") -> "webapi/scripts/fuseki-repository-config.ttl.template", // needed for initialization of triplestore
),
// use packaged jars (through packageBin) on classpaths instead of class directories for production
Compile / exportJars := true,
)
.settings(
scalacOptions ++= customScalacOptions,
logLevel := Level.Info,
javaAgents += Dependencies.aspectjweaver,
)
.settings(LocalSettings.localScalacOptions: _*)
.settings(
// prepare for publishing
// Skip packageDoc and packageSrc task on stage
Compile / packageDoc / mappings := Seq(),
Compile / packageSrc / mappings := Seq(),
// define folders inside container
Universal / mappings ++= {
// copy the scripts folder
directory("webapi/scripts") ++
// copy configuration files to config directory
contentOf("webapi/src/main/resources").toMap.mapValues("config/" + _)
},
// add 'config' directory to the classpath of the start script,
Universal / scriptClasspath := Seq(
"webapi/scripts",
"webapi/src/main/resources/knora-ontologies",
"../config/",
) ++ scriptClasspath.value,
// need this here, so that the Manifest inside the jars has the correct main class set.
Compile / mainClass := Some("org.knora.webapi.Main"),
// add dockerCommands used to create the image
// docker:stage, docker:publishLocal, docker:publish, docker:clean
Docker / dockerRepository := Some("daschswiss"),
Docker / packageName := "knora-api",
dockerUpdateLatest := true,
dockerBaseImage := "eclipse-temurin:21-jre-noble",
dockerBuildxPlatforms := Seq("linux/arm64/v8", "linux/amd64"),
Docker / maintainer := "[email protected]",
Docker / dockerExposedPorts ++= Seq(3333, 3339),
Docker / defaultLinuxInstallLocation := "/opt/docker",
dockerLabels := Map[String, Option[String]](
"org.opencontainers.image.version" -> (ThisBuild / version).value.some,
"org.opencontainers.image.revision" -> Some(gitCommit),
"org.opencontainers.image.source" -> Some("github.com/dasch-swiss/dsp-api"),
).collect { case (key, Some(value)) => (key, value) },
dockerCommands += Cmd(
"RUN",
"apt-get update && apt-get install -y jq && rm -rf /var/lib/apt/lists/*",
), // install jq for container healthcheck
dockerCommands += Cmd(
"""HEALTHCHECK --interval=30s --timeout=10s --retries=3 --start-period=30s \
|CMD bash /opt/docker/scripts/healthcheck.sh || exit 1""".stripMargin,
),
// use filterNot to return all items that do NOT meet the criteria
dockerCommands := dockerCommands.value.filterNot {
// Remove USER command
case Cmd("USER", args @ _*) => true
// don't filter the rest; don't filter out anything that doesn't match a pattern
case cmd => false
},
)
.settings(
buildInfoKeys ++= Seq[BuildInfoKey](
name,
version,
"sipi" -> Dependencies.sipiImage,
"fuseki" -> Dependencies.fusekiImage,
"pekkoHttp" -> Dependencies.pekkoHttp,
"buildCommit" -> buildCommit,
"buildTime" -> buildTime,
),
buildInfoPackage := "org.knora.webapi.http.version",
)
//////////////////////////////////////
// INTEGRATION (./integration)
//////////////////////////////////////
run / connectInput := true
lazy val integration: Project = Project(id = "integration", base = file("integration"))
.dependsOn(webapi, sipi)
.settings(buildSettings)
.settings(
inConfig(Test) {
Defaults.testSettings ++ Defaults.testTasks ++ baseAssemblySettings ++ headerSettings(Test)
},
scalacOptions ++= customScalacOptions,
logLevel := Level.Info,
javaAgents += Dependencies.aspectjweaver,
Test / testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
Test / exportJars := false,
Test / fork := true, // run tests in a forked JVM
Test / testForkedParallel := false,
Test / parallelExecution := false,
Test / javaOptions += "-Dkey=" + sys.props.getOrElse("key", "pekko"),
Test / testOptions += Tests.Argument("-oDF"), // show full stack traces and test case durations
libraryDependencies ++= Dependencies.webapiDependencies ++ Dependencies.webapiTestDependencies ++ Dependencies.integrationTestDependencies,
)
.settings(LocalSettings.localScalacOptions: _*)
.enablePlugins(SbtTwirl, JavaAppPackaging, DockerPlugin, JavaAgent, BuildInfoPlugin, HeaderPlugin)
.settings(
name := "integration",
resolvers ++= Seq(
"Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
),
)