Using envvault with Kotlin
Note: Please ensure that you have completed the previous steps
Prerequisites
- JDK installed
- Kotlin installed
- envvault CLI tool installed
- An existing Kotlin project
Usage
Running Your Application
To run your Kotlin application with environment variables from envvault:
$ envvault run --env=dev ./gradlew bootRun
Caching Environment Variables
For better performance, you can cache your environment variables:
$ envvault run --env=dev -c -- ./gradlew bootRun
Example Implementation
Here is how to set up a basic Kotlin Spring Boot application with envvault:
// Application.kt
package com.example.demo
import org.springframework.beans.factory.annotation.Value
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
@SpringBootApplication
@RestController
class Application {
// Your environment variables are automatically loaded
@Value("${PORT:8080}")
private lateinit var port: String
@Value("${DB_URL}")
private lateinit var dbUrl: String
@GetMapping("/")
fun hello(): String = "Hello from envvault!"
}
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
// application.properties
server.port=${PORT:8080}
spring.datasource.url=${DB_URL}