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

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!

2 Likes