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.
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?
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?
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.
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?
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.
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!