Spring boot gradle kotlin build - spring-boot

I am trying to create JAVA springboot project with gradle build.I wanted to try out new kotlin support for springboot. Wondering how i can add bootrun task inside build.gradle.kts file.
plugins {
}
val coreServiceVersion = "3.0.0"
dependencies {
}
That's the over all structure of build.gradle.kts

You can use the following configuration:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
val kotlinVersion = "1.2.41"
id("org.springframework.boot") version "2.0.2.RELEASE"
id("org.jetbrains.kotlin.jvm") version kotlinVersion
id("org.jetbrains.kotlin.plugin.spring") version kotlinVersion
id("io.spring.dependency-management") version "1.0.4.RELEASE"
}
val coreServiceVersion = "3.0.0"
version = "1.0.0-SNAPSHOT"
tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
freeCompilerArgs = listOf("-Xjsr305=strict")
}
}
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
compile("org.jetbrains.kotlin:kotlin-reflect")
compile("com.fasterxml.jackson.module:jackson-module-kotlin")
testCompile("org.springframework.boot:spring-boot-starter-test")
}
See https://github.com/sdeleuze/spring-boot-kotlin-demo/ for a complete project.

Related

Could not resolve 'spring-boot-security-test' when testing service with mockk

I'm currently getting the following error when I try to test my service:
Execution failed for task ':compileTestKotlin'.
> Error while evaluating property 'filteredArgumentsMap' of task ':compileTestKotlin'.
> Could not resolve all files for configuration ':testCompileClasspath'.
> Could not find org.springframework.boot:spring-boot-security-test:.
Required by:
project :
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
Currently, my test file looks like this:
class UserServiceTest {
val userRepository: HireOutUserRepository = mockk()
val passwordEncoder: BCryptPasswordEncoder = BCryptPasswordEncoder()
val userService: UserService = UserServiceImpl(userRepository, passwordEncoder)
#Test
fun whenGetAllUsers_thenReturnSize() {
verify(exactly = 0) { userService.getUsers() }
}
}
and my build.gradle.kts looks like this:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "3.0.1"
id("io.spring.dependency-management") version "1.1.0"
kotlin("jvm") version "1.7.22"
kotlin("plugin.spring") version "1.7.22"
}
group = "com.jre"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
//implementation("org.springframework.boot:spring-boot-starter-data-jdbc")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
runtimeOnly("com.h2database:h2")
//testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-security-test")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "org.mockito", module = "mockito-core")
}
testImplementation("org.junit.jupiter:junit-jupiter-engine")
testImplementation("io.mockk:mockk:1.13.2")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
I'm not sure what is going on to cause the error to trigger as this should be a simple test scenario in my code.
Thanks
Dependency is incorrect. The correct dependency is this.
testImplementation 'org.springframework.security:spring-security-test'
Please add this in build.gradle file and try again

How to configure 'mainClassName' for Spring Boot Application in a Kotlin Gradle multi-module project?

I have a Gradle multi-module project in Kotlin using Spring Boot in some of the modules.
I use the 'org.springframework.boot' and 'io.spring.dependency-management' plugin in the project's root build.gradle which besides other things provide the bootRun task.
AFAIK I need to configure the mainClassName (or entry point) like this:
springBoot {
mainClassName = 'de.shinythings.hexagon.HexagonApplicationKt'
}
While this works in the subproject that contains the actual Spring Boot application it does not work in the root project.
I want to be able to configure it in the root project because I want to be able to run my project with ./gradlew bootRun instead of ./gradlew configuration:bootRun.
Other tasks like ./gradlew build or ./gradlew test work fine.
Here is the root project's build.gradle (link):
plugins {
id 'org.jetbrains.kotlin.jvm' version '1.4.10'
id 'org.springframework.boot' version '2.3.4.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
implementation project(':configuration')
}
springBoot {
mainClassName = 'de.shinythings.hexagon.HexagonApplicationKt'
}
subprojects {
group = 'de.shinythings.hexagon'
version = '0.0.1-SNAPSHOT'
repositories {
mavenCentral()
}
ext {
mockKVersion = '1.10.2'
}
apply plugin: 'org.jetbrains.kotlin.jvm'
apply plugin: 'io.spring.dependency-management'
dependencies {
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}
dependencyManagement {
imports {
mavenBom('org.springframework.boot:spring-boot-dependencies:2.3.4.RELEASE')
}
}
compileKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
freeCompilerArgs = ["-Xjsr305=strict"]
allWarningsAsErrors = true
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8
freeCompilerArgs = ["-Xjsr305=strict"]
allWarningsAsErrors = true
}
}
}
In the subproject/module this setting works (link) :
plugins {
id 'org.springframework.boot'
id 'org.jetbrains.kotlin.plugin.spring' version '1.4.10'
}
dependencies {
implementation project(':common')
implementation project(':application')
implementation project(':adapters:persistence')
implementation project(':adapters:web')
implementation 'org.springframework.boot:spring-boot-starter'
implementation('com.h2database:h2')
}
springBoot {
mainClassName = 'de.shinythings.hexagon.HexagonApplicationKt'
}
What do I have to do to configure the mainClassName is the root's project build.gradle?

