Create multiple jars, one for each environment(spring profile) using spring-boot


(David Tomić) #1

Hi,

I want to have 3 tasks:

buildLocal
buildDev
buildProd

each of them will create jar file for specific spring-boot-profile.

i.g.
buildLocal task will use application.properties
buildDev task will use application-dev.properties
buildProd task will use application-prod.properties

when running spring-boot app this are options to provide specific profile:

  1. Set spring.profiles.active parameter in application.properties
  2. WebApplicationInitializer
  3. JVM System parameter
  4. Environment variable

I want to use 1. option because that way I will have jar which knows which profile is activated.

I tried something like this:

task buildDev {

      doLast {
      	
    	def file = file('src/main/resources/application.properties')
      
      	def lines = file.readLines()
      	lines.remove(0)
      
      	file.write "spring.profiles.active=dev\n"
      	lines.each { line ->
      		file << line + "\n"
      	}
      }
}

buildDev.dependsOn build

This almost works, jar file is how it should be, but the problem is, application.properties in a project is changed too, which I don’t want, it should be same as before launching buildDev task.

I will appreciate any help here, thank you.