How can I exlude files from WAR when using WAR plugin against default or webAppDirName location


(kevin_harper) #1

I am building a slimmed down WAR file where I want to exlude a large number files from the archive. This is the behavior that I am seeing when using the WAR plugin.

  1. WAR plugin always builds the WAR file from default location ‘src.main.webapp’ or the location specified by the variable webAppDirName. All content from these directories are included in the WAR file and any Exclude condition applied against this directory is ignored. 2) If I specify an additional directory to include in the WAR archive (using the FROM tag), any Exclude condition applied against the additional directory is applied

Is this the expected behavior? How can I exclude content from being included in the WAR file the archive is always built from a default directory where all files are included?


#2

Excluding from the default webapp files works fine for me:

war {
  exclude('**/foo.jsp')
  exclude('**/bar/**')
}

In this case, any file named ‘foo.jsp’ and any file in a nested ‘bar’ directory is excluded. Can you confirm that this syntax doesn’t work for you?


(kevin_harper) #3

No - this syntax is not working for me.

war {

exclude(’**/*.jar’) }

Other variations are not working either exclude ‘/*.jar’ or exludes [’/*.jar’]


#4

You are telling Gradle to exclude any jar files it finds under the default webapp directory.

Are these jar files located in your source tree under ‘src/main/webapp’? If not, where are the jar files you want to exclude coming from?

If you can provide a small sample app demonstrating this behaviour I’m sure we can work out what’s going wrong.


(kevin_harper) #5

Yes - the jar files are actually coming from the web apps classpath and they are getting thrown into the web-inf/lib directory in the war file. But the example I provided was illustrative only. There are plenty of other examples such as trying to exclude the .class files which would be on the src/main/webapp path. Also, if I set the webAppDirName to ‘WebContent’, all the content is being included and I am not able to exclude any content such as .xml, .html, .jsp files, etc…

What is the best way to provide a zip file of a sample app?


#6

Yes - the jar files are actually coming from the web apps classpath and they are getting thrown into the web-inf/lib directory in the war file.

So these files are not in your webapp source directory, and are not controlled by an exclude element.

But the example I provided was illustrative only. There are plenty of other examples such as trying to exclude the .class files which would be on the src/main/webapp path.

But this example is the same as above. Unless your project is very unusual, ‘.class’ files will not be located under ‘src/main/webapp’. Are they?

Again, ‘exclude’ controls the files that are copied from ‘src/main/webapp’ to the war. It doesn’t control the files that are included in the WEB-INF directory, such as those coming from the classpath. To control these files, you need to work with the ‘classpath’ property of the war task.

Also, if I set the webAppDirName to ‘WebContent’, all the content is being included and I am not able to exclude any content such as .xml, .html, .jsp files, etc…

This also works fine for me.

What is the best way to provide a zip file of a sample app?

A github repository is often the easiest. Otherwise, you could share a zip file via dropbox, Google Drive, etc, and link it here.


(kevin_harper) #7

I have made some progress and can now see how the process works. I am now able to exclude a bulk of the content out of the WAR file and I have assigned a different classpath in order to keep the classpath jar files from being included in the WAR file.

However, I have one last hurdle as the class files are being included in the WAR file. I know this is an atypical scenario but is the case for the WAR file assoicated with this project. Any suggestions on how to keep the class files out of the WAR file?


#8

Where are the class files coming from? Where are they ending up?


compileOnly not useable for excluding jars from WAR?
(kevin_harper) #9

The class files are generates from the web projects java source files. My best guess is that they are being pulled in from the build/classes/main directory where Gradle places compiled class files.


#10

Yes, by default any files in src/main/java will be compile and included under WEB-INF/classes. I’m not sure why you would have java files that you didn’t want to be included in that way. For alternative java sources it would be usual to include them in a different source set “src/other/java”.

But today I just learned about a different way to exclude from a ‘war’ archive, using the ‘rootSpec’. Any exclude specified on the rootSpec will automatically apply files copied into the war file, including ‘WEB-INF’.

So the simplest solution to what you’re trying to achieve would be something like:

war {
    rootSpec.exclude("**/*.class")
    rootSpec.exclude("**/*.jar")
}

Sorry I wasn’t aware of this earlier. We all learn something new every day!


(kevin_harper) #11

Thank you - this is exactly the information that I needed. Tried it out and it works as expected!