Unresolved reference: compileKotlin in build.gradle.kts

Kotlin project success build by build.gradle:
compileKotlin {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}
compileTestKotlin {
kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8
}
Nice.
But I need to change to build.gradle.kts:
plugins {
kotlin("jvm") version "1.2.10"
id("application")
}
group = "com.myproject"
version = "1.0-SNAPSHOT"
application {
mainClassName = "MainKt"
}
java.sourceCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
jcenter()
}
val kotlinVer = "1.2.10"
dependencies {
compile(kotlin(module = "stdlib-jre8", version = kotlinVer))
implementation("com.google.code.gson:gson:2.7")
implementation("com.squareup.okhttp3:logging-interceptor:3.8.0")
implementation("com.squareup.retrofit2:converter-gson:2.1.0")
implementation("com.squareup.retrofit2:retrofit:2.5.0")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
and now I get error:
Line 32: compileKotlin {
^ Unresolved reference: compileKotlin
There's an issue in the Kotlin Gradle DSL that causes this.
https://github.com/gradle/kotlin-dsl-samples/issues/1368
You will need to use the following workaround until it gets resolved.
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}
Following official Kotlin documentation (using Gradle part), I suggest to use such constructions in the build.gradle.kts:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version ("1.3.21")
}
// repositories, dependencies, etc...
val compileKotlin: KotlinCompile by tasks
val compileTestKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
Use withType keyword:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
val kotlin = "1.3.61"
kotlin("jvm") version kotlin apply false
}
subprojects {
repositories { mavenCentral(); mavenLocal() }
apply(plugin = "org.jetbrains.kotlin.jvm")
tasks {
val java: String by project
withType<KotlinCompile>{
kotlinOptions { jvmTarget = java }; sourceCompatibility = java; targetCompatibility = java
}
}
}

Gradle, Kotlin, Multi Module Project: Getting build errors while compiling this project

