Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a values in properties file during gradle jar build process?

Tags:

java

gradle

I have build.gradle file:

...
jar {
   baseName 'dev-filename'
   manifest {
      attributes (
         'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
         'Main-Class': 'package.of.main.class'
      )
   }
}
...

And properties file src/main/resources/application.properties:

...
database.username=dev_user
database.password=dev_password
...

How to create tasks (dev and prod) to build jar file and update values in the property file?

UPD1: I've tried next, but it doesn't work:

...
jar {
   baseName 'dev-filename'
   manifest {
      attributes (
         'Class-Path': configurations.runtime.collect {it.getName() }.join(' ')
         'Main-Class': 'package.of.main.class'
      )
   }
   ant.propertyfile(file: 'application.properties') {
      entry(key: 'database.username', value: 'new_username')
      entry(key: 'database.password', value: 'new_password')
   }
}
...
like image 338
Hugh Avatar asked Nov 22 '25 16:11

Hugh


1 Answers

Try to process your resources before packing it.

processResources {
  filesMatching('*.properties') {
    filter( ReplaceTokens, tokens:['foo' : 'bar'])
  }
}
like image 57
LazerBanana Avatar answered Nov 25 '25 07:11

LazerBanana