Stealth Rabbi
Stealth Rabbi

Reputation: 10346

Running bootRun and use a different application.properties file

I'd like to be able to run bootRun for spring boot, but use a different application.properties file besides the one in src/main/resources/. Is that possible? I'd prefer to not overwrite the file in src/main/resources/, as it would dirty the file.

Is this possible?

Upvotes: 3

Views: 7239

Answers (4)

Kira Resari
Kira Resari

Reputation: 2440

I got it to work like this:

tasks.named("bootRun") {
    systemProperty "spring.config.location", "file:$projectDir/myConfigFolder"
    mainClass = "my.project.MyMainClass"
}

Upvotes: 0

nayan dhabarde
nayan dhabarde

Reputation: 2356

Try this command,

./gradlew bootRun --args='--spring.profiles.active=dev'

I too was looking for the solution, found it in official documentation https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#running-your-application

Upvotes: 2

Sam
Sam

Reputation: 97

Here is what my bootRun task looks like that does this

bootRun {
    systemProperty "spring.config.location", "file:$projectDir/spring-config/"
    main = springBootAppClass
}

Which specifies to use the spring-config directory in the project's root folder.

If using in conjunction with @PropertySource it looks like this

@PropertySource("${spring.config.location}/persistence.properties")

Upvotes: 1

Sachin Gupta
Sachin Gupta

Reputation: 8398

You can use profile based configuration selection. Just set a system environment property:

spring.profiles.active=dev

And now provide application-dev.properties in application resources(src/main/resources/) By this way you can use different properties for different environment.

If you want to provide files at a different location the use this environment property

spring.config.location=<path>

If you wish to use different name then application in property file name the use this environment property: spring.config.name=<new_name>

For more info check this link: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

Upvotes: 1

Related Questions