Reputation: 1391
I have an application.properties file with default variable values. I want to be able to change ONE of them upon running with mvn spring-boot:run
. I found how to change the whole file, but I only want to change one or two of these properties.
Upvotes: 126
Views: 152311
Reputation: 1630
I had to use quotes differently than what has been answered by most of people here.
Command I used is as : mvn spring-boot:run "-Dspring-boot.run.arguments=--server.port=9090"
Similarly, for other custom properties, it is same as above. For instance,
mvn spring-boot:run "-Dspring-boot.run.arguments=--com.my.custom.ppt=custom-value"
Additionally, to pass multiple values, remove the comma in between and pass as follows: mvn spring-boot:run "-Dspring-boot.run.arguments=--server.port=9090 --com.my.custom.ppt=some-value"
Upvotes: 0
Reputation: 45
I Was making a mistake with the syntax of commandline command , while passing command-line arguments, I was wrapping multiple arguments between " " and this was the issue. I simply ran the same command having multiple arguments separated by a space without wraaping them between "" and it worked just fine.
Please note this answer is for cases where we are trying to run this scenario from a jar file(not using mvn).
Correct Command: java -jar myJar.jar --com.arg1=10 --com.arg2=1
Incorrect Command: java -jar myJar.jar "--com.arg1=10 --com.arg2=1"
Upvotes: 0
Reputation: 31
If you have the jar file after doing mvn clean install, you can override any property that you have in your application.yml using the double -, like this:
java -jar name_of_your_jar_file.jar --parameter=value
For example, if you need to change your server port when starting you server, you can write the following:
java -jar name_of_your_jar_file.jar --server.port=8888
Upvotes: 2
Reputation: 1715
Running by Gradle:
./gradlew bootRun
./gradlew bootRun --args='--server.port=8888'
application.properties
file named PORT
, run this: PORT=8888 ./gradlew bootRun
Running by Maven:
mvnw spring-boot:run
mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
application.properties
file named PORT
, run this: SERVER_PORT=9093 mvn spring-boot:run
Using java -jar
:
./gradlew clean build
. We will find the jar file inside: build/libs/
folder.mvn clean install
. We will find the jar file inside:target
folder.java -jar myApplication. jar
java -jar myApplication.jar --port=8888
java -jar -Dserver.port=8888 myApplication.jar
SERVER_PORT
in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar
Upvotes: 6
Reputation: 2734
You can set an environment variable to orverride the properties. For example, you have an property name test.props=1
. If you have an environment variable TEST_PROPS
spring boot will automatically override it.
export TEST_PROPS=2
mvn spring-boot:run
You can also create a json string with all the properties you need to override and pass it with -Dspring.application.json
or export the json with SPRING_APPLICATION_JSON
.
mvn spring-boot:run -Dspring.application.json='{"test.props":"2"}'
Or just pass the properties with -Dtest.props=2
mvn spring-boot:run -Dtest.props=2
Tested on spring boot 2.1.17
and maven 3.6.3
Upvotes: 11
Reputation: 2203
Quick update:
if you are using the latest versions of spring-boot 2.X and maven 3.X, the below command line will override your server port:
java -jar -Dserver.port=9999 your_jar_file.jar
Upvotes: 25
Reputation: 116061
You can pass in individual properties as command-line arguments. For example, if you wanted to set server.port
, you could do the following when launching an executable jar:
java -jar your-app.jar --server.port=8081
Alternatively, if you're using mvn spring-boot:run
with Spring boot 2.x:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"
Or, if you're using Spring Boot 1.x:
mvn spring-boot:run -Drun.arguments="--server.port=8081"
You can also configure the arguments for spring-boot:run
in your application's pom.xml
so they don't have to be specified on the command line every time:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<arguments>
<argument>--server.port=8085</argument>
</arguments>
</configuration>
</plugin>
Upvotes: 213
Reputation: 187
If not working with comma, to override some custom properties or spring boot properties in multiple mode, use whitespace instead of comma, like this code bellow:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
Upvotes: 7
Reputation: 11
In Spring Boot we have provision to override properties as below
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8082
Upvotes: 1
Reputation: 131324
To update a little things, the Spring boot 1.X Maven plugin relies on the --Drun.arguments
Maven user property but the Spring Boot 2.X Maven plugin relies on the -Dspring-boot.run.arguments
Maven user property.
So for Spring 2, you need to do :
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"
And if you need to pass multiple arguments, you have to use ,
as separator and never use whitespace between arguments :
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081,--foo=bar"
About the the maven plugin configuration and the way of passing the argument from a fat jar, it didn't change.
So the very good Andy Wilkinson answer is still right.
Upvotes: 23