There are many situations where you will need to include additional resource files or folders for compilation or testing. You might also have to exclude specific files or folders. Let us see how we can do this.
Maven is set up on your system and is verified for work. To do this, refer to the first three recipes of Chapter 1, Getting Started.
project-with-include-exclude
.build
section of your pom
file:<resources> <resource> <directory>src/resources/xml</directory> <includes> <include>*.xml</include> </includes> </resource> <resource> <directory>src/resources/json</directory> <includes> <include>include.json</include> </includes> <excludes> <exclude>exclude.json</exclude> </excludes> </resource> </resources>
mvn resources:resources
[INFO] --- maven-resources-plugin:2.6:resources (default-cli) @ project-with-include-exclude --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 2 resources [INFO] Copying 1 resource [INFO] ------------------------------------------------------------------------
resources
folder:The resources
goal of the Maven Resources plugin copies all the resources required by the source to build the output directory. This goal is bound to the process-resources
phase, which is part of the default
lifecycle.
By default, the goal copies over the contents of src/main/resources
. When the resources
tag is specified in the pom
file, it copies the contents of the directories specified there, based on the include
and exclude
filters specified.
In our specific example, we did three things:
src/resources/xml
foldersrc/resources/json
foldersrc/resouces/json
folderWhat if we need to copy test resources selectively? For this, we would need to do the following:
build
section of your pom
file:<testResources> <testResource> <directory>src/resources/xml</directory> <includes> <include>*.xml</include> </includes> </testResource> <testResource> <directory>src/resources/json</directory> <includes> <include>include.json</include> </includes> <excludes> <exclude>exclude.json</exclude> </excludes> </testResource> </testResources>
mvn resources:testResources
test-classes
folder:This will now copy over the specified test resources to the test output directory (target/test-classes
).
We saw that the resources
and testResources
goals copied resources to classes
and test-classes
respectively. What if we need to copy these to specific folders, For instance, xml
files to the xml
folder and json
files to the json
folder? The add-resource
and add-test-resource
goals of the Build Helper Maven plugin come to our assistance here.
pom
file with the following code:<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>1.9.1</version> <executions> <execution> <id>add-resource</id> <phase>generate-resources</phase> <goals> <goal>add-resource</goal> </goals> <configuration> <resources> <resource> <directory>src/resources/xml</directory> <targetPath>xml</targetPath> </resource> <resource> <directory>src/resources/json</directory> <targetPath>json</targetPath> <includes> <include>include.json</include> </includes> <excludes> <exclude>exclude.json</exclude> </excludes> </resource> </resources> </configuration> </execution> </executions> </plugin>
mvn compile
target/classes
folder now.You will see the xml
and json
subfolders with their respective content.
3.147.84.157