Skip to content
Advertisement

Spring: overriding one application.property from command line

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.

Advertisement

Answer

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>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement