Add a new Profile to JHipster Application(Spring Boot Application)

Balvinder Singh
Tekraze
Published in
3 min readApr 30, 2019

Hi Everyone, this is my first post for the Tekraze Handle. Today I am gonna share about one of the experiences with JHipster for adding a new profile for deployment(For managing multi-staging servers).

Before we start let me tell you why I needed to add a new profile as there is already dev and prod profile there. So the thing is we are maintaining two servers in our company, so there are like one for staging(for testing production release before actual release) and one for production, where we deploy for the final release. So there are different IPs and domains for each, also there can be different cloud-based or local storage for each.

Like as we using Minio for local storage (AWS Bucket Replication), and Digital Ocean Spaces for production storage. And as we host on different servers they are having different domains and IPs. And both are actual production servers, so we needed to separate the configurations for each. So we added, a new profile as staging for the staging server. If you also need to maintain servers like these with a different configuration for each, go ahead and read the post.

So now let start and know how you can achieve the same:

Step 1. Create application.yml for related profile

Here I am taking staging as a new profile. So I am taking application-staging.yml for staging profile. Just copy-paste the application-prod.yml, if you want to create for production and copy application-dev.yml if you want to copy the dev environment.

Note : You can also create new file without copying, as copying will allow you to add same configurations faster as you only need to change values.

Step 2. Add new profile to pom.xml or Build.gradle based on your java build tool

Pom.xml
Go to pom.xml and search for <profiles> starting tag. Go to prod profile or dev profile and copy-paste. Change name in <id></id> tag like <id>staging</id>. Below is the sample code.

<profile>
<id>staging</id>
<dependencies>
// All your dependencies go here
</dependencies>
<build>
<plugins>
// All your plugins will go here
</plugins>
</build>
<properties>
<!-- default Spring profiles -->
<spring.profiles.active>staging${profile.swagger} </spring.profiles.active>
</properties>
</profile>

Here spring.profiles.active is used to make a profile active and also add to the environment variable, so the application can know which profile to use.

Build.gradle (Submitted by Shashi Kiran Deshetti )

if (project.hasProperty(‘prod’)) {
apply from: ‘gradle/profile_prod.gradle’
} else if (project.hasProperty(‘staging’)) {
apply from: ‘gradle/profile_staging.gradle’
} else {
apply from: ‘gradle/profile_dev.gradle’
}

Create the new file gradle/profile_staging.gradle like profile_dev.gradle or profile_prod.gradle and modify them to your liking.

Step 3. Add profile to Custom Configurations (Optional )

After that, there is like some configurations require to be annotated with profiles, for which the configuration needs to be active. For example, we have a Cassandra configuration class that have profile annotations. So we need to add there also.

@Configuration
@Profile({JHipsterConstants.SPRING_PROFILE_DEVELOPMENT, JHipsterConstants.SPRING_PROFILE_PRODUCTION,"staging"})
public class CassandraConfiguration {

Here I have added staging for the staging profile, this means that whenever we run as a staging profile, the Cassandra configuration needs to be active.

Step 4. Now running the custom profile

Just add flag of the profile which you want to run like as below for staging.

./mvnw -Pstaging -DskipTests 

Here -DskipTests is optional flag for skippin running tests

This is all for today, as you will be able to add a new profile and run the same. Have a happy deployment.

Hope you like the post, please share your views in the comments below or show your support by a clap. Also for more posts like this, do-follow.

--

--

Balvinder Singh
Tekraze

Open Source FullStack Developer | Blogger | Crypto Enthusiast | Gamer