Skip to content

Commit 922961f

Browse files
committed
Scala build infra
Adds build-infra related work for Scala projects, like #1208
1 parent 267acd1 commit 922961f

File tree

6 files changed

+214
-47
lines changed

6 files changed

+214
-47
lines changed

build-logic/src/main/kotlin/polaris-java.gradle.kts

+62-46
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,25 @@ apply<PublishingHelperPlugin>()
3939

4040
tasks.withType(JavaCompile::class.java).configureEach {
4141
options.compilerArgs.addAll(listOf("-Xlint:unchecked", "-Xlint:deprecation"))
42-
options.errorprone.disableAllWarnings = true
43-
options.errorprone.disableWarningsInGeneratedCode = true
44-
options.errorprone.excludedPaths =
45-
".*/${project.layout.buildDirectory.get().asFile.relativeTo(projectDir)}/generated/.*"
46-
options.errorprone.error(
47-
"DefaultCharset",
48-
"FallThrough",
49-
"MissingCasesInEnumSwitch",
50-
"MissingOverride",
51-
"ModifiedButNotUsed",
52-
"OrphanedFormatString",
53-
"PatternMatchingInstanceof",
54-
"StringCaseLocaleUsage",
55-
)
42+
43+
if (project.extra.has("reused-project-dir")) {
44+
options.errorprone.disableAllChecks = true
45+
} else {
46+
options.errorprone.disableAllWarnings = true
47+
options.errorprone.disableWarningsInGeneratedCode = true
48+
options.errorprone.excludedPaths =
49+
".*/${project.layout.buildDirectory.get().asFile.relativeTo(projectDir)}/generated/.*"
50+
options.errorprone.error(
51+
"DefaultCharset",
52+
"FallThrough",
53+
"MissingCasesInEnumSwitch",
54+
"MissingOverride",
55+
"ModifiedButNotUsed",
56+
"OrphanedFormatString",
57+
"PatternMatchingInstanceof",
58+
"StringCaseLocaleUsage",
59+
)
60+
}
5661
}
5762

5863
tasks.register("compileAll").configure {
@@ -150,42 +155,53 @@ tasks.withType(Jar::class).configureEach {
150155
}
151156
}
152157

