Copying multiple directories in a single Gradle task with up-to-date checking

How to write a single Gradle task to copy multiple directories and still use up-to-date checking..

Object Partners

Suppose you wanted to copy several directories of Twitter Bootstrap styling and JavaScript, each one into a different destination directory. And you wanted to use Gradle’s up-to-date checking with defined inputs and outputs to only run the task when needed, i.e. when a task input or output changed. Gradle has a built-in Copy task type, but that only supports a single destination directory. Now if you didn’t want to define a separate Gradle task for each directory, how can you copy multiple directories in a single Gradle task?

Maybe the first way you find is to use Gradle’s append syntax to define the task.

task copyBootstrap << {
    copy {
        from "${bowerComponentsDir}/bootstrap-sass-official/assets/fonts"
        into 'theme/fonts/vendor'
    }
    copy {
        from "${bowerComponentsDir}/jquery/dist"
        into 'theme/javascripts/vendor/jquery'
    }
    copy {
        from "${bowerComponentsDir}/bootstrap/dist/js"
        into 'theme/javascripts/vendor/bootstrap'
    }
    copy {
        from "${bowerComponentsDir}/bootstrap-sass-official/assets/stylesheets"
        into 'theme/sass/vendor/bootstrap'
    }
}

But if you try to define task inputs and outputs with the above task type, Gradle will warn you in the console that up-to-date checking wasn’t supported in this task.

Don’t despair, there is a solution! You can use a doLast closure inside the Gradle task to be able to copy multiple directories and still use up-to-date checking. This is what the resulting task might look like with up-to-date checking enabled:

task copyBootstrap {
    inputs.file 'bower.json'
    inputs.dir 'bower_components/bootstrap'
    inputs.dir 'bower_components/bootstrap-sass-official'

    outputs.dir 'theme/fonts/vendor/bootstrap'
    outputs.dir 'theme/javascripts/vendor/jquery'
    outputs.dir 'theme/javascripts/vendor/bootstrap'
    outputs.dir 'theme/sass/vendor/bootstrap'

    doLast {
        copy {
            from "${bowerComponentsDir}/bootstrap-sass-official/assets/fonts"
            into 'theme/fonts/vendor'
        }
        copy {
            from "${bowerComponentsDir}/jquery/dist"
            into 'theme/javascripts/vendor/jquery'
        }
        copy {
            from "${bowerComponentsDir}/bootstrap/dist/js"
            into 'theme/javascripts/vendor/bootstrap'
        }
        copy {
            from "${bowerComponentsDir}/bootstrap-sass-official/assets/stylesheets"
            into 'theme/sass/vendor/bootstrap'
        }
    }
}

Now you have a single task that will copy multiple directories and will only run when one of those defined inputs or outputs changes!

Share this Post

Related Blog Posts

JVM

Case-insensitive criteria ordering on child properties

December 3rd, 2014

A workaround for a couple of Grails 2.x bugs and changes that have made it difficult to sort criteria case-insensitively when using child/nested properties.

Igor Shults
JVM

Vert.x Fat Jar Deployments

November 25th, 2014

Fat jar deployments in Vert.x can simplify the delivery of your software. One drawback is limited options for module dependencies, which this post will address.

Object Partners
JVM

UDP Server with Spring Boot and Reactor

November 18th, 2014

A tutorial and accompanying project for creating a simple UDP server with Spring Boot and Reactor.

Object Partners

About the author