how to pass application.properties in commandLine for a spring boot application? - spring-boot

I have a spring boot application and I want to pass application.properties file in commandLine when I start-up.
i.e when I run mvn spring-boot:run --application.properties
I will have a default application.properties in src/main/resources. but that is only for testing purposes. In the production run, I would like to pass the property file in commandLine.
I am aware of passing single arguments such as
mvn spring-boot:run --server.port=9001.
But I have many such properties and would prefer to pass a property file if that is possible.

You can do that with spring.config.location property:
mvn spring-boot:run -Dspring.config.location=your.properties

In case if anyone finds it useful as it was for me. If you want to pass in individual application properties as parameters when using the maven spring boot run command you can use the argument spring-boot.run.jvmArguments.
Eg:
mvn spring-boot:run -Dspring-boot.run.jvmArguments='
-Dspring.datasource.url=jdbc:postgresql://localhost:5432/mydb
-Dspring.datasource.username=admin
-Dspring.datasource.password=admin'
With the above command I am setting (overriding) the following properties which were in the application.properties file.
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=admin
spring.datasource.password=admin

mvn spring-boot:run -Dspring-boot.run.arguments=--spring.config.location=classpath:/application-local.properties

Related

spring maven active profile

I am trying to run the Spring application from the server.
What is the difference between:
mvn spring-boot:run -Dspring.profiles.active=dev
and
mvn spring-boot:run -Dspring-boot.run.profiles=dev
Tnx
The difference is that the second property, spring-boot.run.profiles, comes from the Spring Boot Maven plugin which allows you to define profiles in your pom.xml, while spring.profiles.active comes from spring and can be used without any plugin, but in the end, they can do the same thing.

Maven Spring boot: set additional properties file location on test

Currently, I'm setting additional spring boot properties file using spring.config.additional-location:
mvn -DskipTests spring-boot:run -Dspring-boot.run.arguments=--spring.config.additional-location=api-props.properties
I'd like to know how to set this additional properties file when I test it. Up to now, I've tested to put this parameter on test.
mvn test -Dspring-boot.run.arguments=--spring.config.additional-location=api-props.properties
Any ideas?
In general, there are many options.
Start with application.properties in src/test/resources folder.
If the test is marked with #SpringBootTest this properties file will be picked automatically.
It will also override properties specified in "usual" application properties
Another good option is using #TestPropertySources annotation
For complete list see here

tomcat7-maven-plugin to deploy spring boot with appropriate spring profile selected

My goal is to be able to use the tomcat-maven plugin to deploy my spring boot application from the command line where an argument is supplied that tells spring which profile to use like this:
mvn tomcat7:deploy -Dspring.profiles.active="dev"
I've tried several different things such as the solution described here but the default application.properties is still always selected.
The only way that I've been able to get the application-dev.properties selected is by using
mvn spring-boot:run -Dspring.profiles.active="dev"
But we don't want to have tomcat packaged in our war
I'm new to maven and spring boot and I've been spinning my wheels for the better part of a a day now so any advice would be appreciated.
Consider using MAVEN_OPTS environment variable to set VM argument. (Linux/osx) example you would need to execute before your maven goal:
export MAVEN_OPTS="-Dspring.profiles.active=dev"
I found out the issue and I was able to get the correct profile selected using
export SPRING_PROFILES_ACTIVE=dev. The problem that I was having was when I was starting my local tomcat server through the eclipse UI my environment variables were being ignored. When starting tomcat through startup.bat the environment variable gets used and spring uses the correct profile.

How to configure jhipster app to run on different port when launching with maven

Jhipster docs say you have to
mvn -Pprod package
and then you can execute the generated war with
java -jar jhipster-0.0.1-SNAPSHOT.war --spring.profiles.active=prod
you may configure the server port with
java -jar jhipster-0.0.1-SNAPSHOT.war --spring.profiles.active=prod --server.port=9000
as per the Spring Boot docs specify that command line arguments take precedence over application properties files and YAML variants.
But, when trying to run the app with maven on a different port
mvn -Pprod -Dserver.port=9000 spring-boot:run
it still reads the server.port from application-prod.yml
mvn -Pprod spring-boot:run -Drun.arguments="--server.port=9000,--spring.profiles.active=prod"
mvn spring-boot:run -Drun.arguments="--server.port=9090"
For me, passing the SERVER_PORT as env variable worked:
SERVER_PORT=9000 mvn spring-boot:run
Spring translates the os env variables SERVER_PORT into spring's server.port configuration.
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

Why spring bean profile can not get system property passed from mvn -D?

if I do a export spring_profiles_default=staging then run the project it will work. But if I do mvn -Dspring_profiles_default=staging -pl project/abc jetty:run-exploded, it will not work. Any ideas? Thanks.
Also I should mention that I put System.getProperty("spring_profiles_default") in my code and it does get the value passed from mvn -D so it looks like Spring can not get it for some reason.
I know about the spring properties spring.profiles.default/spring.profiles.active (Spring 3.1). Did you mix this up with spring_profiles_default?

Resources