I have a simple Springboot app that can be ran with the command: ./mvnw spring-boot:run. This works fine if I put the URI to my database inside of the application.properties file but the problem is that this file is used by Heroku, and is not meant for my local use.
So I came across a Stackoverflow answer that said I could simply make another .properties file but name it application-dev.properties and then when I run my app, the correct .properties file will automatically be chosen when I set the active profile to dev.
So I tried this by doing the following:
- Make the
application.propertiesfile use the environment variable from Heroku since this is the.propertiesfile I do NOT want to use locally. - I created a
.propertiesfile calledapplication-dev.propertiesthat has this line in it:
spring.data.mongodb.uri=mongodb srv://MY_NAME:[email protected]/Employees?retryWrites=true&w=majority
- I run the app like this:
./mvnw spring-boot:run -Dspring.profiles.active=dev - The app fails with a ton of different errors because it is trying to use the
application.propertiesfile and not theapplication-dev.propertiesfile
Part of the error message:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo';
CodePudding user response:
-Dspring.profiles.active is setting the spring.profiles.active system property in the JVM that's running Maven, not the JVM that's running your application. To fix the problem, use the spring-boot.run.jvmArguments system property to configure the arguments of the JVM that is used to run your application:
./mwnw -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
Alternatively, there's a property specifically for setting the active profiles which is slightly more concise:
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
You can learn more in the relevant section of the reference documentation for Spring Boot's Maven plugin.
