I was working on a simple application in Spring Boot. It was developed locally (and it works) with:
application.properties
placed on project's rootsrc/main/groovy
, src/main/resources
, etc.) Now it's the time when I'd like to deploy it to the Openshift, so I need to create an additional, production configuration with a MySQL settings, but I don't know where to put it and how to use it.
So my questions are:
development
and production
)?build.gradle
?I'm rather a frontend dev and all these backend stuff are not obvious for me, so please consider it in your answers.
This is the content of my current build.gradle
plugins {
id 'org.springframework.boot' version '1.5.3.RELEASE'
id 'java'
id 'groovy'
}
jar {
baseName = 'myproject'
version = '0.0.1-SNAPSHOT'
}
repositories {
jcenter()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile 'mysql:mysql-connector-java'
compile("com.h2database:h2")
compile("org.springframework.boot:spring-boot-starter-security")
compile('io.jsonwebtoken:jjwt:0.7.0')
compile localGroovy()
}
It allows you to package executable jar or war archives, run Spring Boot applications, and use the dependency management provided by spring-boot-dependencies . Spring Boot’s Gradle plugin requires Gradle 6.8, 6.9, or 7.x and can be used with Gradle’s configuration cache. In addition to this user guide, API documentation is also available. 2.
Setting up a consistent environment for your build is as simple as placing these settings into a gradle.properties file. The configuration is applied in following order (if an option is configured in multiple locations the last one wins): gradle.properties in Gradle installation directory. gradle.properties in project root directory.
Gradle properties such as org.gradle.caching=true that are typically stored in a gradle.properties file in a project root directory or GRADLE_USER_HOME environment variable. Environment variables such as GRADLE_OPTS sourced by the environment that executes Gradle.
Once you have profile specific configuration, you would need to set the active profile in an environment. Use spring.profiles.active=prod in application.properties In this example let’s set it in application.properties. Lets add another property to application.properties
What should I do to have two different configurations (development and production)?
In your case, you can use a profiles to achieve it. You can read about it here. For each profile you can have specific application properties file (named application-%PROFILE_NAME%.properties
, like application-prod.properties
, the same is true for .yml
configuration files) And you have to specify what profile yo use then you are starting your app via command line switch for example like so:
--spring.profiles.active=prod
Where to put the configuration files?
Just in the same place as your application.properties
file.
Do I have to change something in the build.gradle?
No, you don't need to modify your build script. Since all specific configurations are needed for running your application, not for building.
How to build the app with a development or production config?
You don't need to build it with some specific configuration, just run it with it.
How to run the app with a development or production config?
As it was said earlier - just specify what profile to use when starting the application.
What are the best practices for creating multiple environment configurations?
As for me, if you use a spring - to use profiles and profile specific configuration and bean-definitions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With