A weird class cannot be casted to class 'java.io.File'

The structure of my project is similar to the following:

-- root
  -- gradlew
  -- build.gradle
  -- src
     -- main
       -- frontend
          -- webpack.config.js
          -- package.json
          -- src
          -- node_modules
       -- java
       -- resources
          -- static

I’d like the gradle to get run the webpack.config.js located inside the root/src/main/frontend folder, and write it to bundle.js in src/main/java/resources/static. I tried this script:

task myNpmInstall(type: NodeTask) {
	args = ['install']
    inputs.file { file("$projectDir/src/main/frontend/package.json") }
    outputs.dir { file("$projectDir/src/main/frontend/node_modules") }
}

task webpack(type: NodeTask, dependsOn: 'myNpmInstall') {
	script = {file("$projectDir/src/main/frontend/node_modules/.bin/webpack")}
}

I get this error:

Cannot cast object 'build_6ueikf0cuxfrzfgsftzoyoi48$_run_closure5$_closure11@4ee37d59' with class 'build_6ueikf0cuxfrzfgsftzoyoi48$_run_closure5$_closure11' to class 'java.io.File'

This is the stack trace:

Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'build_6ueikf0cuxfrzfgsftzoyoi48$_run_closure5$_closure11@4ee37d59' with class 'build_6ueikf0cuxfrzfgsftzoyoi48$_run_closure5$_closure11' to class 'java.io.File'
        at org.gradle.internal.metaobject.BeanDynamicObject$MetaClassAdapter.setProperty(BeanDynamicObject.java:375)
        at org.gradle.internal.metaobject.BeanDynamicObject.trySetProperty(BeanDynamicObject.java:176)
        at org.gradle.internal.metaobject.CompositeDynamicObject.trySetProperty(CompositeDynamicObject.java:66)
        at org.gradle.internal.metaobject.ConfigureDelegate.setProperty(ConfigureDelegate.java:97)
        at build_6ueikf0cuxfrzfgsftzoyoi48$_run_closure5.doCall(/home/arian/workspace/cut-costs/build.gradle:53)
        at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:71)
        at org.gradle.util.ConfigureUtil.configureTarget(ConfigureUtil.java:160)
        at org.gradle.util.ConfigureUtil.configureSelf(ConfigureUtil.java:136)
        at org.gradle.api.internal.AbstractTask.configure(AbstractTask.java:638)
        at org.gradle.api.internal.project.DefaultProject.task(DefaultProject.java:1198)
        at org.gradle.internal.metaobject.BeanDynamicObject$MetaClassAdapter.invokeMethod(BeanDynamicObject.java:479)
        at org.gradle.internal.metaobject.BeanDynamicObject.tryInvokeMethod(BeanDynamicObject.java:191)
        at org.gradle.internal.metaobject.CompositeDynamicObject.tryInvokeMethod(CompositeDynamicObject.java:98)
        at org.gradle.internal.metaobject.MixInClosurePropertiesAsMethodsDynamicObject.tryInvokeMethod(MixInClosurePropertiesAsMethodsDynamicObject.java:30)
        at org.gradle.groovy.scripts.BasicScript$ScriptDynamicObject.tryInvokeMethod(BasicScript.java:134)
        at org.gradle.internal.metaobject.AbstractDynamicObject.invokeMethod(AbstractDynamicObject.java:160)
        at org.gradle.groovy.scripts.BasicScript.invokeMethod(BasicScript.java:83)
        at build_6ueikf0cuxfrzfgsftzoyoi48.run(/home/arian/workspace/cut-costs/build.gradle:52)
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:90)
        ... 93 more

The script property for a NodeTask only allows a File. It does not accept any arbitrary Object that can be resolved to a File. You’ve tried to set script to a closure. A closure becomes a class (name is generated) and that class cannot be cast to a File.

What should be done if I want to run the webpack “closure” (which is a file) using a task ? Is there any other property I can use ?

I don’t see any reason you need a closure. Just set script directly to the file instead of wrapping it in the closure.

I don’t know what you mean by closure. But it is the “webpack” file.

#!/usr/bin/env node

/*
	MIT License http://www.opensource.org/licenses/mit-license.php
	Author Tobias Koppers @sokra
*/
var path = require("path");

// Local version replace global one
try {
	var localWebpack = require.resolve(path.join(process.cwd(), "node_modules", "webpack", "bin", "webpack.js"));
	if(__filename !== localWebpack) {
		return require(localWebpack);
	}
} catch(e) {}
var yargs = require("yargs")
	.usage("webpack " + require("../package.json").version + "\n" +
		"Usage: https://webpack.js.org/api/cli/\n" +
		"Usage without config file: webpack <entry> [<entry>] <output>\n" +
		"Usage with config file: webpack");

require("./config-yargs")(yargs);

var DISPLAY_GROUP = "Stats options:";
var BASIC_GROUP = "Basic options:";

