Card 02/ 06

ExampleDifficulty: Intermediate1 min

Configuration Example: A Command-Line Argument Overrides the File

The same jar and the same file, and one argument changes the port the server starts on.

A command-line argument has a higher priority than application.properties, so passing a property on the command line overrides the file without editing it. The file below sets the port:

application.propertiestext
server.port=8080

The application prints the port its web server started on. It is run twice, from the same jar, once without an argument and once with --server.port=9090:

text
java -jar app.jar
effective port: 8080

java -jar app.jar --server.port=9090
effective port: 9090
run in a container, Spring Boot 3.3.4: same file, same jar, different argument

Nothing in application.properties changed between the two runs. The command-line argument is higher in Spring Boot's order of sources, so it won.

The same override works as the environment variable SERVER_PORT=9090, which sits below the command line but above the file, and is the usual way a container platform passes it in.

So to change a setting for one run, or one environment, pass it at startup and leave the file as the default. The jar that was tested is then exactly the jar that runs in every environment.