Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

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.

2. Maven Command-Line 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.

2.1. Spring Boot 1.x

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:

  • Arguments should be comma separated.
  • Each argument should be prefixed with —
  • We can also pass configuration properties, like spring.main.banner-mode, as shown in the example above.

2.2. Spring Boot 2.x

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

3. Gradle Command-Line Arguments

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

4. Overriding System Properties

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:

  • Spring Boot converts command-line arguments to properties, and adds them as environment variables.
  • We can use short command-line arguments, –port=8085 instead of –server.port=8085, by using a placeholder in our application.properties:
    server.port=${port:8080}
  • Command-line arguments take precedence over application.properties values.

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);
    }
}

5. Accessing Command-Line Arguments

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.

6. Passing Command-Line Arguments to the SpringBootTest

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");
    }
}

7. Conclusion

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.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW