95

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.

Andy Wilkinson
  • 97,688
  • 22
  • 237
  • 223
clueless user
  • 1,061
  • 1
  • 9
  • 12

7 Answers7

159

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>
Andy Wilkinson
  • 97,688
  • 22
  • 237
  • 223
19

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.

davidxxx
  • 115,998
  • 20
  • 192
  • 199
  • Cool!. System properties like -Dproperty1=value1 -Dproperty2=value2 can also be passed on the command line via mvn spring-boot:run -Dspring-boot.run.arguments="--property1=value1,--property2=value2" – Karthick Meenakshi Sundaram Nov 01 '19 at 12:50
  • Not working for me. If I put a whitespace the application will not detect the arguments, and if I put a whitespace the SDK will not read it. Still looking for how to add application.properties args. – fermoga May 31 '20 at 15:32
15

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
Ikbel
  • 1,335
  • 1
  • 11
  • 29
  • 1
    Why in the world did they change it to this syntax of all things? -D? – Spencer Sutton Jan 28 '21 at 23:08
  • 1
    They're not defining that syntax. That's the `java` executable's syntax for defining *system properties* (the kind you can get from System.getProperty()). Spring just grabs those in addition to its other sources. – Joshua Taylor Aug 06 '21 at 20:27
4

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"
Vielen Danke
  • 139
  • 6
4

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

Shawrup
  • 1,838
  • 2
  • 6
  • 16
0

In Spring Boot we have provision to override properties as below

mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8082

Jaydeep
  • 1,628
  • 1
  • 16
  • 28
0

Running by Gradle:

  • Run in default port(8080): ./gradlew bootRun
  • Run in provided port(8888): ./gradlew bootRun --args='--server.port=8888'
  • If we have any variable in the application.properties file named PORT, run this: PORT=8888 ./gradlew bootRun

Running by Maven:

  • Run in default port(8080): mvnw spring-boot:run
  • Run in provided port(8888): mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=8085'
  • Run in provided port(8888): mvn spring-boot:run -Dspring-boot.run.arguments='--server.port=8085'
  • Run in provided port(8888) with other custom property: mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8899 --your.custom.property=custom"
  • If we have any variable in the application.properties file named PORT, run this: SERVER_PORT=9093 mvn spring-boot:run

Using java -jar:

  • Create the .jar file:
    • For Gradle: ./gradlew clean build. We will find the jar file inside: build/libs/ folder.
    • For Maven: mvn clean install. We will find the jar file inside:target folder.
  • Run in default port(8080): java -jar myApplication. jar
  • Run in provided port(8888): java -jar myApplication.jar --port=8888
  • Run in provided port(8888): java -jar -Dserver.port=8888 myApplication.jar
  • Run in provided port(8888) having variable SERVER_PORT in application.properties file: SERVER_PORT=8888 java -jar target/myApplication.jar