How to exclude a file from src/main/resources from the WAR file?


The question in the title of the post pretty much sums it up.

I am using the war plugin and the conventional file structure. In


I have a bunch of properties files, velocity templates, etc. I need to exclude one of these files from the WAR file.

Previously I have done something like:

war {

which excludes files relative to


. I have tried a few things but nothing seems to work. Looking for a simple solution.


(Peter Niederwieser) #2

Can you show a concrete file path and exclude expression that doesn’t work as you’d expect it to?


Well the file path I want to exclude from WEB-INF/classes is:


It’s not really that a particular expression is working unexpectedly – I’ve tried a few things that don’t seem to work, but in most I cases I think I understand why. So now I just can’t find one that does what I want and trial and error is getting tiresome.

Here are some examples of what I’ve tried:

//try 1 (doesn't work)
//try 2 (doesn't affect resources copied to classes/ dir)
   exclude ''
//try 3
excludes = ['**/']
//try 4: doesn't exclude the file and has weird effect of duplicating all jars in WEB-INF/lib...
classpath = classpath - files('resources/')

Exclude resource file from jar file
(Peter Niederwieser) #4

‘exclude “WEB-INF/classes/”’ doesn’t work? (You should first make sure that the path is correct by looking into the War, e.g. with ‘jar -tf path/to/war’).


No that doesn’t work. Correct me if I’m wrong, but it’s not building the war file from there is it? It uses the stuff inside of ‘build/’ such as ‘build/resources/main/’.

But I don’t know how to exclude that. If you see my previous post I tried 'exclude ‘…/resources/’.

And just to be clear this is in the context of the war configuration such as:

war {
  dependsOn x, y, z
    manifest {
    archiveName = 'MyApp.war'
    //TODO: fill in the blank how to exclude src/main/resources/


Yeah that works. Are you going to change the ‘War’ task in some way?


(Peter Niederwieser) #7

The design seems to be intentional, and the soonest we could change it would be Gradle 2.0.

(Peter Niederwieser) #8

Sorry you are right. You’ll have to exclude based on, and relative to, the source path. However, for some reason the ‘War’ task shields ‘WEB-INF’ content from direct modification. Eventually I came up with this solution:

war.rootSpec.exclude ""