I created a new project consisting of a library an...
# spring
n
I created a new project consisting of a library and an application (which uses the library) using the guides at https://spring.io/guides/tutorials/spring-boot-kotlin/ and https://spring.io/guides/gs/multi-module/. I also upgraded the Kotlin version to 1.3.11. Building the project and running the resulting jar works just fine. However, when building the library, the test task fails due to the following exception being thrown during the initialization of my service:
Copy code
...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'modelService' defined in file [G:\src\java\afraid\model\build\classes\kotlin\main\de\nimelrian\afraid\model\ModelService.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: Resource not found in classpath: kotlin/coroutines/coroutines.kotlin_builtins
Caused by: java.lang.IllegalStateException: Resource not found in classpath: kotlin/coroutines/coroutines.kotlin_builtins
...
I have a reproduction repository here: https://github.com/Nimelrian/kotlin-1.3-spring-test-error Running
.\gradlew build
should show the failing test. Gradle also shows some warnings regarding differing versions, so I tried pinning the versions using the dependency management plugin, which makes both the warnings and the test error disappear (See the
potential-fix
branch in the repo). I do wonder whether this is the correct way though.
c
Well, there is a much simpler fix. For your build file just add
ext['kotlin.version'] = kotlinVersion
somewhere under build script. This tells spring dependency management to use specific version of Kotlin
Another fix would be to actually apply spring boot plugin, which you have declared in buildscript but have not applied.
apply plugin: 'org.springframework.boot'
The boot plugin is smart enough to detect Kotlin plugin and align its own Kotlin dependency version with Kotlin plugin's. Spring dependency-management plugin isn't that smart (or doesn't care) metaphorically speaking 🙂
n
The multi module guide specifically tells you to not use the spring boot plugin
c
I see, so you apply boot plugin in the application part but not in the library part. Then my first suggestion should suit you nicely.
n
Yup, that worked just fine. Thanks!
c
uh, oh, my bad, that's actually a bit of a dirty way if you have a nicely declared dependency management block. Proper way would be to do it there, like this:
Copy code
dependencyManagement {
    imports {
        mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}") {
            bomProperty 'kotlin.version', kotlinVersion
        }
    }
}
This way you do not unnecessarily abuse
ext
system
another benefit of following this style is that it is immediately obvious what the code does, while having a free floating, seemingly unused
ext['something']
needs to be at least commented, to be understood without previous knowledge.
👌 1
n
👌