Smajl
Smajl

Reputation: 8005

Eclipse does not pass mail properties to Spring Boot after migration

I am migrating my Spring Boot application from version 1.5.7 to 2.0.0 and I noticed that it no longer takes mail properties from ENV variables for some reason.

I am using java.mail.Sender and have the following propeties in my application.properties file:

spring.mail.host=smtp.example.com
spring.mail.username=username
spring.mail.password=password
spring.mail.port=587
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.defaultEncoding=UTF-8

This is there just to mock the mail properties in tests. I am injecting the real ones using the same keys as ENV variables: spring.mail.host=smtp.google.com, etc.

But when I try to send the email, I see that it is still using smtp.example.com. I thought that ENV variables had higher priority than values from application.properties. Did something change? Everything worked fine in Spring Boot 1.5.7.

EDIT: The following command works so it is definitely some problem with Eclipse:

SPRING_PROFILES_ACTIVE=development SPRING_MAIL_HOST=smtp.gmail.com SPRING_MAIL_USERNAME=xxx SPRING_MAIL_PASSWORD=xxx ./gradlew clean bootRun

What I don't understand is why the exact same configuration works, when I switch back to 1.5.7. It is also strange that when passign env variables via Eclipse run configuration, it works for profile. So some env variables are applied and some not...

Eclipse ENV variables

Upvotes: 2

Views: 1725

Answers (3)

CGS
CGS

Reputation: 2872

I was able to recreate this issue. Created a Spring boot App with 1.5.X and injected Environment variables from Eclipse. Now, when I migrate to 2.X release, the environment variables are not getting injected.

On further analysis, found out this interesting thread

One of the Spring-boot developers made this comment enter image description here

Hence my conclusion is when we are using 2.X release, there is one of the component within Spring-boot-parent which is making the spring boot maven plugin to fork it and run in a separate JVM. Thus, the environment variable is not getting passed.

That answers the question why profile value is picked-up from the environment section. Profile flag is always passed as an argument irrespective of whether the app runs in the maven JVM or a new one

To confirm this, you can add the config entries to the JVM argument tab like the one below enter image description here

You will now be able to see the new values passed to spring boot

Upvotes: 3

harsha kumar Reddy
harsha kumar Reddy

Reputation: 1386

first Make sure your IDE is running on Java 8 or later version .

With Spring Boot 2.0, many configuration properties were renamed/removed and developers need to update their application.properties/application.yml accordingly. To help you with that, Spring Boot ships a new spring-boot-properties-migrator module. Once added as a dependency to your project, this will not only analyze your application’s environment and print diagnostics at startup, but also temporarily migrate properties at runtime for you. This is a must have during your application migration:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-properties-migrator</artifactId>
    <scope>runtime</scope>
</dependency>

runtime("org.springframework.boot:spring-boot-properties-migrator")

Note Once you’re done with the migration, please make sure to remove this module from your project’s dependencies.

For more information follow this link

Spring Boot 2.X migration guide

Upvotes: 0

Hany Sakr
Hany Sakr

Reputation: 2939

I don't know much about your configurations, but if the project structure is okay with correct dependencies and the application.properties exit under src/main/resources and your startup class annotated with @SpringBootApplication, it should work fine.

you can test if the application reads your properties file by injecting a variable String with annotation @Value inside any class and log or print it.

@Value("${spring.mail.host}")
private String host;

Upvotes: 0

Related Questions