
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: February 20, 2025
In this quick tutorial, we’ll learn how to pass command-line arguments to a Spring Boot application.
We can use command-line arguments to configure our application, override application properties, and pass custom arguments.
First, let’s see how we can pass arguments while running our application using Maven Plugin.
Then we’ll learn how to access the arguments in our code.
For Spring Boot 1.x, we can pass the arguments to our application using -Drun.arguments:
mvn spring-boot:run -Drun.arguments=--customArgument=custom
We can also pass multiple parameters to our app:
mvn spring-boot:run -Drun.arguments=--spring.main.banner-mode=off,--customArgument=custom
Note that:
For Spring Boot 2.x, we can pass the arguments using -Dspring-boot.run.arguments:
mvn spring-boot:run -Dspring-boot.run.arguments=--spring.main.banner-mode=off,--customArgument=custom
Next, let’s discover how to pass arguments while running our application using Gradle Plugin.
We’ll need to configure our bootRun task in the build.gradle file:
bootRun {
if (project.hasProperty('args')) {
args project.args.split(',')
}
}
Now we can pass the command-line arguments:
./gradlew bootRun --args=--spring.main.banner-mode=off,--customArgument=custom
Along with passing custom arguments, we can also override system properties.
For example, here’s our application.properties file:
server.port=8081
spring.application.name=SampleApp
To override the server.port value, we need to pass the new value in the following manner (for Spring Boot 1.x):
mvn spring-boot:run -Drun.arguments=--server.port=8085
Similarly, for Spring Boot 2.x:
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8085
Note that:
server.port=${port:8080}
If necessary, we can stop our application from converting command-line arguments to properties:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(Application.class);
application.setAddCommandLineProperties(false);
application.run(args);
}
}
Let’s see how we can access the command-line arguments from our application’s main() method:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
for(String arg:args) {
System.out.println(arg);
}
SpringApplication.run(Application.class, args);
}
}
This will print the arguments we passed to our application from the command-line, but we can also use them later in our application.
With the release of Spring Boot 2.2, we gained the ability to inject command-line arguments during testing using @SpringBootTest and its args attribute:
@SpringBootTest(args = "--spring.main.banner-mode=off")
public class ApplicationTest {
@Test
public void whenUsingSpringBootTestArgs_thenCommandLineArgSet(@Autowired Environment env) {
Assertions.assertThat(env.getProperty("spring.main.banner-mode")).isEqualTo("off");
}
}
In this brief article, we learned how to pass arguments to our Spring Boot application from the command-line using both Maven and Gradle.
We also demonstrated how to access those arguments from our code in order to configure our application.