I have a spring boot application that uses a database. I have the local configuration in the application.properties file. I understand it is possible to change the values when I build the project with maven. I want to set the production values (db on AWS).
Here is application.properties
spring.datasource.url=${db.url:jdbc:postgresql://localhost:5432/mydb}
spring.datasource.username=${db.username:localusername}
spring.datasource.password=${db.password:localpassword}
I would like to change the values when I run. This will create a docker image.
mvn package docker:build
Is it possible?
Related
I am building a Spring Boot application using Bamboo. Is there any way I can read maven build related information like:
buildNumber and buildTimeStamp?
I have tried reading properties like below but they are not working.
#Value("${bamboo.buildNumber}")
private String buildNumber;
and
#Value("${buildNumber}")
private String buildNumber;
I think the bit I missing can be any of below:
Bamboo is not writing properties to Jar exported to Artifactory.
I am reading properties with wrong keys.
I am missing some POM configuration.
Any help is much appreciated.
The #Value notation reads values from a properties file when you run the spring application. There is no way that properties which existed in the Bamboo environment at the time that your spring app was built will be available when you run your spring app, unless you take steps to make them available.
You will need to build a .properties file during the Bamboo build, and have this packaged into your spring boot application.
I am now to Spring profile and I have a question, if I am doing a environment specific build using maven like mvn -Ptest then do I need to provide SPRING_PROFILES_ACTIVE parameter on execution.
If I understand Spring Profile correctly, giving SPRING_PROFILES_ACTIVE will direct my spring boot application to pick up the necessary application properties/#profile beans then why do I need to do mvn -Ptest.
One point I came accross is that mvn -Ptest allows us to package our properties file accordingly but in that case, isnt using Spring Profile a better solution.
It will be great if someone can point any scenario wherein we have to use mvn -Ptest even if we are using spring profile in Java application.
I think you are confusing maven profiles with spring profiles.
Maven profiles allow you to execute builds with different build configurations. It is only used during the maven build process.
Spring profiles can allow you to load different property files and is available at runtime to do whatever you want.
Now, you may have a maven profile that executes your spring application with a spring profile set but the difference is build vs execution time.
I have an app that uses Spring Boot app and I want to add a DB migration with flyway to it but for some reason is not working.
In my gradle file I have added this dependency:
implementation 'org.flywaydb:flyway-core:5.2.4'
Then in my yml configuration file, I add the following:
spring:
flyway.url: jdbc:postgresql://localhost:5432/my_database
flyway.user: postgres
flyway.password: postgres
In addition to this, I have my .sql scripts under resources/db/migration.
They have the right naming format(V1__base.sql).
I run the app using gradle bootrun
According to the documentation that I am following, this should just work out of the box. My database is running locally in a docker container and I don't see any error in the terminal. What am I missing?
The instructions I am following are from here: https://flywaydb.org/documentation/plugins/springboot
My spring boot application can runs mvn spring-boot:run or java execution, I need to know in which mode the application is running inside the main method.
You can't just detect it, but you can help yourself by setting a property that you can check in your code:
mvn spring-boot:run -Drun.jvmArguments="-Drunning.from.maven=true"
Then you can check using
System.getProperty("running.from.maven")
// or
Boolean.getBoolean("running.from.maven")
Or using Spring. Whatever you want.
Maybe 72.6 Set the active Spring profiles & 72.7 Change configuration depending on the environment will be helpfull:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
You can set different profiles for different configurations(e.g. production, dev).
How can I start flyway manually with Spring Boot? Because I have 2 dbs in my application, so after Springboot load the properties and before connect to DB, I need to run the flyway from my 2 dbs.
If I understand you correctly, you don't want to start flyway manually, but run it on two databases. Here is how:
create two Datasources and mark one as #Primary, the other (should be a bean) as #FlywayDataSource.
When flyway runs, it will automatically run on both data sources. see https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-execute-flyway-database-migrations-on-startup
You can use flyway maven plugin to populate your databases.
You can either have two properties file for each database and execute flyway using mvn -Dflyway.configFile=myConfig.properties
Or
you can provide the db properties while executing maven like mvn -Dflyway.user=myUser -Dflyway.schemas=schema1,schema2 -Dflyway.placeholders.keyABC=valueXYZ
But please note that you have to execute flyway maven task twice. Each time for one database. Like,
$mvn -Dflyway.configFile=db1.properties compile migrate
$mvn -Dflyway.configFile=db2.properties compile migrate
Refer https://flywaydb.org/documentation/maven/ for more information.
If you are fine with this maven approach you don't need spring to do flyway migration.