Other Tutorials

Maven Resources Plugin

Introduction:

In this tutorial, we’ll learn about Maven Resources Plugin. We’ll start by learning what it is used for and finally learn to configure it in our own Maven project.

So let’s start by understanding what’s a resource?

Resources are non-source code files like xmls, images, property files etc that we use in our projects.

The Maven Resources Plugin helps us copying our project resources, main as well as test resources, to the output directory (build output). It empowers us to choose which files needs to be copied. It also us to define the output directory of our choice.

Resources Plugin Goals:

This plugin has three main goals:

  1. resources: Copies the main source code related resources to the main output directory
  2. testResources: Copies the test source code related resources to the test output directory
  3. copy-resources: Copies the specified resources to the output directory. It requires us to configure the project resources to be copied

The resources & testResources goals are bound to process-resources and process-test-resources lifecycle phases respectively and so execute automatically.

Configuring Resources Plugin:

Our plugin configuration in our POM would usually look like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    ...
</plugin>

The latest version of this plugin can be checked out at Maven Central.

Specifying Resources Directory:

Usually, all our maven project resources are defined under src/main/resources directory. But what if we choose to have some of our resources defined elsewhere?

For such a case, we’ll need to specify the path to such resources in our POM:

<build>
    <resources>
        <resource>
            <directory>src/otherResourcesDir1</directory>
        </resource>
        <resource>
            <directory>src/otherResourcesDir2</directory>
        </resource>
        ...
    </resources>
    ...
</build>

Here, src/otherResourcesDir1 & src/otherResourcesDir2 are the paths to some of the directories storing our additional main source code resources.

Similarly, we can define the additional test resources:

<build> 
    <testResources> 
        <testResource> 
            <directory>src/otherTestResourcesDir</directory>
         </testResource> 
    <testResources> 
    ...
</build>

Including And Excluding Files/Directories:

The Maven Resources Plugin helps us configure which files and directories to include or exclude while copying the resources to our build output.

To include files, we’ll need to add an <includes> element. Similarly to define an exclusion rule, we’ll add an <excludes> element.

For example, while producing the build output if we wish to include all .txt and .jpeg files and but to exclude all files with ‘sample’ in their name from our src/otherResourcesDir1 folder, we’ll write:

<build>
    ...
    <resources>
        <outputDirectory>outputDir</outputDirectory>
        <resource>
            <directory>src/otherResourcesDir1</directory>
            <includes>
                <include>**/*.txt</include>
                <include>**/*.jpeg</include>
            </includes>
            <excludes>
                <exclude>**/*sample*.*</exclude>
            </exludes>
        </resource>
        ...
    </resources>
    ...
</build>

Filtering:

We can choose to define variables in some property file and then use them in our resource files. Filtering allows us to replace those variables with their actual values while generating the build output.

Let’s assume we have a property file app.properties:

website.name=ProgrammerGirl

and our resource file src/main/resources/details.txt uses this variable:

I love ${website.name}

To be able to replace the variable with its value, we would:

  1. First, define a filter in our POM:
    <build>
        <filters>
            <filter>src/main/app.properties</filter>
        </filters>
        ...
    </build>

    Here, we specified the path to our properties file.

  2. Next, we need to set filtering to true for that resource:
    <build>
       ...
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        ...
    </build>

    However, we need to be careful while we turn on filtering. It will corrupt any binary files present in that path while copying them.

Using Copy-Resources Goal:

The resources and testResources goals work with build.resources and build.testResources elements and execute automatically as part of the lifecycle.

However, copy-resources goal usage requires us to define an execution phase. The basic configuration to work with this plugin goal looks something like:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-resources-sample</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>outputDir</outputDirectory>
                <resources>
                    <resource>
                        <directory>inputDir</directory>
                        <includes>
                            <include>**/*.*</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Here inputDir and outputDir are input and output directories respectively.

Conclusion:

In this tutorial, we learned about the Maven Resources Plugin. We now know how to configure and use it to meet our varying needs while copying project resources.

Be the First to comment.

Leave a Comment

Your email address will not be published. Required fields are marked *