Spring: overriding one application.property from command line

We Are Going To Discuss About Spring: overriding one application.property from command line. So lets Start this Java Article.

Spring: overriding one application.property from command line

Advertisements
  1. Spring: overriding one application.property from command line

    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:

  2. Spring: overriding one application.property from command line

    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:

Solution 1

Advertisements

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>

Original Author Andy Wilkinson Of This Content

Solution 2

Advertisements

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.

Original Author davidxxx Of This Content

Solution 3

Advertisements

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

Original Author Ikbel Of This Content

Solution 4

Advertisements

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"

Original Author Vielen Danke Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment