I am trying to control which files make into the WAR package that is created by mvn package
goal. Specifically, I want to exclude some files from the default src/main/resources
folder for each package (I am trying to do builds/package for different environments).
I tried using maven-war-plugin but failed. If I add this configuration (for testing):
<webResources>
<resource>
<directory>src/main/resources</directory>
<targetPath>WEB-INF/classes</targetPath>
<excludes>
<exclude>*.xml</exclude>
</excludes>
</resource>
</webResources>
…my WEB-INF/classes
will still contain the XML files. This is because webResources
parameter seems to duplicate the copying process (the above configuration actually works, an files are not copied… but they get copied in some other process instead).
Maven-war-plugin documentation states:
The default resource directory for all Maven 2 projects is src/main/resources which will end up in target/classes and in WEB-INF/classes in the WAR. The directory structure will be preserved in the process.
The WAR Plugin is also capable of including resources not found in the default resource directory through the webResources parameter.
This is a bit confusing. Does it mean that:
- The
webResources
parameter is a feature in maven-war-plugin that allows files to be included only from outsidesrc/main/resources
folder? If so, how can we alter the copied files from insidesrc/main/resources
? - The
webResources
parameter is a feature in maven-war-plugin that allows files to be included also from outsidesrc/main/resources
folder? If so, how can it be configured to do this? - Some other option?
Best Answer
Resources from the default resource directory (
src/main/resources
) are copied into the build output directory (target/classes
) during theprocess-resources
phase. Then, during thepackage
phase, the content oftarget/classes
is taken and packaged into a distributed archive, like a WAR, and end up inWEB-INF/classes
in that case.So, if you want to control what resources end up in
WEB-INF/classes
, you need to control what resources will end up intarget/classes
i.e to somehow alter the behavior of the goal bound to theprocess-resources
phase, specifically theresources:resources
goal of the Maven Resources Plugin.And to do so, (this is probably not intuitive), you can declare
exclude
orinclude
elements insideresources
element of the pom, as shown in Including and excluding files and directories. Applied to the default resource directory:And if you want to do use custom exclusion rules for different environments, combine this with profiles. For example:
And when using this profile, the xml files won't end up in
target/classes
and consequently they won't end up inWEB-INF/classes
in the final war.What you're doing here is adding an additional resources directory, which appears to be the already included default resource directory. So, whatever you'll exclude, this has no effect since the files get copied to
target/classes
duringprocess-resources
anyway and thus still end-up inWEB-INF/classes
.In other words, use
webResources
when you want to add extra resources that are not part of the default resource directory:But I don't think that this is what you're looking for here and suggest using the approach suggested above.