An introduction to Spring profiles

Before we prepare our application for production, let's talk a little bit about Spring profiles. 

Spring profiles (https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-definition-profiles-java) let you change the way your application behaves based on environments. This is achieved using the @Profile annotations and profile-specific configuration files, which can be activated by specifying the spring.profiles.active property. Based on the profile that we set here, Spring will choose the appropriate application.properties/application.yml files and will include/exclude components that are included/excluded for the specific profile using the @Profile annotation in the Java source code. For example, if we set spring.profiles.active=prod, all the Spring components that have @Profile("prod") will be instantiated and any component that has @Profile("!prod") will be excluded. Similarly, Spring will load and use the application-prod.yml or application-prod.properties file if it is available on the classpath.

JHipster configures a dev and prod profile by default and includes an application-dev.yml and application-prod.yml in the src/main/resources/config folder, along with the base application.yml file. JHipster goes a step further and provides a dev and prod profile for the Gradle build as well (Available for Maven as well) so that we can build/run the application for a particular profile, which is very handy. Here are the profile and database configurations defined in the application-dev.yml file:

...

spring:
profiles:
active: dev
include: swagger
...
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:file:./build/h2db/db/store;DB_CLOSE_DELAY=-1
username: store
password:
...

The following profiles are available in a JHipster application:

dev

Tuned for development and productivity, it enables Spring dev tools, in-memory databases, and so on

prod

Tuned for production, it focuses on performance and stability

swagger

Enables Swagger documentation for the API

no-liquibase

Disables Liquibase, and is useful in production environments where you don't want Liquibase to run