Skip to content

CopySpec.eachFile(…) Action not Applied to Copied Directories #2940

@cspurk

Description

@cspurk

Expected Behavior

The action given to CopySpec.eachFile(…) should also be applied to each copied directory – at least when includeEmptyDirs == true.

Current Behavior

The action given to CopySpec.eachFile(…) is only applied to normal files, even if includeEmptyDirs == true.

Context

In shell terms, I would like to do something as simple as cp -r source/*/* target/ (i.e., copy all contents of the subdirectories of source to target – including possibly empty directories). Unfortunately, I couldn’t find any comparably simple way to achieve this in a Gradle Copy task, so I tried to remove the leading subdirectory path segment from each copied file in a CopySpec.eachFile(…) action.

Steps to Reproduce (for bugs)

Try the following bash script to quickly set up and execute a simple sample build:

mkdir test
cd test
mkdir -p source/empty_dir source/path/to/other_empty_dir source/some_dir
touch source/some_dir/file.txt
gradle wrapper --gradle-version 4.1
cat > build.gradle <<'EOF'
task foo(type: Copy) {
    from 'source'
    into 'target'
    includeEmptyDirs = true
    eachFile { println "SEEN: $it" }
}
EOF
./gradlew foo

It prints the following:

> Task :foo
SEEN: file '/tmp/test/source/some_dir/file.txt'


BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed

I would expect to also get “SEEN” lines for at least the empty directories. They are definitely copied:

$ tree target/
target/
├── empty_dir
├── path
│   └── to
│       └── other_empty_dir
└── some_dir
    └── file.txt

5 directories, 1 file

Your Environment

$ ./gradlew -version

------------------------------------------------------------
Gradle 4.1
------------------------------------------------------------

Build time:   2017-08-07 14:38:48 UTC
Revision:     941559e020f6c357ebb08d5c67acdb858a3defc2

Groovy:       2.4.11
Ant:          Apache Ant(TM) version 1.9.6 compiled on June 29 2015
JVM:          1.8.0_131 (Oracle Corporation 25.131-b11)
OS:           Linux 4.4.0-93-generic amd64

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:featureA new functionalityin:file-taskscopy sync zip tar rename delete

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions