Writing and using a custom task with third party dependencies

I have a custom task in a sub project build script in Groovy. It uses a third party library components FileUtils from commons-io. In my build script I’m getting the following error on the import line: unable to resolve class org.apache.commons.io.FileUtils.

The script is in a sub project. I tried adding the following to the sub project script without success:

apply pluing: ‘java’

apply plugin: ‘war’

buildScript {

dependencies {

classpath gradleApi()

classpath localGroovy()

classpath ‘commons-io:commons-io:2.0.1’



import org.apache.commons.io.FileUtils

class IncrementalSync extends DefaultTask {


def FileCollection src


def File destination


void execute(IncrementalTaskInputs inputs) {

if (!inputs.incremental) {



inputs.outOfDate({ change ->

FileUtils.copyFile(change.file, targetFile(change.file))

} as Action)

inputs.removed({ change ->


} as Action)


def targetFile(def inputFile) {

new File(destination, change.file.name)



It’s ‘buildscript’, not ‘buildScript’ (you’ll get an error for that). Also, the ‘buildscript’ block needs to have a ‘repositories’ block, and it’s never necessary to declare ‘localGroovy’ or ‘gradleApi’ in a ‘buildscript’'s ‘dependencies’ block.

Thanks, fixed name and added repositories and that fixed the problem.

But I have a follow up question. I configured repositories in the root build script inside allprojects. Is there a way to reuse that configuration, or must I repeat the block in my sub project script?

One ‘buildscript’ block at the top level of your root project’s ‘build.gradle’ is typically good enough. If that block needs the same repository declaration, the easiest solution is to repeat it (once).