153-
spotless {
154-
java {
155-
target("src/*/java/**/*.java")
156-
googleJavaFormat()
157-
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"))
158-
endWithNewline()
159-
custom(
160-
"disallowWildcardImports",
161-
object : Serializable, FormatterFunc {
162-
override fun apply(text: String): String {
163-
val regex = "~/import .*\\.\\*;/".toRegex()
164-
if (regex.matches(text)) {
165-
throw GradleException("Wildcard imports disallowed - ${regex.findAll(text)}")
158+
if (!project.extra.has("reused-project-dir") && !gradle.ideSyncActive()) {
159+
spotless {
160+
java {
161+
target("src/*/java/**/*.java")
162+
googleJavaFormat()
163+
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"))
164+
endWithNewline()
165+
custom(
166+
"disallowWildcardImports",
167+
object : Serializable, FormatterFunc {
168+
override fun apply(text: String): String {
169+
val regex = "~/import .*\\.\\*;/".toRegex()
170+
if (regex.matches(text)) {
171+
throw GradleException("Wildcard imports disallowed - ${regex.findAll(text)}")
172+
}
173+
return text
166174
}
167-
return text
168-
}
169-
},
170-
)
171-
toggleOffOn()
172-
}
173-
kotlinGradle {
174-
ktfmt().googleStyle()
175-
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"), "$")
176-
target("*.gradle.kts")
177-
}
178-
format("xml") {
179-
target("src/**/*.xml", "src/**/*.xsd")
180-
targetExclude("codestyle/copyright-header.xml")
181-
eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep.XML)
182-
.configFile(rootProject.file("codestyle/org.eclipse.wst.xml.core.prefs"))
183-
// getting the license-header delimiter right is a bit tricky.
184-
// licenseHeaderFile(rootProject.file("codestyle/copyright-header.xml"), '<^[!?].*$')
175+
},
176+
)
177+
toggleOffOn()
178+
}
179+
scala {
180+
scalafmt(requiredDependencyVersion("scalafmt"))
181+
.configFile(rootProject.file("codestyle/scalafmt.conf").toString())
182+
licenseHeaderFile(
183+
rootProject.file("codestyle/copyright-header-java.txt"),
184+
"^(package|import) .*$",
185+
)
186+
target("src/**/scala/**/*.scala")
187+
}
188+
kotlinGradle {
189+
ktfmt().googleStyle()
190+
licenseHeaderFile(rootProject.file("codestyle/copyright-header-java.txt"), "$")
191+
target("*.gradle.kts")
192+
}
193+
format("xml") {
194+
target("src/**/*.xml", "src/**/*.xsd")
195+
targetExclude("codestyle/copyright-header.xml")
196+
eclipseWtp(com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep.XML)
197+
.configFile(rootProject.file("codestyle/org.eclipse.wst.xml.core.prefs"))
198+
// getting the license-header delimiter right is a bit tricky.
199+
// licenseHeaderFile(rootProject.file("codestyle/copyright-header.xml"), '<^[!?].*$')
200+
}
185201
}
186202
}
187203

188-
dependencies { errorprone(versionCatalogs.named("libs").findLibrary("errorprone").get()) }
204+
dependencies { errorprone(requiredDependency("errorprone")) }
189205

190206
java {
191207
withJavadocJar()

build-logic/src/main/kotlin/polaris-root.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ spotless {
4141
}
4242
}
4343

44-
if (System.getProperty("idea.sync.active").toBoolean()) {
44+
if (gradle.ideSyncActive()) {
4545
idea {
4646
module {
4747
isDownloadJavadoc = false // was 'true', but didn't work
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import org.gradle.api.publish.PublishingExtension
21+
import org.gradle.api.publish.maven.MavenPublication
22+
import org.gradle.api.tasks.bundling.Jar
23+
import org.gradle.api.tasks.scala.ScalaCompile
24+
import org.gradle.api.tasks.scala.ScalaDoc
25+
import org.gradle.kotlin.dsl.*
26+
import org.gradle.language.scala.tasks.KeepAliveMode
27+
28+
plugins {
29+
id("polaris-server")
30+
scala
31+
}
32+
33+
tasks.withType<ScalaCompile>().configureEach {
34+
options.release = 21
35+
scalaCompileOptions.additionalParameters.add("-release:21")
36+
sourceCompatibility = "21"
37+
targetCompatibility = "21"
38+
}
39+
40+
tasks.withType<ScalaCompile>().configureEach {
41+
scalaCompileOptions.keepAliveMode = KeepAliveMode.DAEMON
42+
scalaCompileOptions.encoding = "UTF-8"
43+
}
44+
45+
val scaladoc = tasks.named<ScalaDoc>("scaladoc")
46+
val scaladocJar = tasks.register<Jar>("scaladocJar")
47+
48+
scaladocJar.configure {
49+
dependsOn(scaladoc)
50+
val baseJar = tasks.getByName<Jar>("jar")
51+
from(scaladoc.get().destinationDir)
52+
destinationDirectory = baseJar.destinationDirectory
53+
archiveClassifier = "scaladoc"
54+
}
55+
56+
tasks.named("assemble").configure { dependsOn(scaladocJar) }
57+
58+
configure<PublishingExtension> {
59+
publications {
60+
withType(MavenPublication::class.java) {
61+
if (name == "maven") {
62+
artifact(scaladocJar)
63+
}
64+
}
65+
}
66+
}

build-logic/src/main/kotlin/util.kt

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import java.lang.IllegalStateException
21+
import org.gradle.api.Project
22+
import org.gradle.api.artifacts.MinimalExternalModuleDependency
23+
import org.gradle.api.artifacts.VersionCatalogsExtension
24+
import org.gradle.api.invocation.Gradle
25+
import org.gradle.kotlin.dsl.getByType
26+
27+
fun Gradle.ideSyncActive(): Boolean =
28+
System.getProperty("idea.sync.active").toBoolean() ||
29+
System.getProperty("eclipse.product") != null ||
30+
startParameter.taskNames.any { it.startsWith("eclipse") }
31+
32+
fun Project.requiredDependencyVersion(dependencyName: String): String =
33+
requiredDependency(dependencyName).version!!
34+
35+
fun Project.requiredDependency(dependencyName: String): MinimalExternalModuleDependency {
36+
val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
37+
val dependency =
38+
versionCatalog.findLibrary(dependencyName).orElseThrow {
39+
IllegalStateException("No library '$dependencyName' defined in version catalog 'libs'")
40+
}
41+
return dependency.get()
42+
}

codestyle/scalafmt.conf

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
version = 3.9.4
21+
runner.dialect = scala213
22+
23+
maxColumn = 100
24+
25+
preset = default
26+
align.preset = some
27+
28+
assumeStandardLibraryStripMargin = true
29+
align.stripMargin = true
30+
31+
rewrite.rules = [
32+
AvoidInfix
33+
RedundantBraces
34+
RedundantParens
35+
SortModifiers
36+
PreferCurlyFors
37+
Imports
38+
]
39+
40+
rewrite.imports.sort = original
41+
docstrings.style = Asterisk
42+
docstrings.wrap = fold

gradle/libs.versions.toml

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ prometheus-metrics-exporter-servlet-jakarta = { module = "io.prometheus:promethe
8484
quarkus-bom = { module = "io.quarkus.platform:quarkus-bom", version.ref = "quarkus" }
8585
scala212-lang-library = { module = "org.scala-lang:scala-library", version.ref = "scala212" }
8686
scala212-lang-reflect = { module = "org.scala-lang:scala-reflect", version.ref = "scala212" }
87+
scalafmt = { module = "org.scalameta:scalafmt-core_2.13", version = "3.9.4" }
8788
s3mock-testcontainers = { module = "com.adobe.testing:s3mock-testcontainers", version = "3.12.0" }
8889
slf4j-api = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
8990
smallrye-common-annotation = { module = "io.smallrye.common:smallrye-common-annotation", version = "2.10.0" }

0 commit comments

Comments
 (0)