Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle copy resources, processResources

Tags:

gradle

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".

like image 813
Daniil Shevelev Avatar asked Jan 15 '15 16:01

Daniil Shevelev


2 Answers

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.

like image 192
Invisible Arrow Avatar answered Oct 30 '22 09:10

Invisible Arrow


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'}
}
like image 38
Steven Spungin Avatar answered Oct 30 '22 07:10

Steven Spungin