Dec 11, 2014

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

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.

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:

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

About the Author

Object Partners profile.

One thought on “Copying multiple directories in a single Gradle task with up-to-date checking

  1. Jesse says:

    How were u able to use ${bowerComponentsDir}? where did you define the variable.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Android Development for iOS Developers
Android development has greatly improved since the early days. Maybe you tried it out when Android development was done in Eclipse, emulators were slow and buggy, and Java was the required language. Things have changed […]
Add a custom object to your Liquibase diff
Adding a custom object to your liquibase diff is a pretty simple two step process. Create an implementation of DatabaseObject Create an implementation of SnapshotGenerator In my case I wanted to add tracking of Stored […]
Keeping Secrets Out of Terraform State
There are many instances where you will want to create resources via Terraform with secrets that you just don’t want anyone to see. These could be IAM credentials, certificates, RDS DB credentials, etc. One problem […]
Validating Terraform Plans using Open Policy Agent
When developing infrastructure as code using terraform, it can be difficult to test and validate changes without executing the code against a real environment. The feedback loop between writing a line of code and understanding […]