Gradle - copy directory - problem

I have a problem copying files in gradle. Here is a build.gradle :

 task mydir() { new File("build/models").mkdir() } task copyTask(dependsOn:mydir, type: Copy){ from 'src/test/groovy/models' into 'build/models' from 'src/test/groovy/test.groovy' into 'build/' } 

The problem here is in the first instance, i.e. test.groovy is copied to the assembly. But the first copy, where I copy all the files in the src /../ models to the assembly / models, does not work. It just copies files only to the assembly directory. My assemblies / models remain empty. Can someone please tell me what I am doing wrong? I also have a separate task to create the build / models directory before copyTask is executed.

+7
directory gradle
source share
2 answers

The script line contains the following errors:

  • A directory is created when mydir configured, and not when it is running. (This means that a directory will be created each time the assembly is called, no matter what tasks are performed.)
  • new File('some/path') creates the path relative to the current working directory of the Gradle process, and not the path to the project directory. Always use project.file() .
  • The Copy task can contain only one top level into .

Here is the corrected version:

 task mydir { doLast { mkdir('build/models') } } task copyTask(dependsOn: mydir, type: Copy) { into 'build' from 'src/test/groovy/test.groovy' into('models') { from 'src/test/groovy/models' } } 

The Copy task will automatically create build/models , so there is no need to have a mydir task. It is odd to use build as the target directory of the Copy task (you should use a subdirectory).

+11
source share

In gradle, a copy task can have only one goal in several with several.

The second "in" overrides the first value "in" and why it is only copied to "build /" not "build / models"

You might want to have a separate copy task or use ant.copy instead.

-one
source share

All Articles