Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is resource filtering in Gradle possible without using tokens?

Tags:

gradle

The recommended way to do resource filtering in Gradle is by having tokens in the properties file and then replacing them when processing.

Example

# config.properties
hostname = @myhost@

and in build.gradle do something like below

processResources {
   filter ReplaceTokens, tokens: [
      "myhost": project.property('myhost')
   ]
}

The problem with this approach is that it won't work when running from IDEs like eclipse. I would like the property files to be free of Gradle specific tokens i.e just have

hostname = localhost

but have option to replace it when building from Gradle.

like image 524
Krishnaraj Avatar asked Aug 15 '14 14:08

Krishnaraj


3 Answers

You could use the following (not tested):

processResources {
    filesMatching('**/config.properties') {
        filter {
            it.replace('localhost', project.property('myhost'))
        }
    }
}

Or you could have a default file, used during development in your IDE, and have another file containing tokens and replacing the development one when building using gradle. Something like this (not tested)

processResources {
    exclude '**/config.properties'
    filesMatching('**/config-prod.properties') {
        setName 'config.properties'
        filter ReplaceTokens, tokens: [
            "myhost": project.property('myhost')
        ]
    }
}
like image 59
JB Nizet Avatar answered Nov 15 '22 21:11

JB Nizet


Can use thing like placeholder if you want.

In config.properties file

var1=${var1}
var2=${var2}

In gradle.properties file

processResources {
    filesMatching('**config.properties') {
        expand(
            'var1': project.property('var1'),
            'var2': project.property('var2'),
        )
    }
}
like image 45
Quy Tang Avatar answered Nov 15 '22 20:11

Quy Tang


The spring-boot approach

project.version=X.X.X.X
[email protected]@

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info-automatic-expansion

like image 1
Chomeh Avatar answered Nov 15 '22 22:11

Chomeh