Running custom task results in 'cannot be cast to java.lang.Class'

In my JVM project, I want to use the gradle-js-plugin, but I want to minify each js file individually rather than concatenate them all into a single output. I’ve created a series of custom tasks as follows:

def allJsFiles = fileTree('web/public') {
    include '**/*.js'
    exclude '**/*.min.js'
task jsAll
allJsFiles.each {File jsFile ->
    def taskName = jsFile.toString().replace(projectDir.toString(),'').replaceAll('\\.', '')
    task("jsFiles-${taskName}", type: minifyJs, description: "test") {
        def srcFileName = jsFile.toString().replace(projectDir.toString(),'')
        def srcFile = file(srcFileName)
        def destFile = file("${buildDir}/${srcFileName}")
        source = srcFile
        dest = destFile

Running the jsAll task gives this result:

> com.eriwen.gradle.js.tasks.MinifyJsTask_Decorated cannot be cast to java.lang.Class

Help - What am I doing wrong?

The task type you’re using when trying to create each task is incorrect. In the build script, minifyJs represents a task of type com.eriwen.gradle.js.tasks.MinifyJsTask that has been created by the plugin. The type needs to be a task class, not an instance of a class, which is why the error says it cannot cast from the runtime type of the task instance to java.lang.Class.

Use com.eriwen.gradle.js.tasks.MinifyJsTask instead of minifyJs.

Ah - ok. Makes sense. Thank you!