Aplications might behave differently in different environments. One might have a specific configuration for Production Environment, which is different from Development Environment.
With simple properties, you can configure a different profile of properties for each of your environments, and access them through an easy to use DSL.
This library was inspired and is very similar to Spring Profiles, but only for Properties, and in a simpler way.
Edit your build.gradle
to contain the following dependency, from Maven Central:
implementation("com.kerooker.simpleproperties:simple-properties:{currentVersion}")
object MySystemProperties {
private val simpleProperties = SimpleProperties()
val databaseUrl: String = simpleProperties["my.database.url"]
}
File | Value of "my.database.url" |
---|---|
application-local.properties | localhost:3306 |
application-qa.properties | qa.database.server.intranet:3306 |
application-prod.properties | prod.database.server.intranet:7725 |
Launching app:
With local profile:
java -jar myapp.jar -Dactive_profiles=local
MySystemProperties.databaseUrl == "localhost:3306"
With production profile:
java -jar myapp.jar -Dactive_profiles=prod
MySystemProperties.databaseUrl == "prod.database.server.intranet:7725"
SimpleProperties will always try to read an optional default file application.properties
. Any profile may override properties from the default file.
SimpleProperties will try to find your profiles in some places:
- System Environment Variables
- Java Properties
- Defined at instantiation
SimpleProperties(profiles = listOf("qa"))
Multiple profiles may be used at once, in a comma separated value in case of System (-Dactive_profiles=foo,bar,baz
).
If keys are overriden, the last profile in the list will take priority.
Files are expected to be in the classpath. In Spring
, this is usually done by placing the file in the resources
folder, and this is allowed in Simple Properties too:
MyProject
`-- src
`-- main
|-- kotlin
| |-- Foo.kt
| `-- Bar.kt
`-- resources
|-- application.properties
|-- application-foo.properties
`-- application-bar.properties
You can customize where your files will be placed when creating the SimpleProperties
instance:
SimpleProperties(filesLocation = "foo/bar")
Which allows for example the structure
MyProject
`-- resources
`-- foo
`-- bar
|-- application.properties
`-- application-foo.properties