Copy single file


#1

I’ve this task:

task createDemoWar(type: War, dependsOn: classes) {
    archiveName "webapi-demo-${versioning.info.display}.war"
    destinationDir = file("$buildDir/dist")
    copy {
        from 'scopes'
        include 'configuration.demo.properties'
        rename { String fileName ->
            fileName.replace('configuration.demo.properties', 'scope.properties')
        }
        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: configuration.demo.properties, configuration.dev.properties, configuration.testing.properties and configuration.production.properties.

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

Any ideas?


(Janito Vaqueiro Ferreira Filho) #2

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: