Copy single file

I’ve this task:

task createDemoWar(type: War, dependsOn: classes) {
    archiveName "webapi-demo-${}.war"
    destinationDir = file("$buildDir/dist")
    copy {
        from 'scopes'
        include ''
        rename { String fileName ->
            fileName.replace('', '')
        into 'src/main/resources/'

As you can see I’m trying to copy one single file from scopes folter to /src/main/resources.
In scopes folder there are 4 files. The four files are:,, and

After having run the task a is created however with the content of the fourth file of scopes folder.

Any ideas?

I believe the solution is the same as in the other thread. Replace copy with either into or from. The rename call should also work with into and from.

The explanation is more detailed in the link, but in summary the copy used doesn’t copy into the War, but actually copies a file to src/main/resources when the task is created (not executed). This means that if you have more than one task copying to the same destination, the last task created will overwrite the file copied by the previous task(s).

The solution is to use the CopyTask's helper methods to define what should be copied into the archive. You can use into to specify the destination path inside the archive, or use from and add it to the root of the archive.

Hope this helps :slight_smile:

1 Like