Spring Boot + Gradle - where to put environment configuration?

akn :

I was working on a simple application in Spring Boot. It was developed locally (and it works) with:

  • Gradle,
  • H2 database with connection properties set in application.properties placed on project's root
  • Maven folders structure (src/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:

  1. What should I do to have two different configurations (development and production)?
  2. Where to put the configuration files?
  3. Do I have to change something in the build.gradle?
  4. How to build the app with a development or production config?
  5. How to run the app with a development or production config?
  6. What are the best practices for creating multiple environment configurations?

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()
}
Stanislav :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=475086&siteId=1