I am getting the following error on running gradle build command on root folder.
E:\Code\mdh>gradle build
> Task :multidemo:compileKotlin FAILED
e: E:\Code\mdh\multidemo\src\main\kotlin\com\simbalarry\multidemo\SampleController.kt: (3, 23): Unresolved reference: somelib
e: E:\Code\mdh\multidemo\src\main\kotlin\com\simbalarry\multidemo\SampleController.kt: (18, 12): Unresolved reference: Tester
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':multidemo:compileKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 5s
4 actionable tasks: 1 executed, 3 up-to-date
Below is my project structure and multidemo project is using some functionalities of somelib. Also, I have deliberetly kept somelib as spring boot project as I will be adding more spring boot related functionalities later.
Root Project
|
|---settings.gradle
|---somelib
| |
| |---build.gradle
| |---settings.gradle
| |---src/main/kotlin/somepackage/Tester.kt
| |---src/main/kotlin/somepackage/SomeLibApplication.kt
|
|---multidemo
| |---build.gradle
| |---settings.gradle
| |---src/main/kotlin/somepackage/SampleController.kt
| |---src/main/kotlin/somepackage/MultidemoApplication.kt
Below are the contents of every file
Root/settings.gradle
include ':somelib', ':multidemo'
somelib/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.kotlin.plugin.spring' version '1.3.21'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
somelib/settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'somelib'
somelib/src/main/kotlin/somepackage/Tester.kt
package com.simbalarry.somelib
/*
* Created on : 17-03-2019
* Author : Nayan Kurude
*/
object Tester {
fun getOutputText(): String {
return "Sample Text"
}
}
somelib/src/main/kotlin/somepackage/SomelibApplication.kt
package com.simbalarry.somelib
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
class SomelibApplication
fun main(args: Array<String>) {
runApplication<SomelibApplication>(*args)
}
multidemo/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.kotlin.plugin.spring' version '1.3.21'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
compile project(':somelib')
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
multidemo/settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
}
}
rootProject.name = 'multidemo'
multidemo/src/main/kotlin/somepackage/MultiDemoApplication.kt
package com.simbalarry.multidemo
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
#SpringBootApplication
class MultidemoApplication
fun main(args: Array<String>) {
runApplication<MultidemoApplication>(*args)
}
multidemo/src/main/kotlin/somepackage/SampleController.kt
package com.simbalarry.multidemo
import com.simbalarry.somelib.Tester
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
/*
* Created on : 17-03-2019
* Author : Nayan Kurude
*/
#RestController
class SampleController {
#RequestMapping("/hello")
fun hello(): String {
return Tester.getOutputText()
}
}
Could anyone please tell me where am i doing something wrong.
As I said in comments: somelib is supposed to be a library. So it shouldn't apply the spring boot plugin, whose role is to create a Spring Boot application.
Your build.gradle file for somelib should look like this:
plugins {
id 'java-library' // 1
id 'org.springframework.boot' version '2.1.3.RELEASE' apply false // 2
id 'io.spring.dependency-management' version '1.0.6.RELEASE' //3
id 'org.jetbrains.kotlin.jvm' version '1.3.21'
id 'org.jetbrains.kotlin.plugin.spring' version '1.3.21'
}
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencyManagement { // 4
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
ext['kotlin.version'] = '1.3.21' // 5
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
compileTestKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
Some explanations (see the comments in the code):
Applies the java library gradle plugin, since that's what you want to create: a library that can be used in the other project
Adds the Spring Boot plugin to the classpath of the build without applying it, in order to be able to use its BOM later. See https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/gradle-plugin/reference/html/#managing-dependencies-using-in-isolation
Applies the spring dependency management plugin to be able to specify dependencies without version, by using the version numbers specified in the spring boot BOM
Configure the dependency management plugin to use the spring boot BOM
Override the kotlin version used in the BOM (1.2.71) by the one you want to use. See https://docs.spring.io/dependency-management-plugin/docs/current-SNAPSHOT/reference/html/#dependency-management-configuration-bom-import-override-property

Ktor startup example errors

I'm very new to Kotlin and Ktor and Gradle, wanted to try Ktor, so gone through the steps explained here, and ended up with this code, and structure shown in the screenshot:
As seen there are lots of error, how to fix them?
package blog
import org.jetbrains.ktor.netty.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.host.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*
fun main(args: Array<String>) {
embeddedServer(Netty, 8080) {
routing {
get("/") {
call.respondText("My Example Blog", ContentType.Text.Html)
}
}
}.start(wait = true)
}
The build.gradle file is auto generated as:
group 'Example'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
You have incomplete build.gradle script (missing dependencies) - see here for details. Here's the good one:
group 'Example'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.8
ext.ktor_version = '0.4.0'
repositories {
mavenCentral()
maven { url "http://dl.bintray.com/kotlin/ktor" }
maven { url "https://dl.bintray.com/kotlin/kotlinx" }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlin_version"
compile "org.jetbrains.ktor:ktor-core:$ktor_version"
compile "org.jetbrains.ktor:ktor-netty:$ktor_version"
compile "ch.qos.logback:logback-classic:1.2.1"
testCompile group: 'junit', name: 'junit', version: '4.12'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
kotlin {
experimental {
coroutines "enable"
}
}
I would recommend using the IntelliJ Ktor plugin to bootstrap your app. The ./gradlew run script runs right out of the box with this configuration without you needing to fiddle with the Gradle configuration. It's the easiest way to get started.
Here is an example Ktor app that uses the configuration: https://gitlab.com/tinacious/ktor-example
If you'd like to run your application from IntelliJ, check out this answer for the run configuration I use: https://stackoverflow.com/a/65350680/1870884

Resources