Daniil Shevelev
Daniil Shevelev

Reputation: 12017

Gradle copy resources, processResources

I am trying to copy 2 folders from my project folder into the jar. When I use following settings Gradle copies the files inside the folders, not the folders themself:

processResources {
    from 'public'
    from 'data'
}

What I need is to have folder "projectDir/data" copyied into "my.jar/data".

Upvotes: 13

Views: 31355

Answers (2)

Steven Spungin
Steven Spungin

Reputation: 29071

There is a method of 'from' that takes a closure with CopySpec.

I would recommend using this instead of either breaking up into separate tasks or restructuring your layout.

processResources {
    from('data') {into 'data'}
    from('publicFolder') {into 'publicFolder'}
}

Upvotes: 11

Invisible Arrow
Invisible Arrow

Reputation: 4905

The copy spec in Gradle copies contents of a folder if the specified path in from is a folder.

One solution is to specify the target folder in the jar using the into directive.

processResources {
    from 'data'
    from 'publicFolder'
}

task data(type: Copy) {
    from 'data' into 'data'
}

task publicFolder(type: Copy) {
    from 'public' into 'public'
}

This will put the contents or the original folders into the folder with the same name within the jar. But as you can see, you'll need to repeat the folder names within the into closure.

Another workaround is to have a parent folder for these resource folders and use this parent folder in the copy spec.

Here's one way you could structure the resource folders:

<project-folder>
|
--> res
    |
    --> public
    --> data

Then in the copy spec of processResources, you can do the following:

processResources {
    from 'res'   
}

This will create folders public and data in the final jar file as you want.

Upvotes: 8

Related Questions