Piotr Zakrzewski Piotr Zakrzewski - 2 months ago 29
Java Question

Gradle war customization - folder name

I need to include additional folder inside my war file.
I found following documentation page: https://docs.gradle.org/current/userguide/war_plugin.html
And followed the example from there (only relevant part):

war {
webInf { from 'src/additionalWebInf' } // adds a file-set to the WEB-INF dir.
}


It correctly moves the content of the src/additionalWebInf folder, I need however to put the content in a folder inside webInf, not directly inside webInf.
current situation:

app.war/WEB-INF/file1
app.war/WEB-INF/file2
app.war/WEB-INF/fileN


desired situation:

app.war/WEB-INF/myCustomFolder/file1
app.war/WEB-INF/myCustomFolder/file2
app.war/WEB-INF/myCustomFolder/fileN


How do I move files into a custom (new) folder inside webInf using gradle?

Answer

The simplest thing to do is to add another level to additionalWebInf such that the files are at src/additionalWebInf/myCustomFolder/fileN.

To answer your question: Reading the documentation the webInf closure configures a CopySpec

So you can do

war {
   webInf { 
      from 'src/additionalWebInf' 
      into 'myCustomFolder'
   }
}

Or

war {
   webInf { 
      with copySpec({
         from 'src/additionalWebInf1' 
         into 'myCustomFolder1'
      })
      with copySpec({
         from 'src/additionalWebInf2' 
         into 'myCustomFolder2'
      })
   }
}