Cannot read value from .env file in Spring Boot - java

In my Spring Boot app, I have the following values in my .env file located in the project root:
.env:
DB_NAME=demo_db
DB_USERNAME=postgres
DB_PASSWORD=pass
On the other hand, when I set environment variebles on the Run configuration of IntelliJ as shown below, it is working:
DB_NAME=demo_db;DB_USERNAME=postgres;DB_PASSWORD=pass
So, I have the following questions:
1. As far as I know, .env file is used to keep environment variables instead of setting. Is that true? Could you explain the usage of it a little bit more (I look at several pages but not a brief explanation).
2. Is there any relation of .env file with application.properties except from reading variable values from .env file? I am not sure if there may also be different version o .env file e.g. .env-dev file ?

Apparently you are mixing up spring boot and InteliJ.
.env will be used by IntelliJ if you have EnvFile - IntelliJ IDE
Plugin
application.properties looked up by spring boot for injecting spring properties
If you are looking to run the application detacted from IDE you should define them in application.propeties defining them in a profile specific approach. Consider this article from offical spring docs.

Related

Read updated configurations from application.properties without repackaging the uber jar

Is there a way to read the updated changes in application.properties without repackaging the jar in Quarkus? I could not find anything related in the official documentation at https://quarkus.io/guides/config-reference. So I am assuming that the uber jar has the application properties built in. So is there a way to override those configs while running the jar?
Edit: What I mean by overriding is: Is there a way to specify the path of the config file that the jar reads while running the jar. Something similar to java -jar app.jar -DConfig='/path/to/application.properties'.
In Quarkus some of the configurations are fixed at build time, (the once in the documentation with the lock). Others are not, this ones you can change them at runtime using several methods, for example, using environmental variables or properties in the launch commands.
Check this links for more information:
https://quarkus.io/guides/config#build-time-configuration
https://quarkus.io/guides/config-reference#configuration-sources
As you can see in the configuration-source section, quarkus will firstly search in system properties and other sources rather than in the application properties file, there is one possibility for you to change the value of your properties.

What generated my "appengine-web.xml" file?

I inherited a Java app that is configured to run in Google App Engine. My pom includes the com.google.appengine.appengine-maven-plugin plugin, which may or may not be related to this question.
In my src directory, in the WEB-INF directory, I have a "app.yaml" file. But when my project is built into a war, the target directory has both a "app.yaml" file and a "appengine-web.xml" file. How did this "appengine-web.xml" file get here?
The first line of this "appengine-web.xml" file says <!-- Generated from app.yaml. Do not edit. -->. If this file was generated from an "app.yaml" file, then what generated it? Which plugin/function has created this file?
As mentioned in the official documentation appengine-web.xml Reference:
App Engine Java applications use a configuration file, named appengine-web.xml, to specify information about your app and to identify which files in the app's WAR file are static files (like images) and which are resource files used by the application.
So, this is created by default, by the App Engine environment, when using the App Engine Maven plugin, so you can handle some specific settings and configurations. You can find more details on these settings here, but it includes the setting of environment variables, how to scale the application - manual, basic or automatic - etc. In addition to that, you can check this example of a sample app that is deployed into war and the file is created as well. Please, bear in mind that this is only on Java 8.
To summarize, this file is created by the App Engine environment when using this plugin, not by a specific function. In this official documentation here, it indicates this as well:
The artifact you used to create the project has done the basic src/main/webapp/WEB-INF/appengine-web.xml configuration for you
So, this confirms that it was created via the plugin. Besides that, it shows the message of the relation with app.yaml, because they work together and each one of them has a specific use and settings that are needed for your application to work correctly.
Let me know if the information helped you!
This was a non-documented feature of App Engine Maven Plugin that was removed about two years ago.
https://github.com/GoogleCloudPlatform/app-maven-plugin/issues/426#issuecomment-665757462

Spring profile loading without being specified

I have a Maven/Spring boot project that I'm seeing some odd behavior in. To summarize, a spring profile is attempting to load, but that profile doesn't exist and isn't specified ANYWHERE in my code. It seems as if the profile name is being picked up from a different Java command line property though, which is why it is confusing me.
Essentially my command line looks something like this:
Java -DEnvironmentID=qa01 -jar myapp.jar com.something.package.Loader LOAD
From what I understand from Spring documentation's command line documentation is that I would need to call java with the -Dspring.profiles.active=profileName argument in order to invoke a profile. So while I'm NOT doing this, "qa01" is being picked as a profile and my application tries to load "application-qa01.properties"
During my testing, I've also noticed that small modifications to the "-DEnvironmentID" argument will prevent this from being picked up as a profile (ex. "qa10", "qa0", "a01", "qa100", etc...)
I'm stumped at this point and can't determine why it would be picked as a profile argument by Spring.
Just in case this could help someone in the future:
The problem was rooted in a dependency of my project. The dependency happened to use the same command line argument as my own to build out a path to the environment specific properties file packaged within the jar. The dependency project just happened to build out the properties file name/path just like a Spring profile, so it ended up not actually being related to profiles after all. When the dependency jar failed to provide the properties file, Spring continued to look for the file in my project, eventually throwing the error.

How to read different property files for different environment?

I have a web application created in maven. I am using tomcat server on my local as well as on dev unix box. I need to use two different .properties file like local.properties and dev.properties (may be in different folders). My doubt is 'How can we configure the tomcat of local and the tomcat of unix box to read different properties file at the time of deployment?' What are different ways for it.
The easiest and out of the box solution you can use with spring is to have two files, named application-dev.properties and application-prod.properties which will be activated if if you pass -Dspring.profiles.active=dev or prod. You pass this parameter when deploying the app. No configuration, no annotation, no xml. As long as you keep the naming convention application-something.properties
Easiest is pass a system parameter to your JVM to identify the environment, then package up a dev.properties and local.properties in the jar.
#PropertySource(value = {"classpath:local.properties", "classpath:${ENVX}.properties"}, ignoreResourceNotFound = true)
Use a -DENVX=dev for dev (add to your tomcat script), if it is not specified local.properties will be picked up.

how use a application-dev.yml from Jhipster on local path

Im trying to use the file application.yml, application-dev.yml and application-prod.yml that jhipster has in the proyect, but i dont want to use this file from the proyect i want to use those file from a local path. for example C://path
Please help me
Thanks
This is purely a spring-boot mechanism not specific to JHipster.
Look at spring-boot doc for all possibilities.
In your case, you could use:
java -jar myproject.jar --spring.config.location=file:///C:/path/application-dev.yml
but the simplest is to put your yaml file in same directory as your jar file, this is what is often used in production.

Categories

Resources