Different application.properties files for development and production in jmix

In jmix app, in application.properties config file I have this 2 blocks

1 for test and dev purposes

main.datasource.jdbcUrl = jdbc:postgresql://srv/mydatabase
main.datasource.username = postgres
main.datasource.password =123

2 and this for production

#main.datasource.jdbcUrl = jdbc:postgresql://localhost/mydatabase
#main.datasource.username = postgres
#main.datasource.password =mypass

When I make war file I decomment block 2 and comment block 1 to have the proper connection params.

Is there a solution to have 2 application.properties files and tell the graddle when making war file to choose the one for production ? Or how i should do it right ?

Thanks.

Hi,
Spring Boot has a Profiles feature for your exact needs.
https://docs.spring.io/spring-boot/docs/2.4.x/reference/html/spring-boot-features.html#boot-features-profiles

See example how it is used in the Jmix deployment recipe:
https://docs.jmix.io/jmix/0.x/deployment/deploy-to-aws.html

1 Like

It is not straight the documentation.

Here is my situatiuon, may be y can help me.
In Intelij, i have 3 application.properties resource files.
1.) application.properties with all settings except database connection. Also I have put
spring.profiles.active=@spring.profiles.active@
2.) application-dev.properties with connection for development
3.) application-prod.properties with connection for production

In Intelij, Run configuration for jmix app I have added environment variable: spring.profiles.active=dev.
When building jmix app from Intelij and run it is ok, it takes the right connection from -dev properties file.

But when i build gradle war file it doesn’t work. I have added also environment variable spring.profiles.active=prod for war gradle run configuration.
How can I tell gradle to take that =prod file when building the war file ?

In build.gradle i have added:
apply plugin: ‘war’
war {
setBaseName(‘myapp’)
setWebAppDirName(“myapp”)
setEnabled(true)
}
and added the environment variable for run configuratuin for war

The environment variable should be specified at the run time, on the target server where the WAR file is deployed to the Tomcat.
One WAR file can be used for several deployments with different profiles (dev, prod, aws, …).

I put spring.profiles.active=prod on the prod server (windows environment variables if os is windows) and worked.
Thanks.

Hi,
just to be sure, that I got it right…
We have

  • application.properties,
  • application-dev.properties,
  • application-qa.properties,
  • application-prod.properties

and in the Application.java we have:

  • @SpringBootApplication
  • @Configuration
  • @PropertySource(value = { “classpath:application.properties” })

on the serve we set the Windows environment variable

  • spring.profiles.active = dev (or qa or prod)

Questions:
After that it will automatically use the appropriate profile, like application-dev.properties for spring.profiles.active = dev, right?

Which means, it overwrites the Application.java setting @PropertySource(value = { “classpath:application.properties” })?

And setting no env variable for spring.profiles.active will automatically cause, that the application.properties file is used?

Regards
Roland

Why do you set this on application class? application.properties is used by Spring Boot application by default.

Hi Konstantin,

good question.
I went through different solutions I found in the forum to get the .war deployment working.
On that way I added that line.
Will take it out.

But the rest works like I listed?

thx
Roland

Yes it should work as you expected:

1 Like

Hello everyone I’m having the same difficulty, but it’s not clear some steps, could you provide an example?