Very basic copy task not working

I’ve read chapters 1 and 2 of “Building and Testing with Gradle.” Once I encountered the Copy task, I tried to experiment with it because it would be very useful in my current project. I can’t seem to get even the most basic copy task to succeed:

task copyFiles(type: Copy) {
def folder = new File(project.projectDir.toString() + ‘/assets’)
def folder2 = new File(project.projectDir.toString() + ‘/assets2’)
println folder.exists()
println folder.toString()
println folder2.exists()
println folder2.toString()
from folder.toString()
into folder2.toString()
println “dbg1”
}

I can see both of these directories exist, however none of the contents of assets get copied into assets2. I can see all the printlns when I run any task, since it is running during the configuration phase. No errors are produced. I tried passing in -d to gradle but this task is completely silent about any errors. I understand this task should recursively process the directories and all files underneath, however like said, nothing happens.

I initially was trying to copy from the root directory of my project into a sub project—but I thought I read somewhere there’s a problem with copying into a project during the configuration phase? (which would be weird…), so I was trying to dial this back and just copy some files around…SOMEWHERE, but it doesn’t work. What am I doing wrong…?

This works.

task copyFiles(type: Copy) {
  // Copy the content of directory 'assets'
  from fileTree(dir: 'assets')

  // ... into another directory named 'assets2'
  into 'assets2'
}
task copyFiles(type: Copy) {
  from 'assets'
  into 'assets2'
}

Works just fine for me. Your code works as well (don’t know why you’re using all those toString() calls though). Could you please provide a reproducible example project on GitHub?

It turns out this was a fundamental misunderstanding I had of tasks. I thought that:

task copyFiles(type: Copy) {
from 'assets’
into ‘assets2’
}

Implied doing copying during the configuration phase, but apparently this task can only work during the execution phase. I made the “assemble” task depend on my copy task and it worked.

I further learned if I want to copy files during the configuration phase (which I probably don’t, but if I did…), I needed to add a block like this within any other configuration block:

copy {
from: directory1
into: directory2
}

Thanks, all.