# Problem with org.gradle.api.tasks.Copy

**URL:** <https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518>\
**Category:** Old Forum Archive\
**Created:** [November 19, 2012, 6:10pm UTC](https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518 "2012-11-19T18:10:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![u1234](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@u1234](https://discuss.gradle.org/u/u1234)\
**Post date:** [November 19, 2012, 6:10pm UTC](https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518/1 "2012-11-19T18:10:00Z")

</div>

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

```gradle
Copy copy = new Copy();
    copy.from(genClasses.getPath());
    copy.setDestinationDir(outputFolder);
    copy.execute();

```

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?

---

<div class="post-metadata">

**Author:** ![Alexander\_Haskell](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Alexander\_Haskell](https://discuss.gradle.org/u/Alexander_Haskell)\
**Post date:** [November 19, 2012, 6:51pm UTC](https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518/2 "2012-11-19T18:51:00Z")

</div>

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:

[http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html](http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html)

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

```gradle
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:

```gradle
task myTask (dependsOn: compileJava) {
    // ...
    copy {
        // ...
    }
    // ...
}

```

Hope that answers your question.

---

<div class="post-metadata">

**Author:** ![ajoberstar](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ajoberstar/32/127_2.png) [@ajoberstar](https://discuss.gradle.org/u/ajoberstar)\
**Post date:** [November 21, 2012, 2:03pm UTC](https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518/3 "2012-11-21T14:03:00Z")

</div>

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’.

---

<div class="post-metadata">

**Author:** ![Alexander\_Haskell](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Alexander\_Haskell](https://discuss.gradle.org/u/Alexander_Haskell)\
**Post date:** [November 21, 2012, 2:26pm UTC](https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518/4 "2012-11-21T14:26:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ajoberstar](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ajoberstar/32/127_2.png) [@ajoberstar](https://discuss.gradle.org/u/ajoberstar)\
**Post date:** [November 22, 2012, 12:11am UTC](https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518/5 "2012-11-22T00:11:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![u1234](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@u1234](https://discuss.gradle.org/u/u1234)\
**Post date:** [November 22, 2012, 7:40am UTC](https://discuss.gradle.org/t/problem-with-org-gradle-api-tasks-copy/6518/6 "2012-11-22T07:40:00Z")

</div>

Yes that was recommended in a previous post:

[http://forums.gradle.org/gradle/topics/calling\_build\_in\_task\_from\_a\_custom\_task](http://forums.gradle.org/gradle/topics/calling_build_in_task_from_a_custom_task)

but requires defining Closures (which I never got to work) which is a lot less intuitive than simply calling o.a.commons.io.FileUtils.copy(src, dest).
