Could not find method filter() for arguments

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 org.apache.tools.ant.filters.ReplaceTokens] on project ‘:myproject’.

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

My build file is below:

import org.apache.tools.ant.filters.ReplaceTokens

apply plugin: ‘java’

version = ‘0.0.1’

afterEvaluate {

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

}

What am I doing wrong?

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

import org.apache.tools.ant.filters.ReplaceTokens
  apply plugin: 'java'
  version = '0.0.1'
  afterEvaluate {
    configure(allProcessResourcesTasks()) {
        filter(ReplaceTokens,
                tokens: [version: project.version, gradleVersion: project.gradle.gradleVersion])
    }
}
  def allProcessResourcesTasks() {
    sourceSets.all.processResourcesTaskName.collect {
        tasks[it]
    }
}

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.

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

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 {
   url='jdbc:oracle:thin:@DEV'
  }
    host='DEV'
  port='25'
      domain='localhost:8080/...'
  prototype='dev/....'
 }
     test {
  jdbc {
   url='jdbc:oracle:thin:@TEST'
  }
  host='TEST'
  port='25'
  domain='test....'
  prototype='test..'
 }
     prod {
  jdbc {
   url='jdbc:oracle:thin:@PROD'
  }
  host='prod'
  port='25'
  domain='www....'
  prototype='www...'
 }
}

Kind regards Mark.

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('gradle.sbs.properties')
 ext.replacements = new ConfigSlurper(environment).parse(configFile.toURL())
    println "jdbc.url:
$replacements.jdbcurl"
 println "mailhost:
$replacements.mailhost"
 println "mailport:
$replacements.mailport"
 println "domain:
$replacements.domain"
 println "prototype:
$replacements.prototype"
       // this would be the "normal way to do things:
 // filter(ReplaceTokens, tokens: replacements)
    // but because of http://issues.gradle.org/browse/GRADLE-1566
 // 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' }
   }

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’.”

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

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.

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

Here is the build.gradle file as it currently is: https://gist.github.com/smbarbour/9649918

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.

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…