Could not find method filter() for arguments

(Erich Schroeter) #1

I am trying to replace the word “version” in a file following this tutorial, but am getting the following error.

  • What went wrong:

org.gradle.api.internal.MissingMethodException: Could not find method filter() for arguments [{tokens={version=project.version}}, class] on project ‘:myproject’.

Could not find method filter() for arguments [{tokens={version=0.0.1}}, class] on project ‘:myproject’.

My build file is below:


apply plugin: ‘java’

version = ‘0.0.1’

afterEvaluate {

filter(ReplaceTokens, tokens: [version: project.version])


What am I doing wrong?

(Luke Daley) #2

You haven’t copied the tutorial correctly. It should be:

  apply plugin: 'java'
  version = '0.0.1'
  afterEvaluate {
    configure(allProcessResourcesTasks()) {
                tokens: [version: project.version, gradleVersion: project.gradle.gradleVersion])
  def allProcessResourcesTasks() {
    sourceSets.all.processResourcesTaskName.collect {

(Erich Schroeter) #3

That does fix the issue. I feel stupid.

I had omitted the ‘allProcessResourcesTasks’ definition simply because I didn’t understand what it was doing and didn’t want to pollute the build file with another definition. I had assumed that ‘filter’ would still be called in the ‘afterEvaluate’ closure, but must be wrong.

(Luke Daley) #4

The ‘filter()’ method is part of the ‘Copy’ task, which ‘allProcessResourcesTasks()’ returns a collection of.

(markus_fried) #5

That thread is pretty old, but anyway…

Based on the above tutorial I want to replace certain tokens depending on the target environment: dev, prod, test - which will be set via cmd-line: -Penv=test/prod/dev

So I have created a config-file with the corresponding environment blocks, but how can I give this config to the filter-Method?

environments {
 dev {
  jdbc {
     test {
  jdbc {
     prod {
  jdbc {

Kind regards Mark.

(markus_fried) #6

Found it, we’re using the following snippet now:

ext.environment = hasProperty('env') ? env : 'dev'
  processResources {
 println "Environment is set to $environment"
    ext.configFile = file('')
 ext.replacements = new ConfigSlurper(environment).parse(configFile.toURL())
    println "jdbc.url:
 println "mailhost:
 println "mailport:
 println "domain:
 println "prototype:
       // this would be the "normal way to do things:
 // filter(ReplaceTokens, tokens: replacements)
    // but because of
 // we have to prevent binary files from being scanned by the replacement routine, otherwise they'd get corrupted!!!!
 from(sourceSets.main.resources.srcDirs) { include '**/*.properties' filter(ReplaceTokens, tokens: replacements) }
 from(sourceSets.main.resources.srcDirs) { exclude '**/*.properties' }

(smbarbour) #7

Found this while following the same tutorial, which apparently doesn’t work for me (using a multi-project setup). The method allProcessResourceTasks() can’t be in a project section and with it at the root level, it fails with “Could not find property ‘sourceSets’ on root project ‘workspace-mcu’.”

(Peter Niederwieser) #8

You need to make sure to apply the ‘java’ plugin before calling the method.

(smbarbour) #9

if I explictly apply it in the project, the same error occurs. If I apply it in allProcessResourcesTasks(), I get “Could not find property ‘all’ on SourceSet container.” If I change all to main, I get “Task with name ‘p’ not found in root project ‘workspace-mcu’.” It seems that I need to somehow pass a reference to the appropriate subproject, but nothing I’ve tried has worked.

(Peter Niederwieser) #10

These are strange errors, but without knowing (and seeing) the specifics of your setup, I can’t tell what’s going on.

(smbarbour) #11

Here is the build.gradle file as it currently is:

(Peter Niederwieser) #12

The method is being used from subprojects, but it configures the root project. One way to fix this is to pass the “current” project as an argument to the method, and then apply the plugin, and get the source sets, from that project.

(smbarbour) #13

Hmm… I was able to get it to not error out by changing sourceSets.all.processResourcesTaskName.collect to sourceSets*.processResourcesTaskName.collect but it’s not replacing the tokens…