yargs.options({
	"json": {
		type: "boolean",
		alias: "j",
		describe: "Prints the result as JSON."
	},
	"progress": {
		type: "boolean",
		describe: "Print compilation progress in percentage",
		group: BASIC_GROUP
	},
	"color": {
		type: "boolean",
		alias: "colors",
		default: function supportsColor() {
			return require("supports-color");
		},
		group: DISPLAY_GROUP,
		describe: "Enables/Disables colors on the console"
	},
	"sort-modules-by": {
		type: "string",
		group: DISPLAY_GROUP,
		describe: "Sorts the modules list by property in module"
	},
	"sort-chunks-by": {
		type: "string",
		group: DISPLAY_GROUP,
		describe: "Sorts the chunks list by property in chunk"
	},
	"sort-assets-by": {
		type: "string",
		group: DISPLAY_GROUP,
		describe: "Sorts the assets list by property in asset"
	},
	"hide-modules": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Hides info about modules"
	},
	"display-exclude": {
		type: "string",
		group: DISPLAY_GROUP,
		describe: "Exclude modules in the output"
	},
	"display-modules": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display even excluded modules in the output"
	},
	"display-max-modules": {
		type: "number",
		group: DISPLAY_GROUP,
		describe: "Sets the maximum number of visible modules in output"
	},
	"display-chunks": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display chunks in the output"
	},
	"display-entrypoints": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display entry points in the output"
	},
	"display-origins": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display origins of chunks in the output"
	},
	"display-cached": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display also cached modules in the output"
	},
	"display-cached-assets": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display also cached assets in the output"
	},
	"display-reasons": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display reasons about module inclusion in the output"
	},
	"display-depth": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display distance from entry point for each module"
	},
	"display-used-exports": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display information about used exports in modules (Tree Shaking)"
	},
	"display-provided-exports": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display information about exports provided from modules"
	},
	"display-optimization-bailout": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display information about why optimization bailed out for modules"
	},
	"display-error-details": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Display details about errors"
	},
	"display": {
		type: "string",
		group: DISPLAY_GROUP,
		describe: "Select display preset (verbose, detailed, normal, minimal, errors-only, none)"
	},
	"verbose": {
		type: "boolean",
		group: DISPLAY_GROUP,
		describe: "Show more details"
	}
});

