How to override spring boot properties? - java

How to override spring boot properties at runtime?
Below command works fine in terminal
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081
but it's not working in Eclipse
-Dspring-boot.run.arguments=--server.port=8081

You have two options.
You can configure server.port=8081 in application.properties file in spring boot application.
Configure only -Dserver.port=8081 in VM arguments in eclipse as displayed in the image.

Use -Dserver.port=8081 as vm argument from run configuration of eclipse. It will work

Related

How to set system property when running a spring boot app in intellij?

I want to set a system property when running a spring boot application in IntelliJ, and then get it in my program. I am using IntelliJ Idea CE.
Initially I tried in a normal Java project (not spring or maven). I just set the value in "VM Options" in run configurations (Ex: -DpropertyName=propertyValue). And then I ran the main function, I was able to access it by System.getProperty("propertyName").
Now I want to do the same with my spring boot project. Its packaging is war. As I am using Community Edition, I run the project by running the maven command spring-boot:run. How do I pass a system property here?
I have tried adding the property in "VM Options" in the run configuration.
I have tried below commands -
spring-boot:run -DpropertyName=propertyValue
spring-boot:run --propertyName=propertyValue
spring-boot:run -Drun.jvmArguments="-DpropertyName=propertyValue"
But in all approaches, when I am getting the property value in my method, it gives null.
Any suggestions are welcome.
EDIT Adding code snipped as requested
public static boolean isSomething() {
return !StringUtils.equals(System.getProperty("propertyName"), Constants.CONSTANT_VALUE);
}

Spring Boot Tomcat won't start on specific port

I want to run a Spring Boot application using Maven but when I run
mvn clean install spring-boot:run -Dserver.port=8888
Spring runs on port 8080. How should I run a Maven Spring Boot application on a specific port?
I use Spring Boot version 2.2.6.
-Dserver.port is not passed to the JVM running your app.
You have to use
mvn clean install spring-boot:run -Dspring-boot.run.arguments="--server.port=8888"
Read more about this:
https://www.baeldung.com/spring-boot-command-line-arguments

intellij spring boot run cannot load application.yml automatically

my application.yml locate
my application.yml
spring boot run server port
Intellij spring boot run configuration
Hi.
My project is Intellij spring boot 1.5.7 Gradle Project
When Intellij spring boot run cannot load application.yml automatically.
but gradle bootRun is very well run&load.
Please suggestion me.
You should specify profile configuration. From your application.yml, you have multiple configuration separated by "---", this is used for multiple profile configuration for different spring profile. Try removing "---" and specify profile as following :
Multiple configuration is used when you have different configuration for different profile, as following:

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...
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
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
You will now be able to see the new values passed to spring boot
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;
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

"-Drun.profiles=.." is not working when trying to execute Spring Boot jar

I am trying to execute the following:
C:\Users\Homeuser>java -jar -Djasypt.encryptor.password=testpass -Drun.profiles.active=dev C:\testproj\target\test-0.0.1-SNAPSHOT.jar
However, the version that runs uses the properties that are specified in the application-local.properties file, and not from the application-dev.properties file as I expected. I know this because the local version uses an H2 db with fake data, and the -dev profile connects to a real database with real data, and what I saw was the fake data. In my application.properties file, I have the following setting:
spring.profiles.active=local
This problem is only happening when I am building a jar first then executing it. When I use the following command from Eclipse maven run profile:
clean spring-boot:run -Drun.profiles=dev -Djasypt.encryptor.password=testpass
It correctly loads up the expected profile. What am I doing wrong?
Thank you.
The property run.profiles is a property which comes from the spring-boot maven plugin. So it only works if you use that to run your application. If you want to activate a plugin when using the jar command, specify the property spring.profiles.active=dev.

Categories