Search code examples
springspring-bootgradlespring-config

Passing spring.config.location in bootRun task


I wanted to change the spring.config.additional-location for my gradle springboot app to run local. There's a properties file in my C:/demo_class_path and it's outside the jar. I was trying to access those properties in code. the command java -jar demo-application.jar spring.config.additional-location=file:C:/demo_class_path to run the jar wtth arguments works and I'll be able get the resource I need. But I was trying to add the arguement in bootRun task and it wasn't successful.

I tried the code below :

bootRun {
    systemProperties = [
        'spring.config.additional-location' : "file:C:/demo_class_path",
        'server.port' : 8090
    ]
}

or

bootRun {
    jvmArgs = [
        "-Dspring.config.additional-location=file:C:/demo_class_path/",
        "-Dserver.port=8090"
    ]
}

With the code above I will be able to change the port to 8090, but my files can't be picken up from the path anymore. I also tried to add spring.config.additional-location=file:C:/demo_class _path to application.properties and that didn't work either. I was wondering if the syntax for the location is wrong. In that case, why would the java command work?


Solution

  • The systemProperties are used to pass properties you normally pass with -D to the runtime.

    The jvmArgs is for passing arguments to the JVM.

    What you want to use is args instead of either one of the above.

    bootRun {
      args = [
        '--spring.config.additional-location=file:C:/demo_class_path/',
        '--server.port=8090'
        ]
    }
    

    It is important to include the / at the end for the spring.config.additional-location. When it doesn't end with a / it is interpreted as the base-name of a file instead of a file location.