How to replace string during processResources task


(Sivachidambaram Masilamani) #1

Hi

We are planning to migrate our build scripts to Gradle and I am working on POC right now.

We have several projects each has its own properties file and their tokens needs to be changed based on the build env like (dev,UAT,prod).

To read environment specific properties we created a new plugin that reads a particular file and put the content to an object which is then set to a project property dynamically.Then when we try to read that propery in the processResources configuration section we are getting propery not found error.

I am not sure what am I doing wrong here as we are very new to Gradle,we do not have any local resources to help us too.

Following are the code for my plugin:

class LoadBuildEnvPropsTask extends DefaultTask{

@TaskAction

def loadBuldEnvProperties() {

def buildEnv = hasProperty(‘env’) ? env : ‘local’

project.ext.environment=buildEnv

logger.debug(“Environment is set to $project.environment”)

def configFile = project.file(‘buildEnvironments.groovy’)

if(!configFile.exists()){

throw new GradleException(‘buildEnvironments.groovy file not found at the root of this project.’)

}

def config = new ConfigSlurper(buildEnv).parse(configFile.toURL())

project.ext.config=config

} }

Here is snippet our build script where we cannot read the config property

processResources {

outputs.upToDateWhen{ false }

filter(ReplaceTokens, tokens: [LOG_FILE_LOC:project.config.LOG_FILE_LOC])

}

Thanks


(Peter Niederwieser) #2

Such configuration needs to be done by a plugin, not a task. The simplest form of a plugin is just another build script that gets included in the main script with ‘apply from: “path/to/script.gradle”’. Alternatively, a plugin can be implemented as a class that implements the ‘org.gradle.api.Plugin’ interface.