Springboot application not using the correct .properties file - java

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.properties file use the environment variable from Heroku since this is the .properties file I do NOT want to use locally.
I created a .properties file called application-dev.properties that has this line in it:
spring.data.mongodb.uri=mongodb+srv://MY_NAME:MY_PASSWORD#springbootcluster.v1maw.mongodb.net/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.properties file and not the application-dev.properties file
Part of the error message:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeRepo';
ERROR MESSAGES

-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.

Related

Issue with external application.properties on Windows

I have a spring-boot jar file that I run on both Windows and Linux. Now, I want to pass in a custom application.properties file for this.
This works perfectly fine on Linux, but on Windows, for some reason, it does not pick up the external application.properties file even though it is in the same folder as the jar file. (I know this because there is a server that I have configured to something in the jar itself, but in the external properties file, I am pointing to a server running on localhost. When I run the jar it is trying to connect to the server configured in the jar)
I then tried passing a custom config file. Did not work. Based on some suggestion in some forum, I passed the external app properties as:
java -jar myApp.jar --spring.config.location=file:///c/jar-path/application.properties.
Now, it looks like it is picking up this properties file. But it still gives me an error:
Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'my.config' in value "${my.config}
I have defined this property as:
#Value("${my.config}") String myConfig
I have added #Autowired to the class.
And in the app properties file, it is defined as:
my.config=true
What is surprising is that this works perfectly fine on Linux. Windows gives me the above error though. When I run this app from Eclipse on Windows, it runs perfectly fine too. But I see this error when run as a jar file. (Again only on windows not on Linux)
Any suggestions on what the issue could be? Is there anything different to be done for Windows platform vs Linux?
Thanks in advance...
Om

Custom application properties in spring boot using IntelliJ

I'm trying to use application properties, other than application.properties, say application_local.properties within resources directory.
So that I can have 2 properties files, one for local and other the server.
As mentioned in many blogs, I should use below command:
spring-boot:run -Dspring.config.location=/Users/myuser/work/MyProject/my-app/src/main/resources/application_local.properties
But this is not working, it is still fetching values from application.properties.
What am I missing, please suggest?
Thanks
1- Follow the naming convention application-{profile}.properties
application-local.properties
2-set profile
-Dspring.profiles.active=local
Briefly, you can use these two links:
How to load property file based on spring profiles
spring-profiles
-Dspring.profile.location takes directory as input. The purpose of this property is to specify additional directory location to keep your property files.
You are using property file name in your command.
Refer to detailed # Answer at other thread here
Instead you can use as suggested by #mehardad
The -D option will send parameters to Java virtual machine. In order to send parameters to Spring boot, the command option of '--' must be used.
Example:
Suppose, there is an option named 'spring.profiles.active' defined in the application.properties file as follows:
spring.profiles.active=dev
This option can be overwritten using command line parameter as follows:
java -jar application.jar --spring.profiles.active=prod
Use Spring profiles and choose at runtime, locally would
-Dspring.profiles.active=local
The property file should be called application-local.properties

Can't run Spring Boot JAR file with specific profiles

Like the image I've attached, I can run my JAR file with command 'java -jar', but it doesn't work when I add a spring.profiles.active config, and I can't get any infomation in my console.
Please try it as below:
java -jar service-app-1.7.44.jar -Dspring.profiles.active=demo

Pick up the environment specific properties file at run time

I want to pick up the environment specific properties file at run time from class path
I am still learning Spring Boot,
we can do it using environment variables or vm arguments or put in application.properties in maven run configuration
However we will always have to change the code at the time of deployment for a specific env if we put it application.properties.
I want to pick it up at run time without modifying the code at all.
Use spring profiles. Create application.properties for each profile like below:
application-dev.properties
application-prod.properties
Keep the application.properties as the master copy. If a property is missing in the profile-specific file then it will be picked from the master application.properties file as a fallback.
Then either set the following in application.properties or set as an environment variable or as a system property to activate a profile:
In application.properties:
spring.profiles.active=dev
Environment variable:
export spring_profiles_active=dev
System Property:
java -jar -Dspring.profiles.active=dev xyzapp.jar
I guess you can try System.setProperty() to set something at application level. But if you don't reset it will remain throughout application.

Heroku error code H10 when Spring Boot app is deployed

I can't open my app in Heroku - I get error H10. When I run my app in Eclipse or use executable jar file - everything work well. Here is my code : GITHub Below is my log file:
DropBox
You app is running on port 8080, but on Heroku it needs to bind to the environment variable $PORT. You can fix Spring to do this by adding the following to your application.properties:
server.port=${PORT:8080}
This will use $PORT if it's set, and default to 8080 if it's not.
I solved this error using Procfile, the Procfile is always a simple text file that is named Procfile without a file extension, For example, Procfile.txt is not valid, The Procfile must live in your app’s root directory. It does not function if placed anywhere else.
What to write in Procfile?
web: java -jar build/libs/your-project-name-version.jar
the version you can find inside build.gradle/pom.xml
Example:- web: java -jar build/libs/khatabook-1.0.jar
Where to create Procfile?
I have given my project structure image link for
[1]: https://i.stack.imgur.com/tsB35.png
My gradle file link
https://github.com/himanshujainpro/khatabook/blob/master/build.gradle
My application.properties
https://github.com/himanshujainpro/khatabook/blob/master/src/main/resources/application.properties
Project link for further reference
https://github.com/himanshujainpro/khatabook

Categories