Skip to content

Testing and fighting with SpringBoot fat jars and Kotlin scripting (extracting jars and classloader battle)

Notifications You must be signed in to change notification settings

micromata/SpringBoot-KotlinScripting

Repository files navigation

Run Kotlin Scripts in Spring Boot: A Minimal Example

🚀 Why Kotlin Scripting in Spring Boot?

Kotlin scripting adds dynamic power to Spring Boot applications. Imagine adapting your app’s behavior, such as reports at runtime without redeploying—Kotlin scripts make this possible! However, using Kotlin scripts inside Spring Boot fat JARs is challenging. This repository explores practical solutions and invites collaboration to refine and improve them.

🛑 The Challenge

Kotlin scripts struggle to load classes from Spring Boot fat JARs. While configuring a custom classloader for Kotlin scripting seems promising, the default Kotlin implementation uses its the standard classloader, making this approach ineffective.

✅ Our Current Solution (for Kotlin 2.0.21)

The workaround involves:

  1. Unpacking JARs: Extracting the Kotlin JARs embedded in the fat JAR.

  2. Copying JARs to Local Filesystem: Using URLClassLoader to load classes from these JARs.

Important
Don’t mix unpacking and copying JARs in the same project. (Look for COPY_MIX_FAIL in the logs.)

We’ll monitor Kotlin updates to see if this limitation gets resolved.

💡 Why Use Kotlin Scripting?

  • Dynamic Adaptability: Change scripts without restarting the application.

  • Runtime Flexibility: Evaluate and execute scripts dynamically for specific tasks.

Security Warning: Scripts can access your application internals. Secure them to avoid risks.

🌟 How This Project Helps You

This project is a step-by-step guide for implementing Kotlin scripting in Spring Boot. It covers:

  • Gradle Configuration: Simplifies fat JAR unpacking and script integration.

  • Practical Examples: Includes sample modules demonstrating unpacking and copying approaches.

  • Pitfalls and Best Practices: Save hours of troubleshooting by learning from our experience.

Modules Overview

  1. Application: Core Spring Boot app with Gradle config for unpacking JARs.

    • Supports passing variables (e.g., for user authentication in scripts).

    • Includes automatic script timeout to prevent endless execution.

  2. Business: Demonstrates an unpacked module accessible to scripts (for test purposes, only).

  3. Commons & Misc: Shows copied JARs loaded via URLClassLoader (for test purposes, only).

🚀 Quick Start

To test this project:

  1. Clone the repository: git clone https://github.com/micromata/SpringBoot-KotlinScripting.git

  2. Build the app: ./gradlew clean build

  3. Run it: java -jar application/build/libs/application-0.0.1.jar

  4. Check the console output for execution details.

🛠️ Why Gradle?

Gradle simplifies fat JAR unpacking compared to Maven. After experimenting with both, I found Gradle to be a more developer-friendly solution for this use case.

🌐 Learn More

Kotlin Scripting Updates

Stay up-to-date with JetBrains' Kotlin scripting blog: State of Kotlin Scripting 2024.

Collaboration Welcome

Have ideas or improvements? Fork the repo, open an issue, or submit a pull request. Your feedback will make this project even better!

🔎 Optimized for Search Engines

Keywords

  • Kotlin scripting

  • Spring Boot scripting

  • Spring Boot fat JAR

  • Spring Boot dynamic runtime

  • Kotlin dynamic evaluation

  • Kotlin script classloader

Meta Description

"Explore a minimal Spring Boot project demonstrating Kotlin scripting integration. Overcome Kotlin classloader issues in combination with Spring Boot Fat JAR with practical solutions and Gradle examples."

About

Testing and fighting with SpringBoot fat jars and Kotlin scripting (extracting jars and classloader battle)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages