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:

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:

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:

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:

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:

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

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

  1. First, define a filter in our POM:

    Here, we specified the path to our properties file.
  2. Next, we need to set filtering to true for that resource:

    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:

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 *