Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve build src #104

Merged
merged 7 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ individual buildSrc modules. This makes the build very modular and also very cle
It has the following details:

- Java 17
- Gradle 8.2.1
- Gradle 8.4
- buildSrc
- Kotlin DSL
- Version Catalog (libs.versions.toml)
- Gradle Build Scan enabled in CI (Attention! Terms of Service are accepted by the environment variable `BUILD_SCAN_TOS_ACCEPTED` and the scan enabled by the environment variable `CI`)
- Kotlin 1.8.10
- Spring Boot 3.0.3
- Dokka 1.7.20
- Kotlin 1.9.20
- Spring Boot 3.1.5
- Detekt 1.23.3
- Dokka 1.9.10
- Spring Dependency-Management

Additionally, I added a POC how Maven Publishing could work. In another project, I got it to work like this without
Expand Down
31 changes: 31 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

val libs: VersionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")

plugins {
Expand All @@ -12,6 +15,34 @@ repositories {
mavenCentral()
}

kotlin {
jvmToolchain {
languageVersion.set(
JavaLanguageVersion.of(libs.findVersion("jdk").get().toString())
)
}
compilerOptions {
@Suppress("SpellCheckingInspection")
freeCompilerArgs.add("-Xjsr305=strict")
allWarningsAsErrors = false
jvmTarget.set(JvmTarget.valueOf("JVM_${libs.findVersion("jdk").get()}"))
languageVersion.set(
KotlinVersion.valueOf(
"KOTLIN_${
libs.findVersion("kotlin").get().toString().substringBeforeLast(".").replace(".", "_")
}"
)
)
apiVersion.set(
KotlinVersion.valueOf(
"KOTLIN_${
libs.findVersion("kotlin").get().toString().substringBeforeLast(".").replace(".", "_")
}"
)
)
}
}

dependencies {
// buildSrc in combination with this plugin ensures that the version set here
// will be set to the same for all other Kotlin dependencies / plugins in the project.
Expand Down
88 changes: 49 additions & 39 deletions buildSrc/src/main/kotlin/kotlin-conventions.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import de.mrclrchtr.education.gradle.constant.JDK_VERSION
import de.mrclrchtr.education.gradle.constant.KOTLIN_VERSION
import io.gitlab.arturbosch.detekt.Detekt
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
import io.gitlab.arturbosch.detekt.DetektCreateBaselineTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion

plugins {
id("java-conventions")
Expand All @@ -13,56 +14,52 @@ plugins {
id("io.gitlab.arturbosch.detekt")
}

val embeddedMajorAndMinorKotlinVersion = project.getKotlinPluginVersion().substringBeforeLast(".")
if (KOTLIN_VERSION != embeddedMajorAndMinorKotlinVersion) {
logger.warn(
"Constant 'KOTLIN_VERSION' ($KOTLIN_VERSION) differs from embedded Kotlin version in Gradle" +
" (${project.getKotlinPluginVersion()})!" +
" Constant 'KOTLIN_VERSION' should be ($embeddedMajorAndMinorKotlinVersion)."
)
}

tasks.compileKotlin {
logger.lifecycle("Configuring $name with version ${project.getKotlinPluginVersion()} in project ${project.name}")
kotlinOptions {
@Suppress("SpellCheckingInspection")
freeCompilerArgs = listOf("-Xjsr305=strict")
allWarningsAsErrors = true
jvmTarget = JDK_VERSION
languageVersion = KOTLIN_VERSION
apiVersion = KOTLIN_VERSION
}
}

tasks.compileTestKotlin {
logger.lifecycle("Configuring $name with version ${project.getKotlinPluginVersion()} in project ${project.name}")
kotlinOptions {
@Suppress("SpellCheckingInspection")
freeCompilerArgs = listOf("-Xjsr305=strict")
allWarningsAsErrors = true
jvmTarget = JDK_VERSION
languageVersion = KOTLIN_VERSION
apiVersion = KOTLIN_VERSION
}
}
// Needs to exist before first usage of 'libs'
val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")

kotlin {
jvmToolchain {
languageVersion.set(
JavaLanguageVersion.of(JDK_VERSION)
JavaLanguageVersion.of(libs.findVersion("jdk").get().toString())
)
}
compilerOptions {
@Suppress("SpellCheckingInspection")
freeCompilerArgs.add("-Xjsr305=strict")
allWarningsAsErrors = false
jvmTarget.set(JvmTarget.valueOf("JVM_${libs.findVersion("jdk").get()}"))
languageVersion.set(
KotlinVersion.valueOf(
"KOTLIN_${
libs.findVersion("kotlin").get().toString().substringBeforeLast(".").replace(".", "_")
}"
)
)
apiVersion.set(
KotlinVersion.valueOf(
"KOTLIN_${
libs.findVersion("kotlin").get().toString().substringBeforeLast(".").replace(".", "_")
}"
)
)
}
}

detekt {
ignoreFailures = false

// Applies the config files on top of detekt's default config file. `false` by default.
buildUponDefaultConfig = true
config = files("$rootDir/detekt.yml")

// Define the detekt configuration(s) you want to use.
// Defaults to the default detekt configuration.
config.setFrom("$rootDir/detekt.yml")

// Builds the AST in parallel. Rules are always executed in parallel.
// Can lead to speedups in larger projects. `false` by default.
parallel = true
}

val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")

dependencies {
constraints {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
Expand All @@ -77,9 +74,22 @@ dependencies {
add("detektPlugins", libs.findLibrary("detekt-formatting").get())
}

// Activate Type Resolution
tasks.withType<Detekt>().configureEach {
// Target version of the generated JVM bytecode. It is used for type resolution.
this.jvmTarget = JDK_VERSION
classpath.setFrom(
sourceSets.main.get().compileClasspath,
sourceSets.test.get().compileClasspath
)
}

// Activate Type Resolution
tasks.withType<DetektCreateBaselineTask>().configureEach {
this.jvmTarget = JDK_VERSION
classpath.setFrom(
sourceSets.main.get().compileClasspath,
sourceSets.test.get().compileClasspath
)
}

afterEvaluate {
Expand Down
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
kotlin-logging = "3.0.5"
lorem = "2.2"
j2html = "1.6.0"
jdk = "17"
kotlin = "1.9.20"
kotlinForDetekt = "1.9.10"
springBoot = "3.1.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MessageClient(
@Value("\${mrclrchtr.message.factory.port}") val port: Int,
) {

private final var uriComponentsBuilder = UriComponentsBuilder.newInstance()
private final val uriComponentsBuilder = UriComponentsBuilder.newInstance()

private val restTemplate = templateBuilder.rootUri(
uriComponentsBuilder
Expand Down