Problem with org.gradle.api.tasks.Copy

(tulsoba) #1

I am trying to call the gradle copy task inside a custom gradle task that I have written in java:

Copy copy = new Copy();

but when I run the above task I get:

Task of type ‘org.gradle.api.tasks.Copy’ has been instantiated directly which is not supported. Tasks can only be created using the DSL.

What is the correct way to call the copy task from gradle? Or is it better to simply use common-io instead?

(Alexander Haskell) #2

If I understand you correctly, you are trying to do copy in a .gradle script from a .java file that you are including?

Unless I am missing something from your specific case, you should be using a copy task directly in a .gradle script and not from .java file. Which is what the error is saying.

Doing a copy task is explained here:

To do a copy from within a task (in a .gradle script) just do something like:

task myTask {
    // ...
    copy {
        from 'path/to/sourceDir'
        into '/path/to/destDir'
    // ...

Just set the copy to occur after your classes get generated with a dependsOn:

task myTask (dependsOn: compileJava) {
    // ...
    copy {
        // ...
    // ...

Hope that answers your question.

(Andrew Oberstar) #3

You can do the same thing Alexander proposes within your .java file, as well. In that case however, you will need to call ‘project.copy’.

(Alexander Haskell) #4

Ah, good to know. I’ve been recently looking at mixing Gradle and Java but can’t find documentation about the conventions of doing it.

(Andrew Oberstar) #5

I have found the best resource to be the code for Gradle itself. Takes a little bit to get used to where things are located, but a great example of good Gradle practices.

(tulsoba) #6

Yes that was recommended in a previous post:

but requires defining Closures (which I never got to work) which is a lot less intuitive than simply calling, dest).