Reputation: 1926
I have the following in a gradle build file, what I am trying to do is filter the content of the files before I copy them, so I created a custom filter class which would be called each time a file is copied. Now I am trying to access the project properties from MyFilter
class, for example I tried to print the profile
property, but I did not find a way to do that so far, can this be done?
Usually I would pass the profile when starting the build file with gradlew build -Pprofile=prd
, still I really don't see a way to access those properties from MyFilter
class.
apply plugin: 'java'
tasks.build.finalizedBy('copyFiles')
class MyFilter extends FilterReader {
MyFilter(Reader input) {
super(new StringReader(""))
//tried many things to print the profile property, nothing worked.
}
}
def profile = project.properties['profile'] ?: "dev"
def amqa = project.properties['amqa'] ?: "127.0.0.1"
def amqaEG = project.properties['amqaEG'] ?: "127.0.0.1"
task copyFiles(type: Copy) {
project.properties['amqa']
from ('config')
into ('.')
include('**/*_#' + profile + '*')
rename { filename ->
filename.replace '_#' + profile, ''
}
filter MyFilter
}
Upvotes: 0
Views: 895
Reputation: 3506
You need to extend BaseParamFilterReader, then you get parameters in setParameters(final Parameter[] parameters)
method.
Copy
task has filter
overload that takes parameters, for example:
filter(ReplaceTokens, tokens: [version: '2.3.1'])
You'd probably write something like
filter(MyFilter, profile: profile)
Investigating ReplaceTokens
might help
Upvotes: 1