paulschwarz / spring-dotenv

Provides a Dotenv property source for Spring
MIT License
301 stars 23 forks source link

Unable to make it run with server.port #3

Closed anasjaghoub closed 4 years ago

anasjaghoub commented 4 years ago

Hi When trying to use it with server.port it fails to start.

it gives an error:

Failed to bind properties under 'server.port' to java.lang.Integer:

Property: server.port
Value: ${env.SERVER_PORT}
Origin: class path resource [application.yml]:2:11
Reason: failed to convert java.lang.String to java.lang.Integer
anasjaghoub commented 4 years ago

One more problem, I've followed completely the same example, using maven. and .env file exists on the same level application.yml however, it kept returning default name value in the config, not the one in .env

anasjaghoub commented 4 years ago

@paulschwarz can u please help in this issue.

aBnormaLz commented 4 years ago

Can you show us your .env file please?

paulschwarz commented 4 years ago

Hi When trying to use it with server.port it fails to start.

it gives an error:

Failed to bind properties under 'server.port' to java.lang.Integer:

Property: server.port
Value: ${env.SERVER_PORT}
Origin: class path resource [application.yml]:2:11
Reason: failed to convert java.lang.String to java.lang.Integer

@anasjaghoub is it possible that you have an environment variable called SERVER_PORT already configured at the system level which is causing an undesired String to be read? If this is not the case, then please try debug where the String to Integer conversion is happening and see if there is a flaw in the logic somewhere. It would be interesting to know if your value is being picked up.

paulschwarz commented 4 years ago

One more problem, I've followed completely the same example, using maven. and .env file exists on the same level application.yml however, it kept returning default name value in the config, not the one in .env

@anasjaghoub please place your .env file in the base of your project. It should be gitignored and as such, it's not really part of your code. Think of it more as a tool for you and your fellow developers. As such, it belongs (by convention) in the root of the project where you'd expect to find the usual candidates, like readme.md, .gitignore, and so on.

Let me know if this solves your issue.

anasjaghoub commented 4 years ago

Hi @paulschwarz I've placed .env file in multiple places on the same level of src inside resources and it's not picked at all.

this is the content of the .env file EXAMPLE_NAME=Anas

and in application.properties example.name = ${env.EXAMPLE_NAME} and followed the same home function in the sample code you've. but always giving me the default World value.

paulschwarz commented 4 years ago

@anasjaghoub did you see this issue and remedy? https://github.com/paulschwarz/spring-dotenv/issues/2#issuecomment-624885900

Where is the word "World" coming from? I don't see it in your example.

How are you configuring example.name into your Spring application?

The logic behind the location of the .env file is as follows https://github.com/cdimascio/java-dotenv#optional-directory (note that this spring-dotenv library does not specify the location, therefore it is subject to the default rules as specified by the upstream java-dotenv library).

If your project does not contain sensitive information, I would be willing to take a look and see if I can spot the issue.

anasjaghoub commented 4 years ago

Yes @paulschwarz I saw #2 (comment)

the "World" is in my application.properties I added it as a default value example.name = ${env.EXAMPLE_NAME:World} without having a default value the application does not run.

here's a link for the repo https://github.com/anasjaghoub/springboot-dotenv/tree/master/src/main/java/com/anas/dotenv/demo it's a very basic demo application please have a look and let me know if you've any questions.

Regards

paulschwarz commented 4 years ago

@anasjaghoub I got your code sample working. Simply remove this line https://github.com/anasjaghoub/springboot-dotenv/blob/master/pom.xml#L48

The correct dependency is:

<dependency>
    <groupId>me.paulschwarz</groupId>
    <artifactId>spring-dotenv</artifactId>
    <version>1.0.0</version>
</dependency>

So you just need to remove <type>pom</type>. This is the same remedy stated here https://github.com/paulschwarz/spring-dotenv/issues/2#issuecomment-624885900

anasjaghoub commented 4 years ago

confirmed @paulschwarz it works perfectly and also the port is now working fine.

shareefhiasat commented 4 years ago

Hi @paulschwarz i have below configuration

image

And its throwing this exception Caused by: java.lang.IllegalArgumentException: No enum constant org.springframework.boot.logging.LogLevel.${env.LOGGING_LEVEL_ORG_HIBERNATE} https://gist.github.com/shareefhiasat/4f4981b444b3e56b324a7820c3e769bc

Can you please check, any hints appreciated thanks in advanced.

paulschwarz commented 4 years ago

@shareefhiasat this is an interesting question. i would need help investigating it. i don't think it's directly related to this library, but would be good to figure it out nonetheless. the issue is this https://gist.github.com/shareefhiasat/4f4981b444b3e56b324a7820c3e769bc#file-exception-env-L39

shareefhiasat commented 4 years ago

@paulschwarz great, i can help to some extent. Can you provide me which line in your repo to debug so i can try replicate on my fork; so we can have an in-depth look at it.

paulschwarz commented 3 years ago

@shareefhiasat the interesting part of this library is just https://github.com/paulschwarz/spring-dotenv/blob/master/library/src/main/java/me/paulschwarz/springdotenv/DotenvPropertyLoader.java

If you want to know how that works, you could follow it through to https://github.com/cdimascio/java-dotenv/blob/master/src/main/kotlin/io/github/cdimascio/dotenv/Dotenv.kt

Did you manage to understand the issue further?

paulschwarz commented 3 years ago

@anasjaghoub the artefact is now available on Maven Central

https://mvnrepository.com/artifact/me.paulschwarz/spring-dotenv

durimkryeziu commented 3 years ago

@shareefhiasat @paulschwarz Did you manage to resolve that issue with logging?

I encountered the very same issue when tried to map like ${env.LOGGING_LEVEL}

Thanks

shareefhiasat commented 3 years ago

@durimkryeziu ,

Actually, no its needed on the very first of the bootstrap loading of the applicationcould not make it work, and i tried to debug but no enough time, My idea was is to create a jar, to be dependent for all my microservices, and not including common .env values, this way i can disable it on all microservice easier without have to do extra commit on each. Another perspective of the issue related to your suggested improvements, is i cant inherent the .env through dependency,e.g.env has log enabled or not,Reading it from other services that have it in pom does not work, i think this needs the feature of configuring the directory i guess.

P.S even using it in the same service wont work, this need amendments on the dotenv library itself @paulschwarz