Apache Maven Resources Plugin

1. Overview

The Resources Plugin handles the copying of project resources to the output directory. There are two different kinds of resources: main resources and test resources. The difference is that the main resources are the resources associated to the main source code while the test resources are associated to the test source code.

2. Plugin Goals

The Resources Plugin copies files specified by Resource elements, to an output directory. The Resources Plugin has three goals:
  • resources:resources copies the resources for the main source code to the main output directory.
  • resources:testResources copies the resources for the test source code to the test output directory.
  • resources:copy-resources copies resources to an output directory.

3. Usage

The Resources Plugin handles the copying of project resources to the output directory. The following examples describe the basic usage of the Plugin.
  • Copy resources for the main source code
mvn resources:resources
  • Copy resources for the unit tests
mvn resources:testResources
  • Copy resources specified by a configuration element
mvn resources:copy-resources

4. Configuring Resources Plugin

Let’s take a look at the resources plugin in the pom.xml:
maven-resources-plugin 3.0.2 ...

5. Examples

1. Specifying resource directories

By default, Maven will look for your project's resources under src/main/resources.
Project
|-- pom.xml
`-- src
    `-- main
        `-- resources
However, all your resources may not be in src/main/resources. Thus, you'd have to specify those directories by adding the following to your POM.
<project>
 ...
 <build>
   ...
   <resources>
     <resource>
       <directory>[your folder here]</directory>
     </resource>
   </resources>
   ...
 </build>
 ...
</project>
So if your resources resides in src/my-resources
Project
|-- pom.xml
`-- src
    `-- my-resources
you can specify that directory by doing the following:
   ...
   <resources>
     <resource>
       <directory>src/my-resources</directory>
     </resource>
   </resources>
   ...
Furthermore, you can have several directories by adding multiple elements:
   ...
   <resources>
     <resource>
       <directory>resource1</directory>
     </resource>
     <resource>
       <directory>resource2</directory>
     </resource>
     <resource>
       <directory>resource3</directory>
     </resource>
   </resources>
   ...

2. Specifying a character encoding schemeA character encoding scheme such as ASCII, UTF-8 or UTF-16 can be chosen to be used for the reading and writing of files.

 <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.1.0</version>
        <configuration>
          ...
          <encoding>UTF-8</encoding>
          ...
        </configuration>
      </plugin>

Example 3

Assume we want to copy resource files from the directory input-resources to the directory output-resources and we want to exclude all files ending with the extension .png.
These requirements are satisfied with this configuration:
<configuration>
    <outputDirectory>output-resources</outputDirectory>
    <resources>
        <resource>
            <directory>input-resources</directory>
            <excludes>
                <exclude>*.png</exclude>
            </excludes>
            <filtering>true</filtering>
        </resource>
    </resources>
</configuration>
The configuration applies to all executions of the resources plugin.

6. Conclusion

In this quick guide, we went over the resources plugin and gave instructions on using and customizing it. Also we have seen the different plugin goals and their usage.

Comments