Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot + Gradle - where to put environment configuration?

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()
}
like image 974
akn Avatar asked May 30 '17 21:05

akn


People also ask

How do I use Gradle with Spring Boot?

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.

How do I set up a consistent environment for a Gradle?

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.

Where are Gradle properties stored in Linux?

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.

How to set active profile in environment in Spring Boot?

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


1 Answers

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.

like image 143
Stanislav Avatar answered Nov 15 '22 05:11

Stanislav