kscript icon indicating copy to clipboard operation
kscript copied to clipboard

run spring boot with kscript

Open jangalinski opened this issue 7 years ago • 5 comments

I tried running

#!/usr/bin/env kscript

@file:DependsOn("org.springframework.boot:spring-boot-starter:2.0.4.RELEASE")

package my.package

import DependsOn
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.SpringApplication

@SpringBootApplication
class DummyApp

SpringApplication.run(DummyApp::class.java, *args)

it starts but fails with

ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
	at org.springframework.util.Assert.notEmpty(Assert.java:450)

A comparable groovy/@Grab script with the same setup works, so I wonder if this is something that can/will never work with kscript due to design or is just a missing feature that can be achieved ... somehow

I know that running complex spring boot apps with kscript seems to be out of scope, but I have quite a experimental microservices that require minimal coding and would benefit from a scripting approach ...

jangalinski avatar Sep 10 '18 18:09 jangalinski

I actually love the use-case.

It's not working for me either. It's because we don't use spring's custom packaging method.

The problem seems to center around the missing META-INF/spring.factories. However, even if I patch the jar created by kscript internally to include the file, it fails with the same error. So to progress here, we'd need to find a way to provide META-INF/spring.factories via classpath. It seems partially documented under https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-auto-configuration.html#boot-features-developing-auto-configuration In particular is says

Auto-configuration classes can be bundled in external jars and still be picked-up by Spring Boot.

So if we could find a way to run a spring-app without their deployment-scheme by providing an extra-jar that adds the missing META-INF/spring.factories the classpath, we could configure kscript to run it accordingly.

holgerbrandl avatar Sep 10 '18 19:09 holgerbrandl

glad you like the idea ... unfortunately, I haven't looked that deep into how kscript is working (just was glad that it IS working :-), so I won't be much help until I do ... but I can do research and testing if required.

I found this https://stackoverflow.com/questions/38792031/springboot-making-jar-files-no-auto-configuration-classes-found-in-meta-inf quite useful ... but it more or less resembles what you already found.

jangalinski avatar Sep 10 '18 19:09 jangalinski

I'd highly appreciate any help/research on this subject. I had stumbled over the same thread, but there was little practical advice about how to run a spring-boot app without jar-packaging.

holgerbrandl avatar Sep 11 '18 05:09 holgerbrandl

Do you think the thin-profile could be a way to go? It seems to support dynamic loading of dependencies: https://www.baeldung.com/spring-boot-thin-jar

jangalinski avatar Sep 11 '18 15:09 jangalinski

I'm not so sure. Whenever a custom build-file is required (e.g. doing something like apply plugin: 'org.springframework.boot') than we will struggle doing this in kscript, since we can not (and do not really want to) mess with the build beyond classpath and compiler settings.

I personally find it an odd design choice by the spring people to force users to use a custom build-plugin.

holgerbrandl avatar Sep 11 '18 15:09 holgerbrandl

It seems to work correctly in v4.2.1:

#!/usr/bin/env kscript

@file:DependsOn("org.springframework.boot:spring-boot-starter:2.0.4.RELEASE")

package my.pckg

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.SpringApplication

@SpringBootApplication
open class DummyApp

SpringApplication.run(DummyApp::class.java, *args)

In an initial example, the package name was incorrect - package is a reserved word.

The latest version of Spring Boot does not work as it was compiled with a newer Java version, and in kscript, we are missing the way to provide a script compilation target. Anyway, the initial problem was resolved.

aartiPl avatar Feb 02 '23 17:02 aartiPl