Search code examples
spring-bootmavenjarapplication.properties

Is it possible to send a application.properties file in arguments through a Jar file?


I have a Springboot project and I know I can send a application.properties file as a argument, but is this possible using a jar file?

I built my jar file using maven and in my application I have this piece of code that runs the programm if the user sent the argument run.

Is there any method that allows me to set the application properties if I receive it through argument? Or does the override of the file happens automatically as it does when I use the command mvn spring-boot:run -Dspring.config.location=your.properties

if (args[0].equals("run")) {
            ConfigurableApplicationContext ctx = SpringApplication.run(MigrationsApplication.class, args);
            int exitCode = SpringApplication.exit(ctx, () -> 0);
            System.exit(exitCode);
        }

Solution

  • For jar you can either pass one of the properties or the complete or its location as beow.

    we can configure the location directly in the command line:

    java -jar app.jar --spring.config.location=file:///Users/home/config/jdbc.properties
    

    We can also pass a folder location where the application will search for the file:

    java -jar app.jar --spring.config.name=application,jdbc --spring.config.location=file:///Users/home/config
    

    And, an alternative approach is running the Spring Boot application through the Maven plugin. There, we can use a -D parameter:

    mvn spring-boot:run -Dspring.config.location="file:///Users/home/jdbc.properties"