// yargs will terminate the process early when the user uses help or version.
// This causes large help outputs to be cut short (https://github.com/nodejs/node/wiki/API-changes-between-v0.10-and-v4#process).
// To prevent this we use the yargs.parse API and exit the process normally
yargs.parse(process.argv.slice(2), (err, argv, output) => {

	// arguments validation failed
	if(err && output) {
		console.error(output);
		process.exitCode = 1;
		return;
	}

	// help or version info
	if(output) {
		console.log(output);
		return;
	}

	if(argv.verbose) {
		argv["display"] = "verbose";
	}

	var options = require("./convert-argv")(yargs, argv);

	function ifArg(name, fn, init) {
		if(Array.isArray(argv[name])) {
			if(init) init();
			argv[name].forEach(fn);
		} else if(typeof argv[name] !== "undefined") {
			if(init) init();
			fn(argv[name], -1);
		}
	}

	function processOptions(options) {
		// process Promise
		if(typeof options.then === "function") {
			options.then(processOptions).catch(function(err) {
				console.error(err.stack || err);
				process.exit(1); // eslint-disable-line
			});
			return;
		}

		var firstOptions = [].concat(options)[0];
		var statsPresetToOptions = require("../lib/Stats.js").presetToOptions;

		var outputOptions = options.stats;
		if(typeof outputOptions === "boolean" || typeof outputOptions === "string") {
			outputOptions = statsPresetToOptions(outputOptions);
		} else if(!outputOptions) {
			outputOptions = {};
		}

		ifArg("display", function(preset) {
			outputOptions = statsPresetToOptions(preset);
		});

		outputOptions = Object.create(outputOptions);
		if(Array.isArray(options) && !outputOptions.children) {
			outputOptions.children = options.map(o => o.stats);
		}
		if(typeof outputOptions.context === "undefined")
			outputOptions.context = firstOptions.context;

		ifArg("env", function(value) {
			if(outputOptions.env) {
				outputOptions._env = value;
			}
		});

		ifArg("json", function(bool) {
			if(bool)
				outputOptions.json = bool;
		});

		if(typeof outputOptions.colors === "undefined")
			outputOptions.colors = require("supports-color");

		ifArg("sort-modules-by", function(value) {
			outputOptions.modulesSort = value;
		});

		ifArg("sort-chunks-by", function(value) {
			outputOptions.chunksSort = value;
		});

		ifArg("sort-assets-by", function(value) {
			outputOptions.assetsSort = value;
		});

		ifArg("display-exclude", function(value) {
			outputOptions.exclude = value;
		});

		if(!outputOptions.json) {
			if(typeof outputOptions.cached === "undefined")
				outputOptions.cached = false;
			if(typeof outputOptions.cachedAssets === "undefined")
				outputOptions.cachedAssets = false;

			ifArg("display-chunks", function(bool) {
				if(bool) {
					outputOptions.modules = false;
					outputOptions.chunks = true;
					outputOptions.chunkModules = true;
				}
			});

			ifArg("display-entrypoints", function(bool) {
				if(bool)
					outputOptions.entrypoints = true;
			});

			ifArg("display-reasons", function(bool) {
				if(bool)
					outputOptions.reasons = true;
			});

			ifArg("display-depth", function(bool) {
				if(bool)
					outputOptions.depth = true;
			});

			ifArg("display-used-exports", function(bool) {
				if(bool)
					outputOptions.usedExports = true;
			});

			ifArg("display-provided-exports", function(bool) {
				if(bool)
					outputOptions.providedExports = true;
			});

			ifArg("display-optimization-bailout", function(bool) {
				if(bool)
					outputOptions.optimizationBailout = bool;
			});

			ifArg("display-error-details", function(bool) {
				if(bool)
					outputOptions.errorDetails = true;
			});

			ifArg("display-origins", function(bool) {
				if(bool)
					outputOptions.chunkOrigins = true;
			});

			ifArg("display-max-modules", function(value) {
				outputOptions.maxModules = +value;
			});

			ifArg("display-cached", function(bool) {
				if(bool)
					outputOptions.cached = true;
			});

			ifArg("display-cached-assets", function(bool) {
				if(bool)
					outputOptions.cachedAssets = true;
			});

			if(!outputOptions.exclude)
				outputOptions.exclude = ["node_modules", "bower_components", "components"];

			if(argv["display-modules"]) {
				outputOptions.maxModules = Infinity;
				outputOptions.exclude = undefined;
				outputOptions.modules = true;
			}
		}

		ifArg("hide-modules", function(bool) {
			if(bool) {
				outputOptions.modules = false;
				outputOptions.chunkModules = false;
			}
		});

		var webpack = require("../lib/webpack.js");

		Error.stackTraceLimit = 30;
		var lastHash = null;
		var compiler;
		try {
			compiler = webpack(options);
		} catch(e) {
			var WebpackOptionsValidationError = require("../lib/WebpackOptionsValidationError");
			if(e instanceof WebpackOptionsValidationError) {
				if(argv.color)
					console.error("\u001b[1m\u001b[31m" + e.message + "\u001b[39m\u001b[22m");
				else
					console.error(e.message);
				process.exit(1); // eslint-disable-line no-process-exit
			}
			throw e;
		}

		if(argv.progress) {
			var ProgressPlugin = require("../lib/ProgressPlugin");
			compiler.apply(new ProgressPlugin({
				profile: argv.profile
			}));
		}

		function compilerCallback(err, stats) {
			if(!options.watch || err) {
				// Do not keep cache anymore
				compiler.purgeInputFileSystem();
			}
			if(err) {
				lastHash = null;
				console.error(err.stack || err);
				if(err.details) console.error(err.details);
				process.exit(1); // eslint-disable-line
			}
			if(outputOptions.json) {
				process.stdout.write(JSON.stringify(stats.toJson(outputOptions), null, 2) + "\n");
			} else if(stats.hash !== lastHash) {
				lastHash = stats.hash;
				var statsString = stats.toString(outputOptions);
				if(statsString)
					process.stdout.write(statsString + "\n");
			}
			if(!options.watch && stats.hasErrors()) {
				process.exitCode = 2;
			}
		}
		if(firstOptions.watch || options.watch) {
			var watchOptions = firstOptions.watchOptions || firstOptions.watch || options.watch || {};
			if(watchOptions.stdin) {
				process.stdin.on("end", function() {
					process.exit(); // eslint-disable-line
				});
				process.stdin.resume();
			}
			compiler.watch(watchOptions, compilerCallback);
			console.log("\nWebpack is watching the files…\n");
		} else
			compiler.run(compilerCallback);

	}

	processOptions(options);

});

It’s a pretty core concept in Groovy, the language build.gradle is written in.
You might want to read at least part of The Apache Groovy programming language - Closures

You’re setting script to a closure. The closure would evaluate to a File, but script doesn’t take a closure so it tries to cast the closure to File.

task webpack(type: NodeTask, dependsOn: 'myNpmInstall') {
	script = {file("$projectDir/src/main/frontend/node_modules/.bin/webpack")}
}

Remove the closure (curly braces) and set script to just the file:

task webpack(type: NodeTask, dependsOn: 'myNpmInstall') {
	script = file("$projectDir/src/main/frontend/node_modules/.bin/webpack")
}

I see now ! I the problem was that I got error before even getting to the webpack task, I got error for the myNpmInstall ! I changed it to:

node {
    version = '8.9.1'
    npmWorkDir = file('src/main/frontend')
    nodeModulesDir = file('src/main/frontend')
    download = true
}

task webpack(type: NodeTask, dependsOn: 'npmInstall') {
	script = file("$projectDir/src/main/frontend/node_modules/.bin/webpack")
}

processResources.dependsOn 'webpack'

and now I get another error, which I don’t think is related to gradle:

Execution failed for task ':webpack' Caused by: org.gradle.process.internal.ExecException: Process 'command '/home/arian/workspace/myproj/.gradle/nodejs/node-v8.9.1-lin‌​ux-x64/bin/node'' finished with non-zero exit value 255