1. Overview

With the default Maven layout, we store resource files under the src/main/resources directory. After a build, Maven moves these files to the build output directory - target/classes. So they become available in the application classpath. There are cases where we have resource files under different directories. In this quick tutorial, we're going to look at how we can add multiple resource directories in a Maven-based Java project.

2. Define Multiple Resource Directories

Maven allows us to define multiple resource directories under the build configuration.

<build>
    <resources>
        <resource>
            <directory>other-resources/environment/prod</directory>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
</build>

In this configuration, we're adding a second resource directory in addition to the default one. These directories are processed with the Resource Maven plugin and copied to the build output directory.

3. Copy Resource Directory using Resource Plugin

Next, we'll copy a resource directory using the copy-resources goal of the Resources Maven plugin.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${build.outputDirectory}/additional-resources</outputDirectory>
                        <resources>
                            <resource>
                                <directory>additional-resources</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

With this configuration, Maven copies the resources under the additional-resources directory to the output directory, target/classes/additional-resources.

4. Add Resource Directory using Build Helper Plugin

Lastly, we'll use the Build Helper Maven plugin to add a resource directory.

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.0.0</version>
            <executions>
                <execution>
                    <id>add-resource</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                                <directory>
                                    imported/main/resources
                                </directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Here, we're executing the add-resource goal in the generate-resources phase. As a result, Maven copies the resources under imported/main/resources to the build output directory.

5. Summary

In this tutorial, we've investigated how we can add more resource directories to a Maven-based project.

As always the source code for all examples in this example is available on Github.