8000 GitHub - raphiz/hotswap: Java library & Gradle plugin providing lightweight and efficient hot reloading (similar to Spring Boot Devtools) for ANY JVM application
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
/ hotswap Public

Java library & Gradle plugin providing lightweight and efficient hot reloading (similar to Spring Boot Devtools) for ANY JVM application

License

Notifications You must be signed in to change notification settings

raphiz/hotswap

Repository files navigation

♨️ Hotswap

Hotswap is a Java library and Gradle plugin providing a lightweight and efficient hot reloading experience, similar to Spring Boot Devtools, for any JVM application during development.

Note

This library is relatively new, and feedback or contributions are welcome!

📌 Main Benefits

  • Framework and language agnostic: Works seamlessly with any JVM application, including Java or Kotlin, and frameworks such as Http4k or Javalin.
  • Faster iteration: Accelerates the development feedback loop by removing the need for manual restarts.
  • Reduced startup time: Eliminates the overhead of restarting the JVM and reloading unchanged classes.
  • Zero dependencies: Integrates easily without external dependencies.
  • Non-intrusive: Designed exclusively for development and does not impact production deployments.
  • Extendable: Provides a simple, flexible core that can be customized to your project's specific requirements.
  • Browser LiveReload (coming soon): Supports automatic browser reloads for browser-connected applications.

🚀 How it Works

Hotswap continuously monitors your classpath for changes, which are triggered by manual recompilation. When changes occur, Hotswap gracefully interrupts your running application, signaling it to shut down. Your application must handle this interruption by properly closing resources and stopping running services. Once the shutdown completes, Hotswap immediately restarts your application using updated classes.

This restart mechanism uses two class loaders, similar to Spring Boot Devtools. A base class loader manages unchanged classes, such as those from the JDK and third-party libraries, while a restart class loader specifically handles classes currently under development. On restart, the restart class loader is discarded and recreated.

If you encounter delays or inconsistencies during reloading, parameters like debounceDuration can be adjusted to better match your development environment.

🛠️ Usage

As Gradle Plugin

Add the following to your build.gradle.kts:

plugins {
    application
    id("io.github.raphiz.hotswap")
}

application {
    mainClass.set("your.main.class")
}

hotswap {
    // optional configuration, see below
}

As Library

Additional documentation will follow.

⚙️ Configuration

Gradle Plugin Options

Option Type Description Required Default
taskName String Name of the gradle task used to run your application no run
packagePrefixes List<String> Classes in this package namespace are hot swapped while all others are not no All classes that are not in one of these packages: java.*, jdk.*, com.sun.* and sun.*)
classPath List<Path> Directories and Files to watch for changes no System.getProperty("java.class.path")
shutdownPollingInterval Duration Interval before logging warnings during shutdown no 5s
debounceDuration Duration Aggregation delay for rapid file changes no 100ms

Library Options

Option Type Description Required Default
mainClass String Your application's main class yes -
packagePrefixes List<String> Classes in this package namespace are hot swapped while all others are not no All classes that are not in one of these packages: java.*, jdk.*, com.sun.* and sun.*)
classPath Set<Path> Directories and Files to watch for changes no System.getProperty("java.class.path")
shutdownPollingInterval Duration Interval before logging warnings during shutdown no 5s
debounceDuration Duration Aggregation delay for rapid file changes no 100ms

📦 Trigger Restarts

Hotswap does not include its own compiler or build system. Instead, it integrates seamlessly with your existing toolchain. To trigger restarts, you simply recompile your code - Hotswap will detect any resulting class file changes and restart the application accordingly.

The most common ways to trigger recompilation include:

Hotswap listens for changes in the compiled output (i.e., class files), not the source files themselves. As long as the build process updates class files in the directories Hotswap is watching, the reload will be triggered.

📝 Logging

Hotswap uses Java's built-in logging infrastructure (java.util.logging). If your application uses a different logging framework (e.g., SLF4J, Logback), additional libraries or bridges might be necessary.

For detailed guidance on integrating logging frameworks, see this article.

🔧 Development

Prerequisites

Recommended: Use nix and direnv for managing dependencies and your development environment.

  • Install nix.
  • Install direnv.
  • Execute direnv allow in your project directory.

Using Nix is recommended but not mandatory; it provides tooling, dependencies, and pre-commit configurations.

Running Tests

./gradlew check

📄 License

This project is licensed under the MIT License. See LICENSE for details.

About

Java library & Gradle plugin providing lightweight and efficient hot reloading (similar to Spring Boot Devtools) for ANY JVM application

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0