# Copy task replacing symbolic link contents

**URL:** <https://discuss.gradle.org/t/copy-task-replacing-symbolic-link-contents/50957>\
**Category:** Help/Discuss\
**Created:** [May 7, 2025, 7:00am UTC](https://discuss.gradle.org/t/copy-task-replacing-symbolic-link-contents/50957 "2025-05-07T07:00:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arthenice](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@arthenice](https://discuss.gradle.org/u/arthenice)\
**Post date:** [May 7, 2025, 7:00am UTC](https://discuss.gradle.org/t/copy-task-replacing-symbolic-link-contents/50957/1 "2025-05-07T07:00:07Z")

</div>

The following behaviour was observed under Windows 10 and Gradle 8.14.

Assuming the following situation:

One directory with regular files, like text files.  
A Second directory with symbolic links pointing to the files in the first directory.  
A third directory with a Gradle project containing a custom task to copy files into the second directory.

```gradle
tasks.register<Copy>("copyDirectoryContents") {
    from("docs")
    into("E:\\workspace\\gradle_copy_bug\\target_directory")
}

```

When performing the task and there is a symbolic link with the same name as a file from the project, instead of overriding the symbolic link with the regular file the content of the file where the symbolic links is pointing to is being replaced.

Before executing task:

```gradle
|- dir_one
| |- test.txt (with content: "Hello world!")
|- dir_two
| |- test.txt (symbolic link pointing to dir_one/test.txt)
|- dir_three
| |- docs
| |- test.txt (with content: "Hello from Gradle directory!")

```

After executing task:

```gradle
|- dir_one
| |- test.txt (with content: "Hello from Gradle directory!")
|- dir_two
| |- test.txt (symbolic link pointing to dir_one/test.txt)
|- dir_three
| |- docs
| |- test.txt (with content: "Hello from Gradle directory!")

```

Expected behaviour:

```gradle
|- dir_one
| |- test.txt (with content: "Hello world!")
|- dir_two
| |- test.txt (with content: "Hello from Gradle directory!")
|- dir_three
| |- docs
| |- test.txt (with content: "Hello from Gradle directory!")

```

Simply deleting the target directory before the task is being called is unfortunately not an option, as there are other files in there that need to be preserved.

Is my expectation of that behaviour wrong or is there a possibility to configure the copy task to perform in the expected way?

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [May 7, 2025, 3:41pm UTC](https://discuss.gradle.org/t/copy-task-replacing-symbolic-link-contents/50957/2 "2025-05-07T15:41:17Z")

</div>

Your observation is correct and unfortunately this is not currently configurable.  
The `Copy` task uses `FileOutputStream` to write the copied file and that always goes to the target of symbolic links.

So what you can do is opening a feature request to make this configurable.  
The newer `java.nio` APIs could be configured what to do with symbolic links.

As a work-around you can probably use `eachFile { ... }` and in there delete the `path` if it exists, so that when the actual copy is done, the symbolic link is gone already.

---

<div class="post-metadata">

**Author:** ![arthenice](https://avatars.discourse-cdn.com/v4/letter/a/d2c977/32.png) [@arthenice](https://discuss.gradle.org/u/arthenice)\
**Post date:** [May 7, 2025, 5:12pm UTC](https://discuss.gradle.org/t/copy-task-replacing-symbolic-link-contents/50957/3 "2025-05-07T17:12:05Z")

</div>

I will try the idea with the `eachFile`.  
With that I should be able to deal with the problem.  
Thank you very much (:

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [May 7, 2025, 6:05pm UTC](https://discuss.gradle.org/t/copy-task-replacing-symbolic-link-contents/50957/4 "2025-05-07T18:05:15Z")

</div>

Might still warrant a feature request though even if you can work-around it. 🙂  
After you opened one, please post a link here for reference.
