Resources are used to include additional files in the build, or use additional files for testing. They are copied into the classpath before compilation.
Resources can be specified in two places: the build
element
(reference) and the unitTest
element (reference).
These resources are specified identically, but are used separately - the first for the main build (which is
also given to the unit tests), and the other only for the unit tests.
The following is a simple use of resources:
<resources> <resource> <directory>src/main/resources</directory> </resource> </resources>
In this example, every file in src/main/resources
is copied as is to the target/classes
directory, preserving any subdirectory structure.
Pattern sets can be used to include and exclude certain files, for example:
<resource> <directory>src/main/resources</directory> <includes> <include>**/*.xml</include> </includes> <excludes> <exclude>directory1/dummy.xml</exclude> </excludes> </resource>
In some cases, a specific resource might need to be copied to a particular subdirectory. Usually, this just means
having the same subdirectory structure in your resources directory, but another alternative is to use
targetPath
:
<resource> <directory>src/main/meta</directory> <targetPath>META-INF</targetPath> <includes> <include>MANIFEST.MF</include> </includes> </resource>
Resources can also be filtered for tokens like @property.name@
, identically to Ant.
First, you must enable filtering for your resources.
<resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource>
At the moment, you must define Ant filters to achieve this. This can be done using a preGoal
on
java:jar-resources
, for example:
<preGoal name="java:jar-resources"> <ant:filter token="some.property" value="some_value" /> <ant:filter filtersfile="some.properties" /> </preGoal>
Note however that filters may cause issues with keeping a single build reproducible. Please see the Best Practices document for more information.